|
| 1 | +const expect = require('chai').expect; |
| 2 | +const CardanoCrypto = require('../../dist/index.js'); |
| 3 | +const bs58 = require('bs58'); |
| 4 | +const cbor = require('cbor'); |
| 5 | +const crc = require('crc'); |
| 6 | + |
| 7 | +const TEST_VECTORS = [ |
| 8 | + { |
| 9 | + redemptionKey: Buffer.from('qXQWDxI3JrlFRtC4SeQjeGzLbVXWBomYPbNO1Vfm1T4=', 'base64'), |
| 10 | + expectedAddress: 'Ae2tdPwUPEZ1xZTLczMGYL5PhADi1nbFmESqS9vUuLkyUe1isQ77TRUE9NS', |
| 11 | + txId: new Uint8Array([0xaa,0xd7,0x8a,0x13,0xb5,0x0a,0x01,0x4a,0x24,0x63,0x3c,0x7d,0x44,0xfd,0x8f,0x8d,0x18,0xf6,0x7b,0xbb,0x3f,0xa9,0xcb,0xce,0xdf,0x83,0x4a,0xc8,0x99,0x75,0x9d,0xcd]), |
| 12 | + txOutIndex: 1, |
| 13 | + coinValue: 12345678 |
| 14 | + } |
| 15 | +]; |
| 16 | + |
| 17 | +let mkTest = (i) => { |
| 18 | + const { redemptionKey, expectedAddress, txId, txOutIndex, coinValue } = TEST_VECTORS[i]; |
| 19 | + const cfg = CardanoCrypto.Config.defaultConfig(); |
| 20 | + |
| 21 | + describe('Test ' + i, function() { |
| 22 | + before(async () => { |
| 23 | + await CardanoCrypto.loadRustModule() |
| 24 | + }); |
| 25 | + |
| 26 | + it('generates valid address', function() { |
| 27 | + const a = CardanoCrypto.Redemption.redemptionKeyToAddress(redemptionKey, cfg.protocol_magic); |
| 28 | + const [tagged, checksum] = cbor.decode(Buffer.from(a)); |
| 29 | + expect(crc.crc32(tagged.value)).equal(checksum); |
| 30 | + }); |
| 31 | + |
| 32 | + it('creates address matching expected', function() { |
| 33 | + const a = CardanoCrypto.Redemption.redemptionKeyToAddress(redemptionKey, cfg.protocol_magic); |
| 34 | + expect(bs58.encode(Buffer.from(a))).equal(expectedAddress) |
| 35 | + }); |
| 36 | + |
| 37 | + it('generates valid transaction', function () { |
| 38 | + const address = CardanoCrypto.Redemption.redemptionKeyToAddress(redemptionKey, cfg.protocol_magic); |
| 39 | + const input = { id: txId, index: txOutIndex }; |
| 40 | + const output = { address: bs58.encode(Buffer.from(address)), value: JSON.stringify(coinValue) }; |
| 41 | + const { result: { cbor_encoded_tx } } = CardanoCrypto.Redemption.createRedemptionTransaction(redemptionKey, input, output, cfg.protocol_magic); |
| 42 | + |
| 43 | + // destruct result transaction |
| 44 | + const [[resultInputs, resultOutputs, attributes], resultWitnesses] = cbor.decode(Buffer.from(cbor_encoded_tx)); |
| 45 | + |
| 46 | + // validate inputs |
| 47 | + expect(resultInputs.length).equal(1); |
| 48 | + expect(resultInputs[0].length).equal(2); |
| 49 | + const [[intputType, inputTagged]] = resultInputs; |
| 50 | + expect(intputType).equal(0); |
| 51 | + const [inputId, inputIndex] = cbor.decode(inputTagged.value); |
| 52 | + expect(inputIndex).equal(txOutIndex); |
| 53 | + expect(inputId).deep.equal(txId); |
| 54 | + |
| 55 | + // validate outputs |
| 56 | + expect(resultInputs.length).equal(1); |
| 57 | + expect(resultInputs[0].length).equal(2); |
| 58 | + const [[outputAddress, outputValue]] = resultOutputs; |
| 59 | + expect(cbor.encode(outputAddress)).deep.equal(address); |
| 60 | + expect(outputValue).equal(coinValue); |
| 61 | + |
| 62 | + // validate witness |
| 63 | + expect(resultWitnesses.length).equal(1); |
| 64 | + expect(resultWitnesses[0].length).equal(2); |
| 65 | + const [[witnessType, witnessTagged]] = resultWitnesses; |
| 66 | + expect(witnessType).equal(2); |
| 67 | + const [witnessPub, witnessSign] = cbor.decode(witnessTagged.value); |
| 68 | + |
| 69 | + // TODO: expecting fake witness data - fix after implementing signing in Rust |
| 70 | + expect(witnessPub.toString('hex')) |
| 71 | + .equal('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'); |
| 72 | + expect(witnessSign.toString('hex')) |
| 73 | + .equal('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'); |
| 74 | + }); |
| 75 | + }); |
| 76 | +}; |
| 77 | + |
| 78 | +describe('Test redemption', function() { |
| 79 | + for (let i = 0; i < TEST_VECTORS.length; i++) { |
| 80 | + mkTest(i); |
| 81 | + } |
| 82 | +}); |
0 commit comments