Created
March 7, 2022 07:22
-
-
Save alko89/73c18c47c1bff6cbfd33568401e8ccd4 to your computer and use it in GitHub Desktop.
Multisig transaction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const fs = require('fs'); | |
const assert = require('assert'); | |
const bcoin = require('bcoin'); | |
const KeyRing = bcoin.wallet.WalletKey; | |
const Script = bcoin.Script; | |
const MTX = bcoin.MTX; | |
const Amount = bcoin.Amount; | |
const Coin = bcoin.Coin; | |
const privateKey1 = fs.readFileSync('regtest-key1.wif').toString(); | |
const privateKey2 = fs.readFileSync('regtest-key2.wif').toString(); | |
const keyPair1 = KeyRing.fromSecret(privateKey1); | |
const keyPair2 = KeyRing.fromSecret(privateKey2); | |
const publicKey1 = keyPair1.publicKey; | |
const publicKey2 = keyPair2.publicKey; | |
const m = 2; | |
const n = 2; | |
const redeem = Script.fromMultisig(m, n, [publicKey1, publicKey2]); | |
// P2SH | |
const script = Script.fromScripthash(redeem.hash160()); | |
console.log(script); | |
const txInfo = { | |
value: Amount.fromBTC('50').toValue(), | |
hash: '7e8f41f9762095be300505d9a864fef44f5d11e29aa3e1dfb078271270044db1', | |
index: 0 | |
} | |
const coin = Coin.fromJSON({ | |
version: 1, | |
height: -1, | |
value: txInfo.value, | |
coinbase: false, | |
script: script.toJSON(), | |
hash: txInfo.hash, | |
index: txInfo.index | |
}) | |
const tx = new MTX(); | |
tx.addOutput({ | |
address: 'bcrt1qsv0eakq6j7785gjtvqacvwpeu4svc0zza7kecr', | |
value: Amount.fromBTC('25').toValue(), | |
}) | |
tx.addOutput({ | |
address: 'bcrt1qjrteh4yns38heymd49w72rwhy2nh24phs862ly', // changeAddress | |
value: Amount.fromBTC('24.99').toValue() | |
}) | |
tx.addCoin(coin); | |
keyPair1.script = redeem; | |
keyPair2.script = redeem; | |
tx.scriptInput(0, coin, keyPair1); | |
tx.signInput(0, coin, keyPair1); | |
tx.signInput(0, coin, keyPair2); | |
assert(tx.verify(), 'TX is invalid'); | |
const raw = tx.toRaw(); | |
console.log(raw.toString('hex')); | |
//console.log(tx); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment