From f33e748a261a355012c72c3e9dd9a4b84784b9e9 Mon Sep 17 00:00:00 2001 From: pdp2121 <71317875+pdp2121@users.noreply.github.com> Date: Tue, 8 Nov 2022 15:25:43 -0500 Subject: [PATCH] docs: code snippets for multisigning (#2120) Add quality of life code snippets for multisigning. Co-authored-by: Jackson Mills --- packages/xrpl/snippets/src/multisigning.ts | 78 ++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 packages/xrpl/snippets/src/multisigning.ts diff --git a/packages/xrpl/snippets/src/multisigning.ts b/packages/xrpl/snippets/src/multisigning.ts new file mode 100644 index 00000000..80621b19 --- /dev/null +++ b/packages/xrpl/snippets/src/multisigning.ts @@ -0,0 +1,78 @@ +import { + multisign, + Client, + AccountSet, + convertStringToHex, + SignerListSet, +} from '../../dist/npm' + +const client = new Client('wss://s.altnet.rippletest.net:51233') + +async function multisigning(): Promise { + await client.connect() + /* + * This wallet creation is for demonstration purposes. + * In practice, users generally will not have all keys in one spot, + * hence, users need to implement a way to get signatures. + */ + const { wallet: wallet1 } = await client.fundWallet() + const { wallet: wallet2 } = await client.fundWallet() + const { wallet: walletMaster } = await client.fundWallet() + const signerListSet: SignerListSet = { + TransactionType: 'SignerListSet', + Account: walletMaster.classicAddress, + SignerEntries: [ + { + SignerEntry: { + Account: wallet1.classicAddress, + SignerWeight: 1, + }, + }, + { + SignerEntry: { + Account: wallet2.classicAddress, + SignerWeight: 1, + }, + }, + ], + SignerQuorum: 2, + } + + const signerListResponse = await client.submit(signerListSet, { + wallet: walletMaster, + }) + console.log('SignerListSet constructed successfully:') + console.log(signerListResponse) + + const accountSet: AccountSet = { + TransactionType: 'AccountSet', + Account: walletMaster.classicAddress, + Domain: convertStringToHex('example.com'), + } + const accountSetTx = await client.autofill(accountSet, 2) + console.log('AccountSet transaction is ready to be multisigned:') + console.log(accountSetTx) + const { tx_blob: tx_blob1 } = wallet1.sign(accountSetTx, true) + const { tx_blob: tx_blob2 } = wallet2.sign(accountSetTx, true) + const multisignedTx = multisign([tx_blob1, tx_blob2]) + const submitResponse = await client.submit(multisignedTx) + + if (submitResponse.result.engine_result === 'tesSUCCESS') { + console.log('The multisigned transaction was accepted by the ledger:') + console.log(submitResponse) + if (submitResponse.result.tx_json.Signers) { + console.log( + `The transaction had ${submitResponse.result.tx_json.Signers.length} signatures`, + ) + } + } else { + console.log( + "The multisigned transaction was rejected by rippled. Here's the response from rippled:", + ) + console.log(submitResponse) + } + + await client.disconnect() +} + +void multisigning()