|
| 1 | +import assert from 'assert'; |
| 2 | + |
| 3 | +import * as utxolib from '@bitgo/utxo-lib'; |
| 4 | + |
| 5 | +import { addBip322ProofMessage, getBip322ProofInputIndex, psbtIsBip322Proof } from '../../src/bip322'; |
| 6 | + |
| 7 | +import { BIP322_FIXTURE_HELLOW_WORLD_TOSIGN_PSBT } from './bip322.utils'; |
| 8 | + |
| 9 | +describe('BIP322 Proof utils', function () { |
| 10 | + it('should add a BIP322 proof message to a PSBT input', function () { |
| 11 | + const psbt = utxolib.bitgo.createPsbtFromBuffer( |
| 12 | + BIP322_FIXTURE_HELLOW_WORLD_TOSIGN_PSBT.toBuffer(), |
| 13 | + utxolib.networks.bitcoin |
| 14 | + ); |
| 15 | + const inputIndex = 0; |
| 16 | + const message = Buffer.from('Hello World'); |
| 17 | + addBip322ProofMessage(psbt, inputIndex, message); |
| 18 | + |
| 19 | + const proprietaryKeyVals = utxolib.bitgo.getPsbtInputProprietaryKeyVals(psbt.data.inputs[inputIndex], { |
| 20 | + identifier: utxolib.bitgo.PSBT_PROPRIETARY_IDENTIFIER, |
| 21 | + subtype: utxolib.bitgo.ProprietaryKeySubtype.BIP322_MESSAGE, |
| 22 | + }); |
| 23 | + |
| 24 | + assert.ok(proprietaryKeyVals.length === 1); |
| 25 | + assert.ok(proprietaryKeyVals[0].value.equals(message)); |
| 26 | + assert.ok(proprietaryKeyVals[0].key.keydata.length === 0); // keydata should be empty |
| 27 | + assert.ok(proprietaryKeyVals[0].key.identifier === utxolib.bitgo.PSBT_PROPRIETARY_IDENTIFIER); |
| 28 | + assert.ok(proprietaryKeyVals[0].key.subtype === utxolib.bitgo.ProprietaryKeySubtype.BIP322_MESSAGE); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should return the input index of a BIP322 proof message', function () { |
| 32 | + const psbt = utxolib.bitgo.createPsbtFromBuffer( |
| 33 | + BIP322_FIXTURE_HELLOW_WORLD_TOSIGN_PSBT.toBuffer(), |
| 34 | + utxolib.networks.bitcoin |
| 35 | + ); |
| 36 | + assert.ok(!psbtIsBip322Proof(psbt)); // initially should not be a BIP322 proof |
| 37 | + |
| 38 | + const inputIndex = 0; |
| 39 | + const message = Buffer.from('Hello World'); |
| 40 | + addBip322ProofMessage(psbt, inputIndex, message); |
| 41 | + |
| 42 | + const resultIndex = getBip322ProofInputIndex(psbt); |
| 43 | + assert.strictEqual(resultIndex, inputIndex); |
| 44 | + assert.ok(psbtIsBip322Proof(psbt)); |
| 45 | + }); |
| 46 | +}); |
0 commit comments