-
Notifications
You must be signed in to change notification settings - Fork 7
/
multisig.js
34 lines (21 loc) · 1007 Bytes
/
multisig.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const cbors = require('@stricahq/cbors');
const bip32ed25519 = require('@stricahq/bip32ed25519');
const typhonjs = require('@stricahq/typhonjs');
const transactionCbor = Buffer.from("CBOR_STRING", 'hex');
const decodedTx = cbors.Decoder.decode(transactionCbor).value;
const txBody = decodedTx[0];
const txBodyByteSpan = txBody.getByteSpan();
const txBytes = transactionCbor.slice(txBodyByteSpan[0], txBodyByteSpan[1]);
const txHash = typhonjs.crypto.hash32(txBytes);
// prepare witness
const privateKey = new bip32ed25519.PrivateKey(PRIVATE_KEY); // or derive from mnemonic
const publicKey = privateKey.toPublicKey().toBytes();
const signature = privateKey.sign(txHash);
const witness = [publicKey, signature];
// add witness to the original transaction
const existingWitnesses = decodedTx[1].get(0);
existingWitnesses.push(witness);
decodedTx[1].set(0, existingWitnesses);
const updatedTxCbor = cbors.Encoder.encode(decodedTx);
// the final tx cbor
console.log(updatedTxCbor.toString('hex'));