|
| 1 | +import assert from 'assert'; |
| 2 | + |
| 3 | +import * as bitcoinMessage from 'bitcoinjs-message'; |
| 4 | +import { decodeProprietaryKey } from 'bip174/src/lib/proprietaryKeyVal'; |
| 5 | +import { KeyValue } from 'bip174/src/lib/interfaces'; |
| 6 | +import { checkForOutput } from 'bip174/src/lib/utils'; |
| 7 | +import { bitgo, networks, testutil, bip32, crypto, address } from '@bitgo/utxo-lib'; |
| 8 | + |
| 9 | +import { |
| 10 | + addPaygoAddressProof, |
| 11 | + getPaygoAddressProofOutputIndex, |
| 12 | + psbtOutputIncludesPaygoAddressProof, |
| 13 | + verifyPaygoAddressProof, |
| 14 | +} from '../../../src/paygo/psbt/PayGoUtils'; |
| 15 | +import { generatePayGoAttestationProof } from '../../../src/testutil/generatePayGoAttestationProof.utils'; |
| 16 | + |
| 17 | +// To construct our PSBTs |
| 18 | +const network = networks.bitcoin; |
| 19 | +const keys = [1, 2, 3].map((v) => bip32.fromSeed(Buffer.alloc(16, `test/2/${v}`), network)); |
| 20 | +const rootWalletKeys = new bitgo.RootWalletKeys([keys[0], keys[1], keys[2]]); |
| 21 | +const psbtInputs = testutil.inputScriptTypes.map((scriptType) => ({ scriptType, value: BigInt(1000) })); |
| 22 | +const psbtOutputs = testutil.outputScriptTypes.map((scriptType) => ({ scriptType, value: BigInt(900) })); |
| 23 | +const dummyPub1 = rootWalletKeys.deriveForChainAndIndex(50, 200); |
| 24 | +// wallet pub and priv key for tbtc |
| 25 | +const attestationPubKey = dummyPub1.user.publicKey; |
| 26 | +const attestationPrvKey = dummyPub1.user.privateKey!; |
| 27 | +// const attestationPubKey = |
| 28 | +// 'xpub661MyMwAqRbcFU2Qx7pvGmmiQpVj8NcR7dSVpgqNChMkQyobpVWWERcrTb47WicmXwkhAY2VrC3hb29s18FDQWJf5pLm3saN6uLXAXpw1GV'; |
| 29 | +// const attestationPrvKey = '3FlzrW1WuPbab2GWGyx+k/pHUNefDlw3SV0NQHLrWA+YEzqbvEQmosFGXMslYqtgpeIy6HiEoEvKzbNKM7yGY8GQHv7E++/sQRFDprOyklaW7GVyC2yEZe/LdaEfBvxf2VHBmu2hYubjsdHYF5+RQ3FhnyaNT+0='; |
| 30 | +// const keypair = ECPair.fromPrivateKey(Buffer.from(attestationPrvKey)); |
| 31 | + |
| 32 | +// UUID structure |
| 33 | +const nilUUID = '00000000-0000-0000-0000-000000000000'; |
| 34 | + |
| 35 | +// our xpub converted to base58 address |
| 36 | +const addressFromPubkey = address.toBase58Check( |
| 37 | + crypto.hash160(Buffer.from(attestationPubKey)), |
| 38 | + networks.bitcoin.pubKeyHash, |
| 39 | + networks.bitcoin |
| 40 | +); |
| 41 | +// this should be retuning a Buffer |
| 42 | +const addressProofBuffer = generatePayGoAttestationProof(nilUUID, Buffer.from(addressFromPubkey)); |
| 43 | +// signature with the given msg addressProofBuffer |
| 44 | +// console.log(attestationPrvKey) |
| 45 | +const sig = bitcoinMessage.sign(addressProofBuffer, attestationPrvKey!); |
| 46 | + |
| 47 | +function getTestPsbt() { |
| 48 | + return testutil.constructPsbt(psbtInputs, psbtOutputs, network, rootWalletKeys, 'unsigned'); |
| 49 | +} |
| 50 | + |
| 51 | +describe('addPaygoAddressProof and verifyPaygoAddressProof', () => { |
| 52 | + function getPaygoProprietaryKey(proprietaryKeyVals: KeyValue[]) { |
| 53 | + return proprietaryKeyVals |
| 54 | + .map(({ key, value }) => { |
| 55 | + return { key: decodeProprietaryKey(key), value }; |
| 56 | + }) |
| 57 | + .filter((keyValue) => { |
| 58 | + return ( |
| 59 | + keyValue.key.identifier === bitgo.PSBT_PROPRIETARY_IDENTIFIER && |
| 60 | + keyValue.key.subtype === bitgo.ProprietaryKeySubtype.PAYGO_ADDRESS_ATTESTATION_PROOF |
| 61 | + ); |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + it("should fail a proof verification if the proof isn't valid", () => { |
| 66 | + const outputIndex = 0; |
| 67 | + const psbt = getTestPsbt(); |
| 68 | + addPaygoAddressProof(psbt, outputIndex, sig, Buffer.from(attestationPubKey)); |
| 69 | + const output = checkForOutput(psbt.data.outputs, outputIndex); |
| 70 | + const proofInPsbt = getPaygoProprietaryKey(output.unknownKeyVals!); |
| 71 | + assert(proofInPsbt.length === 1); |
| 72 | + assert.throws( |
| 73 | + () => verifyPaygoAddressProof(psbt, 0, Buffer.from('Random Signed Message')), |
| 74 | + (e: any) => e.message === 'Cannot verify the paygo address signature with the provided pubkey.' |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should add and verify a valid paygo address proof on the PSBT', () => { |
| 79 | + const outputIndex = 0; |
| 80 | + const psbt = getTestPsbt(); |
| 81 | + addPaygoAddressProof(psbt, outputIndex, sig, Buffer.from(attestationPubKey)); |
| 82 | + verifyPaygoAddressProof(psbt, outputIndex, Buffer.from(addressProofBuffer)); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should throw an error if there are multiple PayGo proprietary keys in the PSBT', () => { |
| 86 | + const outputIndex = 0; |
| 87 | + const psbt = getTestPsbt(); |
| 88 | + addPaygoAddressProof(psbt, outputIndex, sig, Buffer.from(attestationPubKey)); |
| 89 | + addPaygoAddressProof(psbt, outputIndex, Buffer.from('signature2'), Buffer.from('fakepubkey2s')); |
| 90 | + const output = checkForOutput(psbt.data.outputs, outputIndex); |
| 91 | + const proofInPsbt = getPaygoProprietaryKey(output.unknownKeyVals!); |
| 92 | + assert(proofInPsbt.length !== 0); |
| 93 | + assert(proofInPsbt.length > 1); |
| 94 | + assert.throws( |
| 95 | + () => verifyPaygoAddressProof(psbt, outputIndex, addressProofBuffer), |
| 96 | + (e: any) => e.message === 'There are multiple paygo address proofs encoded in the PSBT. Something went wrong.' |
| 97 | + ); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +describe('verifyPaygoAddressProof', () => { |
| 102 | + it('should throw an error if there is no PayGo address in PSBT', () => { |
| 103 | + const psbt = getTestPsbt(); |
| 104 | + assert.throws( |
| 105 | + () => verifyPaygoAddressProof(psbt, 0, addressProofBuffer), |
| 106 | + (e: any) => e.message === 'There is no paygo address proof encoded in the PSBT at output 0.' |
| 107 | + ); |
| 108 | + }); |
| 109 | +}); |
| 110 | + |
| 111 | +describe('getPaygoAddressProofIndex', () => { |
| 112 | + it('should get PayGo address proof index from PSBT if there is one', () => { |
| 113 | + const psbt = getTestPsbt(); |
| 114 | + const outputIndex = 0; |
| 115 | + addPaygoAddressProof(psbt, outputIndex, sig, Buffer.from(attestationPubKey)); |
| 116 | + assert(psbtOutputIncludesPaygoAddressProof(psbt)); |
| 117 | + assert(getPaygoAddressProofOutputIndex(psbt) === 0); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should return undefined if there is no PayGo address proof in PSBT', () => { |
| 121 | + const psbt = getTestPsbt(); |
| 122 | + assert(getPaygoAddressProofOutputIndex(psbt) === undefined); |
| 123 | + assert(!psbtOutputIncludesPaygoAddressProof(psbt)); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should return an error and fail if we have multiple PayGo address in the PSBT in the same output index', () => { |
| 127 | + const psbt = getTestPsbt(); |
| 128 | + const outputIndex = 0; |
| 129 | + addPaygoAddressProof(psbt, outputIndex, sig, Buffer.from(attestationPubKey)); |
| 130 | + addPaygoAddressProof(psbt, outputIndex, sig, Buffer.from('xpub12345abcdef29a028510d3b2d4')); |
| 131 | + assert.throws( |
| 132 | + () => getPaygoAddressProofOutputIndex(psbt), |
| 133 | + (e: any) => e.message === 'There are multiple PayGo addresses in the PSBT output 0.' |
| 134 | + ); |
| 135 | + }); |
| 136 | +}); |
0 commit comments