|
| 1 | +/* eslint-disable sonarjs/no-duplicate-string */ |
| 2 | +import * as Crypto from '@cardano-sdk/crypto'; |
| 3 | +import { AddressType, AsyncKeyAgent, GroupedAddress, SignBlobResult, util } from '@cardano-sdk/key-management'; |
| 4 | +import { BehaviorSubject, firstValueFrom } from 'rxjs'; |
| 5 | +import { Cardano } from '@cardano-sdk/core'; |
| 6 | +import { GenericTxBuilder, OutputValidation } from '../../src'; |
| 7 | +import { dummyLogger } from 'ts-log'; |
| 8 | +import { generateRandomHexString, mockProviders as mocks } from '@cardano-sdk/util-dev'; |
| 9 | +import delay from 'delay'; |
| 10 | + |
| 11 | +class StubKeyAgent implements AsyncKeyAgent { |
| 12 | + knownAddresses$ = new BehaviorSubject<GroupedAddress[]>([]); |
| 13 | + |
| 14 | + constructor(private inputResolver: Cardano.InputResolver) {} |
| 15 | + |
| 16 | + deriveAddress(): Promise<GroupedAddress> { |
| 17 | + throw new Error('Method not implemented.'); |
| 18 | + } |
| 19 | + derivePublicKey(): Promise<Crypto.Ed25519PublicKeyHex> { |
| 20 | + throw new Error('Method not implemented.'); |
| 21 | + } |
| 22 | + signBlob(): Promise<SignBlobResult> { |
| 23 | + throw new Error('Method not implemented.'); |
| 24 | + } |
| 25 | + async signTransaction(txInternals: Cardano.TxBodyWithHash): Promise<Cardano.Signatures> { |
| 26 | + const signatures = new Map<Crypto.Ed25519PublicKeyHex, Crypto.Ed25519SignatureHex>(); |
| 27 | + const knownAddresses = await firstValueFrom(this.knownAddresses$); |
| 28 | + for (const _ of await util.ownSignatureKeyPaths(txInternals.body, knownAddresses, this.inputResolver)) { |
| 29 | + signatures.set( |
| 30 | + Crypto.Ed25519PublicKeyHex(generateRandomHexString(64)), |
| 31 | + Crypto.Ed25519SignatureHex(generateRandomHexString(128)) |
| 32 | + ); |
| 33 | + } |
| 34 | + return signatures; |
| 35 | + } |
| 36 | + getChainId(): Promise<Cardano.ChainId> { |
| 37 | + throw new Error('Method not implemented.'); |
| 38 | + } |
| 39 | + getBip32Ed25519(): Promise<Crypto.Bip32Ed25519> { |
| 40 | + throw new Error('Method not implemented.'); |
| 41 | + } |
| 42 | + getExtendedAccountPublicKey(): Promise<Crypto.Bip32PublicKeyHex> { |
| 43 | + throw new Error('Method not implemented.'); |
| 44 | + } |
| 45 | + async setKnownAddresses(addresses: GroupedAddress[]): Promise<void> { |
| 46 | + this.knownAddresses$.next(addresses); |
| 47 | + } |
| 48 | + shutdown(): void { |
| 49 | + throw new Error('Method not implemented.'); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +describe('TxBuilder bootstrap', () => { |
| 54 | + it('awaits for non-empty knownAddresses$', async () => { |
| 55 | + // Initialize the TxBuilder |
| 56 | + const output = mocks.utxo[0][1]; |
| 57 | + const rewardAccount = mocks.rewardAccount; |
| 58 | + const inputResolver: Cardano.InputResolver = { |
| 59 | + resolveInput: async (txIn) => |
| 60 | + mocks.utxo.find( |
| 61 | + ([hydratedTxIn]) => txIn.txId === hydratedTxIn.txId && txIn.index === hydratedTxIn.index |
| 62 | + )?.[1] || null |
| 63 | + }; |
| 64 | + const keyAgent = new StubKeyAgent(inputResolver); |
| 65 | + const txBuilderProviders = { |
| 66 | + genesisParameters: jest.fn().mockResolvedValue(mocks.genesisParameters), |
| 67 | + protocolParameters: jest.fn().mockResolvedValue(mocks.protocolParameters), |
| 68 | + rewardAccounts: jest.fn().mockResolvedValue([ |
| 69 | + { |
| 70 | + address: rewardAccount, |
| 71 | + keyStatus: Cardano.StakeKeyStatus.Unregistered, |
| 72 | + rewardBalance: mocks.rewardAccountBalance |
| 73 | + } |
| 74 | + ]), |
| 75 | + tip: jest.fn().mockResolvedValue(mocks.ledgerTip), |
| 76 | + utxoAvailable: jest.fn().mockResolvedValue(mocks.utxo) |
| 77 | + }; |
| 78 | + const outputValidator = { |
| 79 | + validateOutput: jest.fn().mockResolvedValue({ coinMissing: 0n } as OutputValidation) |
| 80 | + }; |
| 81 | + const builderParams = { |
| 82 | + inputResolver, |
| 83 | + keyAgent, |
| 84 | + logger: dummyLogger, |
| 85 | + outputValidator, |
| 86 | + txBuilderProviders |
| 87 | + }; |
| 88 | + const txBuilder = new GenericTxBuilder(builderParams); |
| 89 | + |
| 90 | + // Build and sign a tx |
| 91 | + const signedTxReady = txBuilder.addOutput(output).build().sign(); |
| 92 | + await delay(1); |
| 93 | + // keyAgent knownAddresses are initially [], |
| 94 | + // but then eventually resolves to some addresses |
| 95 | + // eslint-disable-next-line @typescript-eslint/no-floating-promises |
| 96 | + keyAgent.setKnownAddresses([ |
| 97 | + { |
| 98 | + accountIndex: 0, |
| 99 | + address: mocks.utxo[0][1].address, |
| 100 | + index: 0, |
| 101 | + networkId: Cardano.NetworkId.Testnet, |
| 102 | + rewardAccount: mocks.rewardAccount, |
| 103 | + type: AddressType.External |
| 104 | + } |
| 105 | + ]); |
| 106 | + const signedTx = await signedTxReady; |
| 107 | + expect(signedTx.tx.witness.signatures.size).toBe(1); |
| 108 | + }); |
| 109 | +}); |
0 commit comments