Skip to content

Instantly share code, notes, and snippets.

@wilsonianb
Created June 30, 2018 04:55
Show Gist options
  • Save wilsonianb/eeba6cfcd15c802d93e42a2881937083 to your computer and use it in GitHub Desktop.
Save wilsonianb/eeba6cfcd15c802d93e42a2881937083 to your computer and use it in GitHub Desktop.
var ripple = require('ripple-lib').RippleAPI
var kp = require('ripple-keypairs')
const api = new ripple.RippleAPI({
server: 'wss://s.altnet.rippletest.net'
});
async function addSigners (account, secret, signers, threshold) {
let weights = []
for (let signer of signers) {
weights.push({
address: signer.address,
weight: 1
})
}
const tx = await api.prepareSettings(account, {
signers: {
threshold: threshold,
weights: weights
}
})
const signed = api.sign(tx.txJSON, secret);
console.log(signed.id)
return await api.submit(signed.signedTransaction)
}
async function sendPayment (account, destination, signers, threshold) {
let signatures = []
for (let i=0; i<threshold; i++) {
const tx = await api.preparePayment(account,
{
"source": {
"address": account,
"amount": {
"value": "1",
"currency": "XRP"
}
},
"destination": {
"address": destination,
"minAmount": {
"value": "1",
"currency": "XRP"
}
}
}, {
signersCount: threshold
})
signatures.push(api.sign(tx.txJSON, signers[i].seed, {
signAs: signers[i].address
}).signedTransaction)
}
const combined = api.combine(signatures)
console.log(combined.id)
return await api.submit(combined.signedTransaction)
}
async function doMultiSign (account, secret, destination, nSigners, threshold) {
try {
await api.connect()
let signers = []
for (let i=0; i<nSigners; i++) {
const seed = kp.generateSeed();
const keypair = kp.deriveKeypair(seed);
const address = kp.deriveAddress(keypair.publicKey);
signers.push({
address: address,
seed: seed
})
}
console.log(await addSigners(account, secret, signers, threshold))
console.log(await sendPayment(account, destination, signers, threshold))
} catch (err) {
console.log(err)
}
}
doMultiSign(
'rDd6FpNbeY2CrQajSmP178BmNGusmQiYMM',
'snyfcPrgMHCDR57M6Wrps8y6RPhiF',
'rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe',
3, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment