0

I have walked through the official document and found a page about how to transfer using polkadot-js https://polkadot.js.org/docs/api/examples/promise/make-transfer

const transfer = api.tx.balances.transfer(BOB, 12345);
const hash = await transfer.signAndSend(alice);

I want to know if I can split the signAndSend method into two and execute at different machines. like in a client machine, in the browser compute the signature.

const transfer = api.tx.balances.transfer(BOB, 12345);
const signature = await transfer.signAsync(alice);

and then in the server side send the transfer transaction.

const mockSigner = createMockSigner(signature); // signature is computed from the client side and send to server over HTTP
const transfer = api.tx.balances.transfer(BOB, 12345);
const res = transfer.send({signer: mockSigner});

The above example doesn't work, I just want to express if I can do sign and send in different machines.

Joey
  • 1,233
  • 3
  • 11
  • 18

2 Answers2

1

Signing a transaction on one computer and sending it from a second computer is definitely possible.

PolkadotJS Tools contains a method for building and signing a transaction offline. You can find the source here. Please note that building the transaction in the browser will still require access to a polkadot node (the endpoint in the code).

The signer sendOffline command has the exact same API, but will not broadcast the transaction. submit and sendOffline must be connected to a node to fetch the current metadata and construct a valid transaction. Their API has the format:

Therefore, you'd need to run a light client in the browser in order to have access to current block information or attach to some other node endpoint outwith the browser.

forgetso
  • 2,194
  • 14
  • 33
  • Thanks, I read something about sign offline, still don't quite understand why sign offline is so difficult and different in substrate. Is that means that in online mode, when calling `api.tx.balance.transfer` a transfer object is unique so its signature can't be send to other machine? And in offline mode we construct transfer object manually so the signature can be shared? – Joey Nov 17 '21 at 07:38
  • Fundamentally both sign offline and online are doing the same thing in terms of signing. [cmdSendOffline.ts](https://github.com/polkadot-js/tools/blob/master/packages/signer-cli/src/cmdSendOffline.ts) creates the transaction payload and signs it then dumps it to JSON. [cmdSign.ts](https://github.com/polkadot-js/tools/blob/master/packages/signer-cli/src/cmdSign.ts) signs a payload only. You can also pass the `submit` command on the CLI in which case the signed payload from [cmdSign](https://github.com/polkadot-js/tools/blob/master/packages/signer-cli/src/cmdSubmit.ts) is submitted – forgetso Nov 19 '21 at 16:00
0

The offline sign version: https://gist.github.com/xcaptain/4d190232411dcf27441d9fadd7ff6988

The online sign version:

const transfer = api.tx.balances.transfer(BOB, 12345);
const signedExtrinsic = await transfer.signAsync(alice).toJSON();
await api.rpc.author.submitExtrinsic(signedExtrinsic);

Don't know what's the difference, but they both work.

Joey
  • 1,233
  • 3
  • 11
  • 18