我可以使用 polkadot-js 发送异步交易吗

Can I send transactions async using polkadot-js

我翻遍了官方文档,找到了一个关于如何使用 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);

我想知道是否可以将signAndSend方法拆分成两个并在不同的机器上执行。就像在客户机中一样,在浏览器中计算签名。

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

然后在服务器端发送转账交易。

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});

上面的例子不行,我只是想表达一下我能不能在不同的机器上签发。

在一台计算机上签署交易并从另一台计算机发送它是完全可能的。

PolkadotJS Tools contains a method for building and signing a transaction offline. You can find the source here。请注意,在浏览器中构建交易仍然需要访问 polkadot 节点(代码中的 endpoint)。

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:

因此,您需要在浏览器中 运行 light client 才能访问当前区块信息或附加到浏览器之外的其他节点端点。

线下签到版本: https://gist.github.com/xcaptain/4d190232411dcf27441d9fadd7ff6988

网签版本:

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

不知道有什么区别,但它们都有效。