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