|
| 1 | +import * as CardanoWasm from '@emurgo/cardano-serialization-lib-nodejs'; |
| 2 | +import should from 'should'; |
| 3 | +import { coins } from '@bitgo/statics'; |
| 4 | +import { KeyPair, Transaction, TransactionBuilderFactory } from '../../src'; |
| 5 | +import { privateKeys, rawTx } from '../resources'; |
| 6 | + |
| 7 | +const TEST_ADDRESS = 'addr_test1vr8rakm66rcfv4fcxqykg5lf0yv7lsyk9mvapx369jpvtcgfcuk7f'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Builds a synthetic Plutus transaction analogous to CBOR WP would hand BitGoJS |
| 11 | + * for RealFi pledge (parse -> sign -> re-serialize) flows. |
| 12 | + */ |
| 13 | +function buildPlutusFixtureHex(includeAuxiliaryData = false): string { |
| 14 | + const addr = CardanoWasm.Address.from_bech32(TEST_ADDRESS); |
| 15 | + |
| 16 | + const inputs = CardanoWasm.TransactionInputs.new(); |
| 17 | + inputs.add(CardanoWasm.TransactionInput.new(CardanoWasm.TransactionHash.from_bytes(Buffer.alloc(32, 1)), 0)); |
| 18 | + |
| 19 | + const datum = CardanoWasm.PlutusData.new_bytes(Buffer.from('deadbeef', 'hex')); |
| 20 | + |
| 21 | + const outputs = CardanoWasm.TransactionOutputs.new(); |
| 22 | + const outputBuilder = CardanoWasm.TransactionOutputBuilder.new().with_address(addr).with_plutus_data(datum); |
| 23 | + const mainOutput = outputBuilder.next().with_coin(CardanoWasm.BigNum.from_str('2000000')).build(); |
| 24 | + outputs.add(mainOutput); |
| 25 | + |
| 26 | + const body = CardanoWasm.TransactionBody.new_tx_body(inputs, outputs, CardanoWasm.BigNum.from_str('200000')); |
| 27 | + |
| 28 | + const collateral = CardanoWasm.TransactionInputs.new(); |
| 29 | + collateral.add(CardanoWasm.TransactionInput.new(CardanoWasm.TransactionHash.from_bytes(Buffer.alloc(32, 2)), 0)); |
| 30 | + body.set_collateral(collateral); |
| 31 | + |
| 32 | + const collateralReturn = CardanoWasm.TransactionOutput.new( |
| 33 | + addr, |
| 34 | + CardanoWasm.Value.new(CardanoWasm.BigNum.from_str('1000000')) |
| 35 | + ); |
| 36 | + body.set_collateral_return(collateralReturn); |
| 37 | + body.set_total_collateral(CardanoWasm.BigNum.from_str('1000000')); |
| 38 | + |
| 39 | + const referenceInputs = CardanoWasm.TransactionInputs.new(); |
| 40 | + referenceInputs.add(CardanoWasm.TransactionInput.new(CardanoWasm.TransactionHash.from_bytes(Buffer.alloc(32, 3)), 0)); |
| 41 | + body.set_reference_inputs(referenceInputs); |
| 42 | + body.set_script_data_hash(CardanoWasm.ScriptDataHash.from_bytes(Buffer.alloc(32, 4))); |
| 43 | + |
| 44 | + const witnessSet = CardanoWasm.TransactionWitnessSet.new(); |
| 45 | + |
| 46 | + const plutusScripts = CardanoWasm.PlutusScripts.new(); |
| 47 | + plutusScripts.add(CardanoWasm.PlutusScript.new(Buffer.from('510100003222253330033371e00c', 'hex'))); |
| 48 | + witnessSet.set_plutus_scripts(plutusScripts); |
| 49 | + |
| 50 | + const plutusDataList = CardanoWasm.PlutusList.new(); |
| 51 | + plutusDataList.add(datum); |
| 52 | + witnessSet.set_plutus_data(plutusDataList); |
| 53 | + |
| 54 | + const redeemers = CardanoWasm.Redeemers.new(); |
| 55 | + redeemers.add( |
| 56 | + CardanoWasm.Redeemer.new( |
| 57 | + CardanoWasm.RedeemerTag.new_spend(), |
| 58 | + CardanoWasm.BigNum.from_str('0'), |
| 59 | + datum, |
| 60 | + CardanoWasm.ExUnits.new(CardanoWasm.BigNum.from_str('1000000'), CardanoWasm.BigNum.from_str('500000')) |
| 61 | + ) |
| 62 | + ); |
| 63 | + witnessSet.set_redeemers(redeemers); |
| 64 | + |
| 65 | + let auxiliaryData: CardanoWasm.AuxiliaryData | undefined; |
| 66 | + if (includeAuxiliaryData) { |
| 67 | + const metadata = CardanoWasm.GeneralTransactionMetadata.new(); |
| 68 | + metadata.insert(CardanoWasm.BigNum.from_str('674'), CardanoWasm.TransactionMetadatum.new_text('realfi-test')); |
| 69 | + auxiliaryData = CardanoWasm.AuxiliaryData.new(); |
| 70 | + auxiliaryData.set_metadata(metadata); |
| 71 | + body.set_auxiliary_data_hash(CardanoWasm.hash_auxiliary_data(auxiliaryData)); |
| 72 | + } |
| 73 | + |
| 74 | + const tx = |
| 75 | + auxiliaryData !== undefined |
| 76 | + ? CardanoWasm.Transaction.new(body, witnessSet, auxiliaryData) |
| 77 | + : CardanoWasm.Transaction.new(body, witnessSet); |
| 78 | + return Buffer.from(tx.to_bytes()).toString('hex'); |
| 79 | +} |
| 80 | + |
| 81 | +describe('ADA Plutus Passthrough Builder', () => { |
| 82 | + const plutusFixtureHex = buildPlutusFixtureHex(); |
| 83 | + |
| 84 | + it('should detect plutus data on the parsed transaction', () => { |
| 85 | + const tx = new Transaction(coins.get('tada')); |
| 86 | + tx.fromRawTransaction(plutusFixtureHex); |
| 87 | + tx.hasPlutusData().should.be.true(); |
| 88 | + |
| 89 | + const plainTx = new Transaction(coins.get('tada')); |
| 90 | + plainTx.fromRawTransaction(rawTx.unsignedNewPledgeTx); |
| 91 | + plainTx.hasPlutusData().should.be.false(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should round-trip a plutus transaction byte-for-byte with no new signature', async () => { |
| 95 | + const factory = new TransactionBuilderFactory(coins.get('tada')); |
| 96 | + const txBuilder = factory.from(plutusFixtureHex); |
| 97 | + const tx = (await txBuilder.build()) as Transaction; |
| 98 | + tx.toBroadcastFormat().should.equal(plutusFixtureHex); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should preserve plutus fields and produce a valid vkey for the body hash', async () => { |
| 102 | + const factory = new TransactionBuilderFactory(coins.get('tada')); |
| 103 | + const txBuilder = factory.from(plutusFixtureHex); |
| 104 | + txBuilder.sign({ key: privateKeys.prvKey4 }); |
| 105 | + const tx = (await txBuilder.build()) as Transaction; |
| 106 | + const broadcastHex = tx.toBroadcastFormat(); |
| 107 | + broadcastHex.should.not.equal(plutusFixtureHex); |
| 108 | + |
| 109 | + const reparsed = CardanoWasm.Transaction.from_bytes(Buffer.from(broadcastHex, 'hex')); |
| 110 | + const body = reparsed.body(); |
| 111 | + const witnessSet = reparsed.witness_set(); |
| 112 | + |
| 113 | + should.exist(body.collateral()); |
| 114 | + should.exist(body.collateral_return()); |
| 115 | + should.exist(body.total_collateral()); |
| 116 | + should.exist(body.reference_inputs()); |
| 117 | + should.exist(body.script_data_hash()); |
| 118 | + body.outputs().get(0).has_plutus_data().should.be.true(); |
| 119 | + |
| 120 | + should.exist(witnessSet.plutus_scripts()); |
| 121 | + should.exist(witnessSet.plutus_data()); |
| 122 | + should.exist(witnessSet.redeemers()); |
| 123 | + |
| 124 | + const originalTx = CardanoWasm.Transaction.from_bytes(Buffer.from(plutusFixtureHex, 'hex')); |
| 125 | + body.collateral()!.to_bytes().should.deepEqual(originalTx.body().collateral()!.to_bytes()); |
| 126 | + body.collateral_return()!.to_bytes().should.deepEqual(originalTx.body().collateral_return()!.to_bytes()); |
| 127 | + body.total_collateral()!.to_bytes().should.deepEqual(originalTx.body().total_collateral()!.to_bytes()); |
| 128 | + body.reference_inputs()!.to_bytes().should.deepEqual(originalTx.body().reference_inputs()!.to_bytes()); |
| 129 | + body.script_data_hash()!.to_bytes().should.deepEqual(originalTx.body().script_data_hash()!.to_bytes()); |
| 130 | + witnessSet.plutus_scripts()!.to_bytes().should.deepEqual(originalTx.witness_set().plutus_scripts()!.to_bytes()); |
| 131 | + witnessSet.plutus_data()!.to_bytes().should.deepEqual(originalTx.witness_set().plutus_data()!.to_bytes()); |
| 132 | + witnessSet.redeemers()!.to_bytes().should.deepEqual(originalTx.witness_set().redeemers()!.to_bytes()); |
| 133 | + |
| 134 | + const keyPair = new KeyPair({ prv: privateKeys.prvKey4 }); |
| 135 | + const expected = CardanoWasm.make_vkey_witness( |
| 136 | + CardanoWasm.hash_transaction(body), |
| 137 | + CardanoWasm.PrivateKey.from_normal_bytes(Buffer.from(keyPair.getKeys().prv!, 'hex')) |
| 138 | + ); |
| 139 | + const vkeys = witnessSet.vkeys(); |
| 140 | + should.exist(vkeys); |
| 141 | + vkeys!.len().should.equal(1); |
| 142 | + vkeys!.get(0).vkey().public_key().to_hex().should.equal(keyPair.getKeys().pub); |
| 143 | + vkeys!.get(0).signature().to_hex().should.equal(expected.signature().to_hex()); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should preserve auxiliary data through the passthrough path', async () => { |
| 147 | + const fixtureWithAux = buildPlutusFixtureHex(true); |
| 148 | + const factory = new TransactionBuilderFactory(coins.get('tada')); |
| 149 | + const txBuilder = factory.from(fixtureWithAux); |
| 150 | + txBuilder.sign({ key: privateKeys.prvKey4 }); |
| 151 | + const tx = (await txBuilder.build()) as Transaction; |
| 152 | + |
| 153 | + const reparsed = CardanoWasm.Transaction.from_bytes(Buffer.from(tx.toBroadcastFormat(), 'hex')); |
| 154 | + const original = CardanoWasm.Transaction.from_bytes(Buffer.from(fixtureWithAux, 'hex')); |
| 155 | + should.exist(reparsed.auxiliary_data()); |
| 156 | + reparsed.auxiliary_data()!.to_bytes().should.deepEqual(original.auxiliary_data()!.to_bytes()); |
| 157 | + should.exist(reparsed.body().auxiliary_data_hash()); |
| 158 | + reparsed |
| 159 | + .body() |
| 160 | + .auxiliary_data_hash()! |
| 161 | + .to_bytes() |
| 162 | + .should.deepEqual(original.body().auxiliary_data_hash()!.to_bytes()); |
| 163 | + }); |
| 164 | +}); |
0 commit comments