From 8e238da45a7e5055d5ed45f6bf175eb10cd5ad28 Mon Sep 17 00:00:00 2001 From: John Oshalusi Date: Thu, 30 May 2024 18:26:11 +0100 Subject: [PATCH] test: fix failing tests --- .../TypeormStakePoolProvider/util.test.ts | 2 +- .../src/transformers/certificates.ts | 6 +- .../test/LedgerKeyAgent.test.ts | 6 +- .../test/transformers/certificates.test.ts | 63 ++++++-- .../transformers/collateralInputs.test.ts | 3 +- .../test/transformers/txIn.test.ts | 8 +- .../transformers/additionalWitnessRequests.ts | 5 +- .../src/transformers/certificates.ts | 15 +- .../src/transformers/keyPaths.ts | 10 +- .../src/transformers/withdrawals.ts | 4 +- packages/hardware-trezor/test/testData.ts | 6 +- .../additionalWitnessRequests.test.ts | 4 +- .../test/transformers/keyPaths.test.ts | 6 +- .../test/transformers/tx.test.ts | 4 +- .../StaticChangeAddressResolver.test.ts | 5 +- .../key-management/test/Bip32Account.test.ts | 30 +++- .../test/InMemoryKeyAgent.test.ts | 24 ++- .../key-management/test/KeyAgentBase.test.ts | 23 ++- .../test/cip8/cip30signData.test.ts | 3 + .../key-management/test/mocks/stakeKey.ts | 3 +- .../test/util/createWitnesser.test.ts | 6 +- .../test/util/ensureStakeKeys.test.ts | 5 +- packages/key-management/test/util/key.test.ts | 16 +- .../test/util/ownSignaturePaths.test.ts | 152 ++++++++++++++---- .../test/util/stubSignTransaction.test.ts | 6 +- .../src/tx-builder/initializeTx.ts | 2 +- .../tx-builder/TxBuilder.bootstrap.test.ts | 5 +- .../test/tx-builder/TxBuilder.test.ts | 8 +- .../TxBuilderDelegatePortfolio.test.ts | 12 +- .../tx-builder/TxBuilderPlutusScripts.test.ts | 5 +- .../wallet/test/PersonalWallet/load.test.ts | 37 +++-- .../test/PersonalWallet/methods.test.ts | 7 +- .../test/PersonalWallet/rollback.test.ts | 5 +- .../test/PersonalWallet/shutdown.test.ts | 21 +-- .../hardware/ledger/LedgerKeyAgent.test.ts | 15 +- .../CustomObservableWallet.test.ts | 10 +- .../test/integration/cip30mapping.test.ts | 23 +-- packages/wallet/test/integration/util.ts | 4 +- .../test/services/ChangeAddress/testData.ts | 35 ++-- .../DelegationDistributionTracker.test.ts | 4 +- .../services/KeyAgent/restoreKeyAgent.test.ts | 8 +- .../services/PublicStakeKeysTracker.test.ts | 11 +- .../wallet/test/services/WalletUtil.test.ts | 5 +- .../SingleAddressDiscovery.test.ts | 5 +- packages/wallet/test/util.ts | 6 +- 45 files changed, 451 insertions(+), 192 deletions(-) diff --git a/packages/cardano-services/test/StakePool/TypeormStakePoolProvider/util.test.ts b/packages/cardano-services/test/StakePool/TypeormStakePoolProvider/util.test.ts index acce639583d..c2fd5c21fd8 100644 --- a/packages/cardano-services/test/StakePool/TypeormStakePoolProvider/util.test.ts +++ b/packages/cardano-services/test/StakePool/TypeormStakePoolProvider/util.test.ts @@ -4,7 +4,7 @@ import { validateFuzzyOptions, withTextFilter } from '../../../src/StakePool/Typ describe('TypeormStakePoolProvider utils', () => { describe('validateFuzzyOptions', () => { it('throws if value is not a valid JSON encoded string', () => - expect(() => validateFuzzyOptions('test')).toThrow('Unexpected token e in JSON at position 1')); + expect(() => validateFuzzyOptions('test')).toThrow(/Unexpected token/)); it('throws if value is not an object', () => expect(() => validateFuzzyOptions('"test"')).toThrow('must be an object')); it('throws without threshold', () => diff --git a/packages/hardware-ledger/src/transformers/certificates.ts b/packages/hardware-ledger/src/transformers/certificates.ts index f5b118620d8..1e86f145054 100644 --- a/packages/hardware-ledger/src/transformers/certificates.ts +++ b/packages/hardware-ledger/src/transformers/certificates.ts @@ -104,7 +104,7 @@ const getCredentialType = (knownAddress: GroupedAddress | undefined) => { const stakeCredentialMapper = (credential: Cardano.Credential, context: LedgerTxTransformerContext) => { const knownAddress = getKnownAddress(credential, context); const credentialType = getCredentialType(knownAddress); - const path = util.stakeKeyPathFromGroupedAddress({ address: knownAddress }); + const path = util.stakeKeyPathFromGroupedAddress({ address: knownAddress, purpose: context.purpose }); return credentialMapper(credential, credentialType, path); }; @@ -160,7 +160,7 @@ const getPoolOperatorKeyPath = ( context: LedgerTxTransformerContext ): Ledger.BIP32Path | null => { const knownAddress = context?.knownAddresses.find((address) => address.rewardAccount === operator); - return util.stakeKeyPathFromGroupedAddress({ address: knownAddress }); + return util.stakeKeyPathFromGroupedAddress({ address: knownAddress, purpose: context.purpose }); }; export const poolRegistrationCertificate: Transform< @@ -270,7 +270,7 @@ const poolRetirementCertificate: Transform< ) ); - const poolKeyPath = util.stakeKeyPathFromGroupedAddress({ address: knownAddress }); + const poolKeyPath = util.stakeKeyPathFromGroupedAddress({ address: knownAddress, purpose: context?.purpose }); if (!poolKeyPath) throw new InvalidArgumentError('certificate', 'Missing key matching pool retirement certificate.'); diff --git a/packages/hardware-ledger/test/LedgerKeyAgent.test.ts b/packages/hardware-ledger/test/LedgerKeyAgent.test.ts index 87b16f18b87..2c3ee82aa7f 100644 --- a/packages/hardware-ledger/test/LedgerKeyAgent.test.ts +++ b/packages/hardware-ledger/test/LedgerKeyAgent.test.ts @@ -3,7 +3,7 @@ import * as Crypto from '@cardano-sdk/crypto'; import * as Ledger from '@cardano-foundation/ledgerjs-hw-app-cardano'; import { Ada, InvalidDataReason } from '@cardano-foundation/ledgerjs-hw-app-cardano'; import { Cardano } from '@cardano-sdk/core'; -import { CardanoKeyConst, CommunicationType, util } from '@cardano-sdk/key-management'; +import { CardanoKeyConst, CommunicationType, KeyPurpose, util } from '@cardano-sdk/key-management'; import { LedgerKeyAgent } from '../src'; import { dummyLogger } from 'ts-log'; import { poolId, poolParameters, pureAdaTxOut, stakeKeyHash, txIn, txOutWithDatum } from './testData'; @@ -366,6 +366,7 @@ describe('LedgerKeyAgent', () => { const txId = '0000000000000000000000000000000000000000000000000000000000000000' as unknown as Cardano.TransactionId; const noAddressesOptions = { knownAddresses: [], + purpose: KeyPurpose.STANDARD, txInKeyPathMap: {} }; @@ -379,7 +380,8 @@ describe('LedgerKeyAgent', () => { communicationType: CommunicationType.Node, extendedAccountPublicKey: Crypto.Bip32PublicKeyHex( '00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' - ) + ), + purpose: KeyPurpose.STANDARD }, { bip32Ed25519: new Crypto.SodiumBip32Ed25519(), diff --git a/packages/hardware-ledger/test/transformers/certificates.test.ts b/packages/hardware-ledger/test/transformers/certificates.test.ts index 73e1591783a..f8eb851ceba 100644 --- a/packages/hardware-ledger/test/transformers/certificates.test.ts +++ b/packages/hardware-ledger/test/transformers/certificates.test.ts @@ -1,10 +1,10 @@ /* eslint-disable sonarjs/no-duplicate-string */ import * as Ledger from '@cardano-foundation/ledgerjs-hw-app-cardano'; import { - AccountKeyDerivationPath, AddressType, CardanoKeyConst, GroupedAddress, + KeyPurpose, KeyRole, TxInId, TxInKeyPathMap, @@ -22,23 +22,26 @@ import { } from '../testData'; import { Cardano } from '@cardano-sdk/core'; import { Hash28ByteBase16 } from '@cardano-sdk/crypto'; -import { LedgerTxTransformerContext } from '../../src'; -import { getKnownAddress, mapCerts } from '../../src/transformers/certificates'; +import { LedgerTxTransformerContext, getKnownAddress, mapCerts } from '../../src'; export const stakeKeyPath = { index: 0, + purpose: KeyPurpose.STANDARD, role: KeyRole.Stake }; -const createGroupedAddress = ( - address: Cardano.PaymentAddress, - rewardAccount: Cardano.RewardAccount, - type: AddressType, - index: number, - stakeKeyDerivationPath: AccountKeyDerivationPath -): GroupedAddress => + +const createGroupedAddress = ({ + address, + rewardAccount, + stakeKeyDerivationPath, + index, + purpose, + type +}: Omit): GroupedAddress => ({ address, index, + purpose, rewardAccount, stakeKeyDerivationPath, type @@ -64,6 +67,7 @@ export const createTxInKeyPathMapMock = (knownAddresses: GroupedAddress[]): TxIn const txInId: TxInId = `MockTxIn_${index}` as TxInId; // Mock TxInId creation result[txInId] = { index: address.index, + purpose: address.purpose, role: KeyRole.Internal }; } @@ -78,13 +82,42 @@ const mockContext: LedgerTxTransformerContext = { handleResolutions: [], knownAddresses: [ - createGroupedAddress(address1, ownRewardAccount, AddressType.External, 0, stakeKeyPath), - createGroupedAddress(address2, ownRewardAccount, AddressType.External, 1, stakeKeyPath) + createGroupedAddress({ + address: address1, + index: 0, + purpose: KeyPurpose.STANDARD, + rewardAccount: ownRewardAccount, + stakeKeyDerivationPath: stakeKeyPath, + type: AddressType.External + }), + createGroupedAddress({ + address: address2, + index: 1, + purpose: KeyPurpose.STANDARD, + rewardAccount: ownRewardAccount, + stakeKeyDerivationPath: stakeKeyPath, + type: AddressType.External + }) ], + purpose: KeyPurpose.STANDARD, sender: undefined, txInKeyPathMap: createTxInKeyPathMapMock([ - createGroupedAddress(address1, ownRewardAccount, AddressType.External, 0, stakeKeyPath), - createGroupedAddress(address2, ownRewardAccount, AddressType.External, 1, stakeKeyPath) + createGroupedAddress({ + address: address1, + index: 0, + purpose: KeyPurpose.STANDARD, + rewardAccount: ownRewardAccount, + stakeKeyDerivationPath: stakeKeyPath, + type: AddressType.External + }), + createGroupedAddress({ + address: address2, + index: 1, + purpose: KeyPurpose.STANDARD, + rewardAccount: ownRewardAccount, + stakeKeyDerivationPath: stakeKeyPath, + type: AddressType.External + }) ]) }; @@ -140,7 +173,7 @@ describe('certificates', () => { params: { stakeCredential: { keyPath: [ - util.harden(CardanoKeyConst.PURPOSE), + util.harden(KeyPurpose.STANDARD), util.harden(CardanoKeyConst.COIN_TYPE), util.harden(0), KeyRole.Stake, diff --git a/packages/hardware-ledger/test/transformers/collateralInputs.test.ts b/packages/hardware-ledger/test/transformers/collateralInputs.test.ts index 54ceff38e9a..b19d8f146e3 100644 --- a/packages/hardware-ledger/test/transformers/collateralInputs.test.ts +++ b/packages/hardware-ledger/test/transformers/collateralInputs.test.ts @@ -1,5 +1,5 @@ import { CONTEXT_WITH_KNOWN_ADDRESSES, txIn } from '../testData'; -import { CardanoKeyConst, TxInId, util } from '@cardano-sdk/key-management'; +import { CardanoKeyConst, KeyPurpose, TxInId, util } from '@cardano-sdk/key-management'; import { mapCollateralTxIns } from '../../src/transformers'; describe('collateralInputs', () => { @@ -12,6 +12,7 @@ describe('collateralInputs', () => { it('can map a a set of collateral inputs', async () => { const keyPath = { index: 0, + purpose: KeyPurpose.STANDARD, role: 1 }; const txIns = mapCollateralTxIns([txIn, txIn, txIn], { diff --git a/packages/hardware-ledger/test/transformers/txIn.test.ts b/packages/hardware-ledger/test/transformers/txIn.test.ts index 0d9fbc58449..d1b053430dc 100644 --- a/packages/hardware-ledger/test/transformers/txIn.test.ts +++ b/packages/hardware-ledger/test/transformers/txIn.test.ts @@ -1,9 +1,9 @@ import { CONTEXT_WITHOUT_KNOWN_ADDRESSES, CONTEXT_WITH_KNOWN_ADDRESSES, txIn } from '../testData'; -import { CardanoKeyConst, TxInId, util } from '@cardano-sdk/key-management'; -import { mapTxIns, toTxIn } from '../../src/transformers'; +import { CardanoKeyConst, KeyPurpose, TxInId, util } from '@cardano-sdk/key-management'; +import { mapTxIns, toTxIn } from '../../src'; describe('txIn', () => { - const paymentKeyPath = { index: 0, role: 1 }; + const paymentKeyPath = { index: 0, purpose: KeyPurpose.STANDARD, role: 1 }; describe('mapTxIns', () => { it('can map a a set of TxIns', async () => { @@ -52,7 +52,7 @@ describe('txIn', () => { expect(ledgerTxIn).toEqual({ outputIndex: txIn.index, path: [ - util.harden(CardanoKeyConst.PURPOSE), + util.harden(KeyPurpose.STANDARD), util.harden(CardanoKeyConst.COIN_TYPE), util.harden(CONTEXT_WITH_KNOWN_ADDRESSES.accountIndex), paymentKeyPath.role, diff --git a/packages/hardware-trezor/src/transformers/additionalWitnessRequests.ts b/packages/hardware-trezor/src/transformers/additionalWitnessRequests.ts index 48c117da340..9908f53e745 100644 --- a/packages/hardware-trezor/src/transformers/additionalWitnessRequests.ts +++ b/packages/hardware-trezor/src/transformers/additionalWitnessRequests.ts @@ -22,7 +22,10 @@ export const mapAdditionalWitnessRequests: Transform< const additionalWitnessPaths: Trezor.DerivationPath[] = [...paymentKeyPaths]; if (context?.knownAddresses?.length) { - const stakeKeyPath = util.stakeKeyPathFromGroupedAddress({ address: context.knownAddresses[0] }); + const stakeKeyPath = util.stakeKeyPathFromGroupedAddress({ + address: context.knownAddresses[0], + purpose: context.purpose + }); if (stakeKeyPath) additionalWitnessPaths.push(stakeKeyPath); } return additionalWitnessPaths; diff --git a/packages/hardware-trezor/src/transformers/certificates.ts b/packages/hardware-trezor/src/transformers/certificates.ts index 29d8ad60736..331e3c05766 100644 --- a/packages/hardware-trezor/src/transformers/certificates.ts +++ b/packages/hardware-trezor/src/transformers/certificates.ts @@ -2,7 +2,7 @@ import * as Crypto from '@cardano-sdk/crypto'; import * as Trezor from '@trezor/connect'; import { BIP32Path } from '@cardano-sdk/crypto'; import { Cardano } from '@cardano-sdk/core'; -import { GroupedAddress, util } from '@cardano-sdk/key-management'; +import { GroupedAddress, KeyPurpose, util } from '@cardano-sdk/key-management'; import { InvalidArgumentError, Transform, @@ -19,7 +19,8 @@ type CertCredentialsType = { const getCertCredentials = ( stakeKeyHash: Crypto.Ed25519KeyHashHex, - knownAddresses: GroupedAddress[] | undefined + knownAddresses: GroupedAddress[] | undefined, + purpose: KeyPurpose | undefined ): CertCredentialsType => { const knownAddress = knownAddresses?.find((address) => areStringsEqualInConstantTime(Cardano.RewardAccount.toHash(address.rewardAccount), stakeKeyHash) @@ -30,7 +31,7 @@ const getCertCredentials = ( !!rewardAddress && areNumbersEqualInConstantTime(rewardAddress?.getPaymentCredential().type, Cardano.CredentialType.KeyHash) ) { - const path = util.stakeKeyPathFromGroupedAddress({ address: knownAddress }); + const path = util.stakeKeyPathFromGroupedAddress({ address: knownAddress, purpose }); return path ? { path } : { keyHash: stakeKeyHash }; } return { @@ -48,7 +49,7 @@ const getPoolOperatorKeyPath = ( context: TrezorTxTransformerContext ): BIP32Path | null => { const knownAddress = context?.knownAddresses.find((address) => address.rewardAccount === operator); - return util.stakeKeyPathFromGroupedAddress({ address: knownAddress }); + return util.stakeKeyPathFromGroupedAddress({ address: knownAddress, purpose: context.purpose }); }; export const getStakeAddressCertificate: Transform< @@ -58,7 +59,8 @@ export const getStakeAddressCertificate: Transform< > = (certificate, context) => { const credentials = getCertCredentials( certificate.stakeCredential.hash as unknown as Crypto.Ed25519KeyHashHex, - context?.knownAddresses + context?.knownAddresses, + context?.purpose ); const certificateType = certificate.__typename === Cardano.CertificateType.StakeRegistration @@ -82,7 +84,8 @@ export const getStakeDelegationCertificate: Transform< const poolIdKeyHash = Cardano.PoolId.toKeyHash(certificate.poolId); const credentials = getCertCredentials( certificate.stakeCredential.hash as unknown as Crypto.Ed25519KeyHashHex, - context?.knownAddresses + context?.knownAddresses, + context?.purpose ); return { keyHash: credentials.keyHash, diff --git a/packages/hardware-trezor/src/transformers/keyPaths.ts b/packages/hardware-trezor/src/transformers/keyPaths.ts index e18ea8904e9..4e123ce1eb8 100644 --- a/packages/hardware-trezor/src/transformers/keyPaths.ts +++ b/packages/hardware-trezor/src/transformers/keyPaths.ts @@ -1,6 +1,6 @@ import { BIP32Path } from '@cardano-sdk/crypto'; import { Cardano } from '@cardano-sdk/core'; -import { GroupedAddress, TxInId, util } from '@cardano-sdk/key-management'; +import { GroupedAddress, KeyPurpose, TxInId, util } from '@cardano-sdk/key-management'; import { TrezorTxTransformerContext } from '../types'; /** Uses the given Trezor input resolver to resolve the payment key path for known addresses for given input transaction. */ @@ -17,10 +17,14 @@ export const resolvePaymentKeyPathForTxIn = ( export const resolveStakeKeyPath = ( rewardAddress: Cardano.RewardAddress, - knownAddresses: GroupedAddress[] + knownAddresses: GroupedAddress[], + purpose: KeyPurpose ): BIP32Path | null => { const knownAddress = knownAddresses.find( ({ rewardAccount }) => rewardAccount === rewardAddress.toAddress().toBech32() ); - return util.stakeKeyPathFromGroupedAddress({ address: knownAddress }); + return util.stakeKeyPathFromGroupedAddress({ + address: knownAddress, + purpose + }); }; diff --git a/packages/hardware-trezor/src/transformers/withdrawals.ts b/packages/hardware-trezor/src/transformers/withdrawals.ts index 91e4b986f23..c886763847b 100644 --- a/packages/hardware-trezor/src/transformers/withdrawals.ts +++ b/packages/hardware-trezor/src/transformers/withdrawals.ts @@ -22,7 +22,9 @@ export const toTrezorWithdrawal: Transform { it('should include payment key paths and reward account key path from given inputs', async () => { - const paymentKeyPath = { index: 0, role: 1 }; + const paymentKeyPath = { index: 0, purpose: KeyPurpose.STANDARD, role: 1 }; const result = mapAdditionalWitnessRequests([txIn], { ...contextWithKnownAddresses, txInKeyPathMap: { [TxInId(txIn)]: paymentKeyPath } diff --git a/packages/hardware-trezor/test/transformers/keyPaths.test.ts b/packages/hardware-trezor/test/transformers/keyPaths.test.ts index 8c7efc9ffd4..3d04a7deabd 100644 --- a/packages/hardware-trezor/test/transformers/keyPaths.test.ts +++ b/packages/hardware-trezor/test/transformers/keyPaths.test.ts @@ -22,9 +22,9 @@ describe('key-paths', () => { }); describe('resolveStakeKeyPath', () => { it('returns the stake key path for a known address', async () => { - expect(resolveStakeKeyPath(rewardAddress, contextWithKnownAddresses.knownAddresses)).toEqual( - knownAddressStakeKeyPath - ); + expect( + resolveStakeKeyPath(rewardAddress, contextWithKnownAddresses.knownAddresses, contextWithKnownAddresses.purpose) + ).toEqual(knownAddressStakeKeyPath); }); }); }); diff --git a/packages/hardware-trezor/test/transformers/tx.test.ts b/packages/hardware-trezor/test/transformers/tx.test.ts index c8e2688e4c6..bdfcc453c92 100644 --- a/packages/hardware-trezor/test/transformers/tx.test.ts +++ b/packages/hardware-trezor/test/transformers/tx.test.ts @@ -1,5 +1,5 @@ import * as Trezor from '@trezor/connect'; -import { CardanoKeyConst, TxInId, util } from '@cardano-sdk/key-management'; +import { CardanoKeyConst, KeyPurpose, TxInId, util } from '@cardano-sdk/key-management'; import { babbageTxBodyWithScripts, contextWithKnownAddresses, @@ -56,7 +56,7 @@ describe('tx', () => { }, certificates: [ { - path: [util.harden(CardanoKeyConst.PURPOSE), util.harden(CardanoKeyConst.COIN_TYPE), util.harden(0), 2, 0], + path: [util.harden(KeyPurpose.STANDARD), util.harden(CardanoKeyConst.COIN_TYPE), util.harden(0), 2, 0], pool: '153806dbcd134ddee69a8c5204e38ac80448f62342f8c23cfe4b7edf', type: Trezor.PROTO.CardanoCertificateType.STAKE_DELEGATION } diff --git a/packages/input-selection/test/ChangeAddress/StaticChangeAddressResolver.test.ts b/packages/input-selection/test/ChangeAddress/StaticChangeAddressResolver.test.ts index 2c800c5eb8f..324d26cf003 100644 --- a/packages/input-selection/test/ChangeAddress/StaticChangeAddressResolver.test.ts +++ b/packages/input-selection/test/ChangeAddress/StaticChangeAddressResolver.test.ts @@ -1,4 +1,4 @@ -import { AddressType, GroupedAddress, KeyRole } from '@cardano-sdk/key-management'; +import { AddressType, GroupedAddress, KeyPurpose, KeyRole } from '@cardano-sdk/key-management'; import { Cardano } from '@cardano-sdk/core'; import { InvalidStateError } from '@cardano-sdk/util'; import { StaticChangeAddressResolver } from '../../src'; @@ -10,8 +10,9 @@ export const knownAddresses = () => address: 'testAddress' as Cardano.PaymentAddress, index: 0, networkId: Cardano.NetworkId.Testnet, + purpose: KeyPurpose.STANDARD, rewardAccount: '' as Cardano.RewardAccount, - stakeKeyDerivationPath: { index: 0, role: KeyRole.Stake }, + stakeKeyDerivationPath: { index: 0, purpose: KeyPurpose.STANDARD, role: KeyRole.Stake }, type: AddressType.External } ]); diff --git a/packages/key-management/test/Bip32Account.test.ts b/packages/key-management/test/Bip32Account.test.ts index a576f5dd1c3..4e7771e31ae 100644 --- a/packages/key-management/test/Bip32Account.test.ts +++ b/packages/key-management/test/Bip32Account.test.ts @@ -96,7 +96,11 @@ describe('Bip32Account', () => { expect(internalAddress.type).toBe(paymentKeyDerivationPath.type); expect(internalAddress.networkId).toBe(testnetChainId.networkId); expect(internalAddress.rewardAccount.startsWith('stake_test')).toBe(true); - expect(internalAddress.stakeKeyDerivationPath).toEqual({ index: stakeKeyDerivationIndex, role: 2 }); + expect(internalAddress.stakeKeyDerivationPath).toEqual({ + index: stakeKeyDerivationIndex, + purpose: KeyPurpose.STANDARD, + role: 2 + }); }); it('derives the address with stake key of the given index', async () => { @@ -121,25 +125,41 @@ describe('Bip32Account', () => { await testnetAccount.deriveAddress({ index, type }, 3) ]; - expect(addresses[0].stakeKeyDerivationPath).toEqual({ index: 0, role: KeyRole.Stake }); + expect(addresses[0].stakeKeyDerivationPath).toEqual({ + index: 0, + purpose: KeyPurpose.STANDARD, + role: KeyRole.Stake + }); expect(addresses[0].rewardAccount).toEqual('stake_test1uruaegs6djpxaj9vkn8njh9uys63jdaluetqkf5r4w95zhc8cxn3h'); expect(addresses[0].address).toEqual( 'addr_test1qruaegs6djpxaj9vkn8njh9uys63jdaluetqkf5r4w95zhlemj3p5myzdmy2edx089wtcfp4rymmlejkpvng82utg90s4cadlm' ); - expect(addresses[1].stakeKeyDerivationPath).toEqual({ index: 1, role: KeyRole.Stake }); + expect(addresses[1].stakeKeyDerivationPath).toEqual({ + index: 1, + purpose: KeyPurpose.STANDARD, + role: KeyRole.Stake + }); expect(addresses[1].rewardAccount).toEqual('stake_test1uzx0qqs06evy77cnpk6u5q3fc50exjpp5t4s0swl2ykc4jsmh8tej'); expect(addresses[1].address).toEqual( 'addr_test1qruaegs6djpxaj9vkn8njh9uys63jdaluetqkf5r4w95zhuv7qpql4jcfaa3xrd4egpzn3gljdyzrghtqlqa75fd3t9qqnvgeq' ); - expect(addresses[2].stakeKeyDerivationPath).toEqual({ index: 2, role: KeyRole.Stake }); + expect(addresses[2].stakeKeyDerivationPath).toEqual({ + index: 2, + purpose: KeyPurpose.STANDARD, + role: KeyRole.Stake + }); expect(addresses[2].rewardAccount).toEqual('stake_test1uqcnxxxatdgmqdmz0rhg72kn3n0egek5s0nqcvfy9ztyltc9cpuz4'); expect(addresses[2].address).toEqual( 'addr_test1qruaegs6djpxaj9vkn8njh9uys63jdaluetqkf5r4w95zhe3xvvd6k63kqmky78w3u4d8rxlj3ndfqlxpscjg2ykf7hs8qc48l' ); - expect(addresses[3].stakeKeyDerivationPath).toEqual({ index: 3, role: KeyRole.Stake }); + expect(addresses[3].stakeKeyDerivationPath).toEqual({ + index: 3, + purpose: KeyPurpose.STANDARD, + role: KeyRole.Stake + }); expect(addresses[3].rewardAccount).toEqual('stake_test1urj8hvwxxz0t6pnfttj9ne5leu74shjlg83a8kxww9ft2fqtdhssu'); expect(addresses[3].address).toEqual( 'addr_test1qruaegs6djpxaj9vkn8njh9uys63jdaluetqkf5r4w95zhly0wcuvvy7h5rxjkhyt8nflneatp097s0r60vvuu2jk5jq73efq0' diff --git a/packages/key-management/test/InMemoryKeyAgent.test.ts b/packages/key-management/test/InMemoryKeyAgent.test.ts index 24139decae2..62eb708a9e1 100644 --- a/packages/key-management/test/InMemoryKeyAgent.test.ts +++ b/packages/key-management/test/InMemoryKeyAgent.test.ts @@ -28,7 +28,8 @@ describe('InMemoryKeyAgent', () => { { chainId: Cardano.ChainIds.Preview, getPassphrase, - mnemonicWords + mnemonicWords, + purpose: KeyPurpose.STANDARD }, { bip32Ed25519, logger: dummyLogger } ); @@ -54,7 +55,8 @@ describe('InMemoryKeyAgent', () => { chainId: Cardano.ChainIds.Preview, getPassphrase, mnemonic2ndFactorPassphrase: 'passphrase', - mnemonicWords + mnemonicWords, + purpose: KeyPurpose.STANDARD }, { bip32Ed25519, logger: dummyLogger } ); @@ -108,6 +110,7 @@ describe('InMemoryKeyAgent', () => { }, { knownAddresses, + purpose: KeyPurpose.STANDARD, txInKeyPathMap } ); @@ -173,7 +176,8 @@ describe('InMemoryKeyAgent', () => { rootPrivateKey: Crypto.Bip32PrivateKeyHex(yoroiRootPrivateKeyHex) }) ), - getPassphrase + getPassphrase, + purpose: KeyPurpose.STANDARD }, { bip32Ed25519, logger: dummyLogger } ); @@ -186,7 +190,8 @@ describe('InMemoryKeyAgent', () => { { chainId: Cardano.ChainIds.Preview, getPassphrase, - mnemonicWords: yoroiMnemonic + mnemonicWords: yoroiMnemonic, + purpose: KeyPurpose.STANDARD }, { bip32Ed25519, logger: dummyLogger } ); @@ -204,7 +209,8 @@ describe('InMemoryKeyAgent', () => { { chainId: Cardano.ChainIds.Preview, getPassphrase, - mnemonicWords: michaelMnemonic + mnemonicWords: michaelMnemonic, + purpose: KeyPurpose.STANDARD }, { bip32Ed25519, logger: dummyLogger } ); @@ -215,7 +221,7 @@ describe('InMemoryKeyAgent', () => { body: { fee: 10n, inputs: [], outputs: [], validityInterval: {} }, hash: Cardano.TransactionId('0000000000000000000000000000000000000000000000000000000000000000') }, - { knownAddresses: [], txInKeyPathMap: {} } + { knownAddresses: [], purpose: KeyPurpose.STANDARD, txInKeyPathMap: {} } ); expect( signature.has(Crypto.Ed25519PublicKeyHex('0b1c96fad4179d7910bd9485ac28c4c11368c83d18d01b29d4cf84d8ff6a06c4')) @@ -272,7 +278,8 @@ describe('InMemoryKeyAgent', () => { }) ), // daedelus enforces min length of 10 - getPassphrase: jest.fn().mockResolvedValue(Buffer.from('nMmys*X002')) + getPassphrase: jest.fn().mockResolvedValue(Buffer.from('nMmys*X002')), + purpose: KeyPurpose.STANDARD }, { bip32Ed25519, logger: dummyLogger } ); @@ -291,7 +298,8 @@ describe('InMemoryKeyAgent', () => { { chainId: Cardano.ChainIds.Preview, getPassphrase, - mnemonicWords: daedelusMnemonic24 + mnemonicWords: daedelusMnemonic24, + purpose: KeyPurpose.STANDARD }, { bip32Ed25519, logger: dummyLogger } ); diff --git a/packages/key-management/test/KeyAgentBase.test.ts b/packages/key-management/test/KeyAgentBase.test.ts index 01f72205ec6..20786db00a6 100644 --- a/packages/key-management/test/KeyAgentBase.test.ts +++ b/packages/key-management/test/KeyAgentBase.test.ts @@ -1,5 +1,5 @@ import * as Crypto from '@cardano-sdk/crypto'; -import { AddressType, KeyAgentBase, KeyAgentType, KeyRole, SerializableInMemoryKeyAgentData } from '../src'; +import { AddressType, KeyAgentBase, KeyAgentType, KeyPurpose, KeyRole, SerializableInMemoryKeyAgentData } from '../src'; import { Cardano } from '@cardano-sdk/core'; import { dummyLogger } from 'ts-log'; @@ -34,7 +34,8 @@ describe('KeyAgentBase', () => { extendedAccountPublicKey: Crypto.Bip32PublicKeyHex( // eslint-disable-next-line max-len 'fc5ab25e830b67c47d0a17411bf7fdabf711a597fb6cf04102734b0a2934ceaaa65ff5e7c52498d52c07b8ddfcd436fc2b4d2775e2984a49d0c79f65ceee4779' - ) + ), + purpose: KeyPurpose.STANDARD }); }); @@ -52,11 +53,23 @@ describe('KeyAgentBase', () => { }); test('derivePublicKey', async () => { - const externalPublicKey = await keyAgent.derivePublicKey({ index: 1, role: KeyRole.External }); + const externalPublicKey = await keyAgent.derivePublicKey({ + index: 1, + purpose: KeyPurpose.STANDARD, + role: KeyRole.External + }); expect(typeof externalPublicKey).toBe('string'); - const stakePublicKey = await keyAgent.derivePublicKey({ index: 1, role: KeyRole.Stake }); + const stakePublicKey = await keyAgent.derivePublicKey({ + index: 1, + purpose: KeyPurpose.STANDARD, + role: KeyRole.Stake + }); expect(typeof stakePublicKey).toBe('string'); - const dRepPublicKey = await keyAgent.derivePublicKey({ index: 0, role: KeyRole.DRep }); + const dRepPublicKey = await keyAgent.derivePublicKey({ + index: 0, + purpose: KeyPurpose.STANDARD, + role: KeyRole.DRep + }); expect(typeof dRepPublicKey).toBe('string'); }); }); diff --git a/packages/key-management/test/cip8/cip30signData.test.ts b/packages/key-management/test/cip8/cip30signData.test.ts index 63bfb227fc2..ef6a9d81a28 100644 --- a/packages/key-management/test/cip8/cip30signData.test.ts +++ b/packages/key-management/test/cip8/cip30signData.test.ts @@ -5,6 +5,7 @@ import { GroupedAddress, KeyAgent, util as KeyManagementUtil, + KeyPurpose, KeyRole, MessageSender, cip8 @@ -77,6 +78,7 @@ describe('cip30signData', () => { expect(publicKeyHex).toEqual( await keyAgent.derivePublicKey({ index: addressDerivationPath.index, + purpose: KeyPurpose.STANDARD, role: addressDerivationPath.type as number }) ); @@ -91,6 +93,7 @@ describe('cip30signData', () => { expect(publicKeyHex).toEqual( await keyAgent.derivePublicKey({ index: 0, + purpose: KeyPurpose.STANDARD, role: KeyRole.Stake }) ); diff --git a/packages/key-management/test/mocks/stakeKey.ts b/packages/key-management/test/mocks/stakeKey.ts index ca62fa12688..ee3e1556f36 100644 --- a/packages/key-management/test/mocks/stakeKey.ts +++ b/packages/key-management/test/mocks/stakeKey.ts @@ -1,6 +1,7 @@ -import { KeyRole } from '../../src'; +import { KeyPurpose, KeyRole } from '../../src'; export const stakeKeyDerivationPath = { index: 0, + purpose: KeyPurpose.STANDARD, role: KeyRole.Stake }; diff --git a/packages/key-management/test/util/createWitnesser.test.ts b/packages/key-management/test/util/createWitnesser.test.ts index c4ec60b3c2d..297de1a60c0 100644 --- a/packages/key-management/test/util/createWitnesser.test.ts +++ b/packages/key-management/test/util/createWitnesser.test.ts @@ -1,4 +1,4 @@ -import { AsyncKeyAgent, SignBlobResult, Witnesser, util } from '../../src'; +import { AsyncKeyAgent, KeyPurpose, SignBlobResult, Witnesser, util } from '../../src'; import { Cardano, Serialization } from '@cardano-sdk/core'; import { HexBlob } from '@cardano-sdk/util'; @@ -15,7 +15,7 @@ describe('createBip32Ed25519Witnesser', () => { }); it('signBlob is unchanged', async () => { - const keyDerivationPath = { index: 0, role: 0 }; + const keyDerivationPath = { index: 0, purpose: KeyPurpose.STANDARD, role: 0 }; const blob = HexBlob('abc123'); const result = {} as SignBlobResult; asyncKeyAgent.signBlob.mockResolvedValueOnce(result); @@ -39,7 +39,7 @@ describe('createBip32Ed25519Witnesser', () => { hash: Cardano.TransactionId('3643bb5fe745ba0532977f82ecf54699963c97adef2626f7c780225d218e9ba6') }; - const options = { knownAddresses: [], txInKeyPathMap: {} }; + const options = { knownAddresses: [], purpose: KeyPurpose.STANDARD, txInKeyPathMap: {} }; const result = new Map(); asyncKeyAgent.signTransaction.mockResolvedValueOnce(result); diff --git a/packages/key-management/test/util/ensureStakeKeys.test.ts b/packages/key-management/test/util/ensureStakeKeys.test.ts index 7d74f8b8c57..7f6667241cd 100644 --- a/packages/key-management/test/util/ensureStakeKeys.test.ts +++ b/packages/key-management/test/util/ensureStakeKeys.test.ts @@ -1,4 +1,4 @@ -import { AddressType, Bip32Account, util } from '../../src'; +import { AddressType, Bip32Account, KeyPurpose, util } from '../../src'; import { Bip32PublicKeyHex } from '@cardano-sdk/crypto'; import { Cardano } from '@cardano-sdk/core'; import { Logger, dummyLogger } from 'ts-log'; @@ -14,7 +14,8 @@ describe('ensureStakeKeys', () => { chainId: Cardano.ChainIds.Preview, extendedAccountPublicKey: Bip32PublicKeyHex( 'fc5ab25e830b67c47d0a17411bf7fdabf711a597fb6cf04102734b0a2934ceaaa65ff5e7c52498d52c07b8ddfcd436fc2b4d2775e2984a49d0c79f65ceee4779' - ) + ), + purpose: KeyPurpose.STANDARD }); }); diff --git a/packages/key-management/test/util/key.test.ts b/packages/key-management/test/util/key.test.ts index 489c6ed8ca1..8fdbedf9bb9 100644 --- a/packages/key-management/test/util/key.test.ts +++ b/packages/key-management/test/util/key.test.ts @@ -1,4 +1,4 @@ -import { AddressType, GroupedAddress, KeyRole } from '../../src'; +import { AddressType, GroupedAddress, KeyPurpose, KeyRole } from '../../src'; import { Cardano } from '@cardano-sdk/core'; import { paymentKeyPathFromGroupedAddress, stakeKeyPathFromGroupedAddress } from '../../src/util'; @@ -12,6 +12,7 @@ export const rewardAccount = Cardano.RewardAccount(rewardKey); const stakeKeyDerivationPath = { index: 0, + purpose: KeyPurpose.STANDARD, role: KeyRole.Stake }; @@ -20,6 +21,7 @@ const knownAddress: GroupedAddress = { address: paymentAddress, index: 0, networkId: Cardano.NetworkId.Testnet, + purpose: KeyPurpose.STANDARD, rewardAccount, stakeKeyDerivationPath, type: AddressType.Internal @@ -31,17 +33,23 @@ const knownAddressStakeKeyPath = [2_147_485_500, 2_147_485_463, 2_147_483_648, 2 describe('key utils', () => { describe('paymentKeyPathFromGroupedAddress', () => { it('returns a hardened BIP32 payment key path', () => { - expect(paymentKeyPathFromGroupedAddress({ address: knownAddress })).toEqual(knownAddressKeyPath); + expect(paymentKeyPathFromGroupedAddress({ address: knownAddress, purpose: KeyPurpose.STANDARD })).toEqual( + knownAddressKeyPath + ); }); }); describe('stakeKeyPathFromGroupedAddress', () => { it('returns null when given an undefined stakeKeyDerivationPath', async () => { const knownAddressClone = { ...knownAddress }; delete knownAddressClone.stakeKeyDerivationPath; - expect(stakeKeyPathFromGroupedAddress({ address: knownAddressClone })).toEqual(null); + expect(stakeKeyPathFromGroupedAddress({ address: knownAddressClone, purpose: KeyPurpose.STANDARD })).toEqual( + null + ); }); it('returns a hardened BIP32 stake key path', () => { - expect(stakeKeyPathFromGroupedAddress({ address: knownAddress })).toEqual(knownAddressStakeKeyPath); + expect(stakeKeyPathFromGroupedAddress({ address: knownAddress, purpose: KeyPurpose.STANDARD })).toEqual( + knownAddressStakeKeyPath + ); }); }); }); diff --git a/packages/key-management/test/util/ownSignaturePaths.test.ts b/packages/key-management/test/util/ownSignaturePaths.test.ts index 55f4b8b3b22..69017a30778 100644 --- a/packages/key-management/test/util/ownSignaturePaths.test.ts +++ b/packages/key-management/test/util/ownSignaturePaths.test.ts @@ -1,10 +1,11 @@ /* eslint-disable sonarjs/no-duplicate-string */ import * as Crypto from '@cardano-sdk/crypto'; -import { AccountKeyDerivationPath, AddressType, GroupedAddress, KeyRole, TxInId, util } from '../../src'; +import { AddressType, GroupedAddress, KeyPurpose, KeyRole, TxInId, util } from '../../src'; import { Cardano } from '@cardano-sdk/core'; export const stakeKeyPath = { index: 0, + purpose: KeyPurpose.STANDARD, role: KeyRole.Stake }; @@ -15,17 +16,18 @@ const toStakeCredential = (stakeKeyHash: Crypto.Ed25519KeyHashHex): Cardano.Cred type: Cardano.CredentialType.KeyHash }); -const createGroupedAddress = ( - address: Cardano.PaymentAddress, - rewardAccount: Cardano.RewardAccount, - type: AddressType, - index: number, - stakeKeyDerivationPath: AccountKeyDerivationPath - // eslint-disable-next-line max-params -): GroupedAddress => +const createGroupedAddress = ({ + address, + index, + rewardAccount, + stakeKeyDerivationPath, + type, + purpose +}: Omit): GroupedAddress => ({ address, index, + purpose, rewardAccount, stakeKeyDerivationPath, type @@ -57,7 +59,15 @@ describe('KeyManagement.util.ownSignaturePaths', () => { let dRepKeyHash: Crypto.Ed25519KeyHashHex; const foreignDRepKeyHash = Crypto.Hash28ByteBase16('8293d319ef5b3ac72366dd28006bd315b715f7e7cfcbd3004129b80d'); - const knownAddress1 = createGroupedAddress(address1, ownRewardAccount, AddressType.External, 0, stakeKeyPath); + // address1, ownRewardAccount, AddressType.External, 0, stakeKeyPath + const knownAddress1 = createGroupedAddress({ + address: address1, + index: 0, + purpose: KeyPurpose.STANDARD, + rewardAccount: ownRewardAccount, + stakeKeyDerivationPath: stakeKeyPath, + type: AddressType.External + }); beforeEach(async () => { dRepPublicKey = Crypto.Ed25519PublicKeyHex('deeb8f82f2af5836ebbc1b450b6dbf0b03c93afe5696f10d49e8a8304ebfac01'); @@ -72,22 +82,43 @@ describe('KeyManagement.util.ownSignaturePaths', () => { ]; const txBody = { inputs } as Cardano.TxBody; const knownAddresses = [address1, address2].map((address, index) => - createGroupedAddress(address, ownRewardAccount, AddressType.External, index, stakeKeyPath) + createGroupedAddress({ + address, + index, + purpose: KeyPurpose.STANDARD, + rewardAccount: ownRewardAccount, + stakeKeyDerivationPath: stakeKeyPath, + type: AddressType.External + }) ); expect( util.ownSignatureKeyPaths(txBody, knownAddresses, { - [TxInId(inputs[0])]: { index: knownAddresses[0].index, role: Number(knownAddresses[0].type) }, - [TxInId(inputs[1])]: { index: knownAddresses[1].index, role: Number(knownAddresses[1].type) }, - [TxInId(inputs[2])]: { index: knownAddresses[0].index, role: Number(knownAddresses[0].type) } + [TxInId(inputs[0])]: { + index: knownAddresses[0].index, + purpose: Number(knownAddresses[0].purpose), + role: Number(knownAddresses[0].type) + }, + [TxInId(inputs[1])]: { + index: knownAddresses[1].index, + purpose: Number(knownAddresses[0].purpose), + role: Number(knownAddresses[1].type) + }, + [TxInId(inputs[2])]: { + index: knownAddresses[0].index, + purpose: Number(knownAddresses[0].purpose), + role: Number(knownAddresses[0].type) + } }) ).toEqual([ { index: 0, + purpose: KeyPurpose.STANDARD, role: KeyRole.External }, { index: 1, + purpose: KeyPurpose.STANDARD, role: KeyRole.External } ]); @@ -123,6 +154,7 @@ describe('KeyManagement.util.ownSignaturePaths', () => { expect(util.ownSignatureKeyPaths(txBody, [knownAddress1], {})).toEqual([ { index: 0, + purpose: KeyPurpose.STANDARD, role: KeyRole.Stake } ]); @@ -143,6 +175,7 @@ describe('KeyManagement.util.ownSignaturePaths', () => { expect(util.ownSignatureKeyPaths(txBody, [knownAddress1], {})).toEqual([ { index: 0, + purpose: KeyPurpose.STANDARD, role: KeyRole.Stake } ]); @@ -161,6 +194,7 @@ describe('KeyManagement.util.ownSignaturePaths', () => { expect(util.ownSignatureKeyPaths(txBody, [knownAddress1], {})).toEqual([ { index: 0, + purpose: KeyPurpose.STANDARD, role: KeyRole.Stake } ]); @@ -180,6 +214,7 @@ describe('KeyManagement.util.ownSignaturePaths', () => { expect(util.ownSignatureKeyPaths(txBody, [knownAddress1], {})).toEqual([ { index: 0, + purpose: KeyPurpose.STANDARD, role: KeyRole.Stake } ]); @@ -203,6 +238,7 @@ describe('KeyManagement.util.ownSignaturePaths', () => { expect(util.ownSignatureKeyPaths(txBody, [knownAddress1], {})).toEqual([ { index: 0, + purpose: KeyPurpose.STANDARD, role: KeyRole.Stake } ]); @@ -243,6 +279,7 @@ describe('KeyManagement.util.ownSignaturePaths', () => { expect(util.ownSignatureKeyPaths(txBody, [knownAddress1], {})).toEqual([ { index: 0, + purpose: KeyPurpose.STANDARD, role: KeyRole.Stake } ]); @@ -264,6 +301,7 @@ describe('KeyManagement.util.ownSignaturePaths', () => { expect(util.ownSignatureKeyPaths(txBody, [knownAddress1], {})).toEqual([ { index: 0, + purpose: KeyPurpose.STANDARD, role: KeyRole.Stake } ]); @@ -337,10 +375,20 @@ describe('KeyManagement.util.ownSignaturePaths', () => { inputs: [{}, {}, {}], withdrawals: [{ quantity: 1n, stakeAddress: ownRewardAccount }] } as Cardano.TxBody; - const knownAddresses = [createGroupedAddress(address1, ownRewardAccount, AddressType.External, 0, stakeKeyPath)]; + const knownAddresses = [ + createGroupedAddress({ + address: address1, + index: 0, + purpose: KeyPurpose.STANDARD, + rewardAccount: ownRewardAccount, + stakeKeyDerivationPath: stakeKeyPath, + type: AddressType.External + }) + ]; expect(util.ownSignatureKeyPaths(txBody, knownAddresses, {})).toEqual([ { index: 0, + purpose: KeyPurpose.STANDARD, role: KeyRole.Stake } ]); @@ -351,7 +399,16 @@ describe('KeyManagement.util.ownSignaturePaths', () => { inputs: [{}, {}, {}], withdrawals: [{ quantity: 1n, stakeAddress: otherRewardAccount }] } as Cardano.TxBody; - const knownAddresses = [createGroupedAddress(address1, ownRewardAccount, AddressType.External, 0, stakeKeyPath)]; + const knownAddresses = [ + createGroupedAddress({ + address: address1, + index: 0, + purpose: KeyPurpose.STANDARD, + rewardAccount: ownRewardAccount, + stakeKeyDerivationPath: stakeKeyPath, + type: AddressType.External + }) + ]; expect(util.ownSignatureKeyPaths(txBody, knownAddresses, {})).toEqual([]); }); @@ -366,12 +423,20 @@ describe('KeyManagement.util.ownSignaturePaths', () => { requiredExtraSignatures: [paymentHash] } as Cardano.TxBody; const knownAddresses = [ - createGroupedAddress(paymentAddress, ownRewardAccount, AddressType.External, 100, stakeKeyPath) + createGroupedAddress({ + address: paymentAddress, + index: 100, + purpose: KeyPurpose.STANDARD, + rewardAccount: ownRewardAccount, + stakeKeyDerivationPath: stakeKeyPath, + type: AddressType.External + }) ]; expect(util.ownSignatureKeyPaths(txBody, knownAddresses, {})).toEqual([ { index: 100, + purpose: KeyPurpose.STANDARD, role: KeyRole.External } ]); @@ -385,11 +450,21 @@ describe('KeyManagement.util.ownSignaturePaths', () => { inputs: [{}, {}, {}], requiredExtraSignatures: [stakeKeyHash] } as Cardano.TxBody; - const knownAddresses = [createGroupedAddress(address1, rewardAccount, AddressType.External, 0, stakeKeyPath)]; + const knownAddresses = [ + createGroupedAddress({ + address: address1, + index: 0, + purpose: KeyPurpose.STANDARD, + rewardAccount, + stakeKeyDerivationPath: stakeKeyPath, + type: AddressType.External + }) + ]; expect(util.ownSignatureKeyPaths(txBody, knownAddresses, {})).toEqual([ { index: 0, + purpose: KeyPurpose.STANDARD, role: KeyRole.Stake } ]); @@ -409,7 +484,18 @@ describe('KeyManagement.util.ownSignaturePaths', () => { // Using multiple stake keys with one payment key to have separate derivation paths per each certificate const knownAddresses = rewardAccounts.map(({ account }, index) => - createGroupedAddress(address1, account, AddressType.External, 0, { index, role: KeyRole.Stake }) + createGroupedAddress({ + address: address1, + index, + purpose: KeyPurpose.STANDARD, + rewardAccount: account, + stakeKeyDerivationPath: { + index, + purpose: KeyPurpose.STANDARD, + role: KeyRole.Stake + }, + type: AddressType.External + }) ); it('is returned for certificates with the wallet stake key hash', async () => { @@ -444,12 +530,12 @@ describe('KeyManagement.util.ownSignaturePaths', () => { } as Cardano.TxBody; expect(util.ownSignatureKeyPaths(txBody, knownAddresses, {})).toEqual([ - { index: 0, role: KeyRole.Stake }, - { index: 1, role: KeyRole.Stake }, - { index: 2, role: KeyRole.Stake }, - { index: 3, role: KeyRole.Stake }, - { index: 4, role: KeyRole.Stake }, - { index: 5, role: KeyRole.Stake } + { index: 0, purpose: KeyPurpose.STANDARD, role: KeyRole.Stake }, + { index: 1, purpose: KeyPurpose.STANDARD, role: KeyRole.Stake }, + { index: 2, purpose: KeyPurpose.STANDARD, role: KeyRole.Stake }, + { index: 3, purpose: KeyPurpose.STANDARD, role: KeyRole.Stake }, + { index: 4, purpose: KeyPurpose.STANDARD, role: KeyRole.Stake }, + { index: 5, purpose: KeyPurpose.STANDARD, role: KeyRole.Stake } ]); }); @@ -468,7 +554,9 @@ describe('KeyManagement.util.ownSignaturePaths', () => { inputs: [{}, {}, {}] } as Cardano.TxBody; - expect(util.ownSignatureKeyPaths(txBody, knownAddresses, {})).toEqual([{ index: 0, role: KeyRole.Stake }]); + expect(util.ownSignatureKeyPaths(txBody, knownAddresses, {})).toEqual([ + { index: 0, purpose: KeyPurpose.STANDARD, role: KeyRole.Stake } + ]); }); it('is returned for StakePool voter in voting procedures', async () => { @@ -490,7 +578,9 @@ describe('KeyManagement.util.ownSignaturePaths', () => { ] } as Cardano.TxBody; - expect(util.ownSignatureKeyPaths(txBody, knownAddresses, {})).toEqual([{ index: 3, role: KeyRole.Stake }]); + expect(util.ownSignatureKeyPaths(txBody, knownAddresses, {})).toEqual([ + { index: 3, purpose: KeyPurpose.STANDARD, role: KeyRole.Stake } + ]); }); }); @@ -511,7 +601,7 @@ describe('KeyManagement.util.ownSignaturePaths', () => { } as Cardano.TxBody; expect(util.ownSignatureKeyPaths(txBody, [knownAddress1], {}, dRepKeyHash)).toEqual([ - { index: 0, role: KeyRole.DRep } + { index: 0, purpose: KeyPurpose.STANDARD, role: KeyRole.DRep } ]); }); @@ -530,7 +620,7 @@ describe('KeyManagement.util.ownSignaturePaths', () => { } as Cardano.TxBody; expect(util.ownSignatureKeyPaths(txBody, [knownAddress1], {}, dRepKeyHash)).toEqual([ - { index: 0, role: KeyRole.DRep } + { index: 0, purpose: KeyPurpose.STANDARD, role: KeyRole.DRep } ]); }); @@ -550,7 +640,7 @@ describe('KeyManagement.util.ownSignaturePaths', () => { } as Cardano.TxBody; expect(util.ownSignatureKeyPaths(txBody, [knownAddress1], {}, dRepKeyHash)).toEqual([ - { index: 0, role: KeyRole.DRep } + { index: 0, purpose: KeyPurpose.STANDARD, role: KeyRole.DRep } ]); }); @@ -571,7 +661,7 @@ describe('KeyManagement.util.ownSignaturePaths', () => { } as Cardano.TxBody; expect(util.ownSignatureKeyPaths(txBody, [knownAddress1], {}, dRepKeyHash)).toEqual([ - { index: 0, role: KeyRole.DRep } + { index: 0, purpose: KeyPurpose.STANDARD, role: KeyRole.DRep } ]); }); }); diff --git a/packages/key-management/test/util/stubSignTransaction.test.ts b/packages/key-management/test/util/stubSignTransaction.test.ts index 95cd8c34900..4802c1e5b0f 100644 --- a/packages/key-management/test/util/stubSignTransaction.test.ts +++ b/packages/key-management/test/util/stubSignTransaction.test.ts @@ -1,7 +1,7 @@ import { Cardano } from '@cardano-sdk/core'; import { Ed25519PublicKey, Ed25519PublicKeyHex } from '@cardano-sdk/crypto'; -import { GroupedAddress, util } from '../../src'; +import { GroupedAddress, KeyPurpose, util } from '../../src'; jest.mock('../../src/util/ownSignatureKeyPaths'); const { ownSignatureKeyPaths } = jest.requireMock('../../src/util/ownSignatureKeyPaths'); @@ -17,7 +17,7 @@ describe('KeyManagement.util.stubSignTransaction', () => { expect( ( await util.stubSignTransaction({ - context: { dRepPublicKey, knownAddresses, txInKeyPathMap }, + context: { dRepPublicKey, knownAddresses, purpose: KeyPurpose.STANDARD, txInKeyPathMap }, txBody }) ).size @@ -25,7 +25,7 @@ describe('KeyManagement.util.stubSignTransaction', () => { expect( ( await util.stubSignTransaction({ - context: { dRepPublicKey, knownAddresses, txInKeyPathMap }, + context: { dRepPublicKey, knownAddresses, purpose: KeyPurpose.STANDARD, txInKeyPathMap }, txBody }) ).size diff --git a/packages/tx-construction/src/tx-builder/initializeTx.ts b/packages/tx-construction/src/tx-builder/initializeTx.ts index 0fb73be673f..3e315d9c6bd 100644 --- a/packages/tx-construction/src/tx-builder/initializeTx.ts +++ b/packages/tx-construction/src/tx-builder/initializeTx.ts @@ -2,8 +2,8 @@ import { Cardano, Serialization } from '@cardano-sdk/core'; import { GreedyTxEvaluator } from './GreedyTxEvaluator'; import { InitializeTxProps, InitializeTxResult } from '../types'; import { KeyPurpose, util } from '@cardano-sdk/key-management'; -import { StaticChangeAddressResolver, roundRobinRandomImprove } from '@cardano-sdk/input-selection'; import { RedeemersByType, defaultSelectionConstraints } from '../input-selection'; +import { StaticChangeAddressResolver, roundRobinRandomImprove } from '@cardano-sdk/input-selection'; import { TxBuilderDependencies } from './types'; import { createPreInputSelectionTxBody, includeChangeAndInputs } from '../createTransactionInternals'; import { ensureValidityInterval } from '../ensureValidityInterval'; diff --git a/packages/tx-construction/test/tx-builder/TxBuilder.bootstrap.test.ts b/packages/tx-construction/test/tx-builder/TxBuilder.bootstrap.test.ts index ef102c1569c..c90cd173a5d 100644 --- a/packages/tx-construction/test/tx-builder/TxBuilder.bootstrap.test.ts +++ b/packages/tx-construction/test/tx-builder/TxBuilder.bootstrap.test.ts @@ -1,4 +1,4 @@ -import { AddressType, Bip32Account, InMemoryKeyAgent, util } from '@cardano-sdk/key-management'; +import { AddressType, Bip32Account, InMemoryKeyAgent, KeyPurpose, util } from '@cardano-sdk/key-management'; import { Cardano } from '@cardano-sdk/core'; import { GenericTxBuilder, OutputValidation, TxBuilderProviders } from '../../src'; import { SodiumBip32Ed25519 } from '@cardano-sdk/crypto'; @@ -35,7 +35,8 @@ describe.each([ { chainId: Cardano.ChainIds.Preview, getPassphrase: async () => Buffer.from([]), - mnemonicWords: util.generateMnemonicWords() + mnemonicWords: util.generateMnemonicWords(), + purpose: KeyPurpose.STANDARD }, { bip32Ed25519: new SodiumBip32Ed25519(), logger } ) diff --git a/packages/tx-construction/test/tx-builder/TxBuilder.test.ts b/packages/tx-construction/test/tx-builder/TxBuilder.test.ts index 6b42ad3a369..14b32c22138 100644 --- a/packages/tx-construction/test/tx-builder/TxBuilder.test.ts +++ b/packages/tx-construction/test/tx-builder/TxBuilder.test.ts @@ -1,4 +1,5 @@ /* eslint-disable func-style */ +import { Cardano, Handle, ProviderError, ProviderFailure } from '@cardano-sdk/core'; /* eslint-disable jsdoc/require-jsdoc */ import * as Crypto from '@cardano-sdk/crypto'; import { @@ -7,13 +8,13 @@ import { Bip32Account, GroupedAddress, InMemoryKeyAgent, + KeyPurpose, SignTransactionOptions, TransactionSigner, util } from '@cardano-sdk/key-management'; import { AssetId, mockProviders as mocks } from '@cardano-sdk/util-dev'; import { BigIntMath, HexBlob } from '@cardano-sdk/util'; -import { Cardano, Handle, ProviderError, ProviderFailure } from '@cardano-sdk/core'; import { GenericTxBuilder, HandleNotFoundError, @@ -75,7 +76,8 @@ describe.each([ { chainId: Cardano.ChainIds.Preprod, getPassphrase: async () => Buffer.from('passphrase'), - mnemonicWords: util.generateMnemonicWords() + mnemonicWords: util.generateMnemonicWords(), + purpose: KeyPurpose.STANDARD }, { bip32Ed25519: new Crypto.SodiumBip32Ed25519(), logger: dummyLogger } ); @@ -258,7 +260,7 @@ describe.each([ let signingOptions: SignTransactionOptions; beforeEach(() => { - signingOptions = { additionalKeyPaths: [{ index: 1, role: 1 }] }; + signingOptions = { additionalKeyPaths: [{ index: 1, purpose: KeyPurpose.STANDARD, role: 1 }] }; }); it('can setSigningOptions without mutating signingOptions', () => { diff --git a/packages/tx-construction/test/tx-builder/TxBuilderDelegatePortfolio.test.ts b/packages/tx-construction/test/tx-builder/TxBuilderDelegatePortfolio.test.ts index f4eef6e2260..c3752371bd4 100644 --- a/packages/tx-construction/test/tx-builder/TxBuilderDelegatePortfolio.test.ts +++ b/packages/tx-construction/test/tx-builder/TxBuilderDelegatePortfolio.test.ts @@ -1,6 +1,13 @@ /* eslint-disable sonarjs/no-duplicate-string */ import * as Crypto from '@cardano-sdk/crypto'; -import { AddressType, Bip32Account, GroupedAddress, InMemoryKeyAgent, util } from '@cardano-sdk/key-management'; +import { + AddressType, + Bip32Account, + GroupedAddress, + InMemoryKeyAgent, + KeyPurpose, + util +} from '@cardano-sdk/key-management'; import { Cardano } from '@cardano-sdk/core'; import { GenericTxBuilder, @@ -144,7 +151,8 @@ describe('TxBuilder/delegatePortfolio', () => { { chainId: Cardano.ChainIds.Preprod, getPassphrase: async () => Buffer.from('passphrase'), - mnemonicWords: util.generateMnemonicWords() + mnemonicWords: util.generateMnemonicWords(), + purpose: KeyPurpose.STANDARD }, { bip32Ed25519: new Crypto.SodiumBip32Ed25519(), logger: dummyLogger } ); diff --git a/packages/tx-construction/test/tx-builder/TxBuilderPlutusScripts.test.ts b/packages/tx-construction/test/tx-builder/TxBuilderPlutusScripts.test.ts index 52672e73777..c1b4869117f 100644 --- a/packages/tx-construction/test/tx-builder/TxBuilderPlutusScripts.test.ts +++ b/packages/tx-construction/test/tx-builder/TxBuilderPlutusScripts.test.ts @@ -1,6 +1,6 @@ /* eslint-disable sonarjs/no-duplicate-string */ import * as Crypto from '@cardano-sdk/crypto'; -import { AddressType, Bip32Account, InMemoryKeyAgent, util } from '@cardano-sdk/key-management'; +import { AddressType, Bip32Account, InMemoryKeyAgent, KeyPurpose, util } from '@cardano-sdk/key-management'; import { Cardano, coalesceValueQuantities } from '@cardano-sdk/core'; import { DatumResolver, @@ -199,7 +199,8 @@ describe('TxBuilder/plutusScripts', () => { { chainId: Cardano.ChainIds.Preprod, getPassphrase: async () => Buffer.from('passphrase'), - mnemonicWords: util.generateMnemonicWords() + mnemonicWords: util.generateMnemonicWords(), + purpose: KeyPurpose.STANDARD }, { bip32Ed25519: new Crypto.SodiumBip32Ed25519(), logger: dummyLogger } ); diff --git a/packages/wallet/test/PersonalWallet/load.test.ts b/packages/wallet/test/PersonalWallet/load.test.ts index 68f3f1abead..a64fa66cb53 100644 --- a/packages/wallet/test/PersonalWallet/load.test.ts +++ b/packages/wallet/test/PersonalWallet/load.test.ts @@ -1,16 +1,12 @@ /* eslint-disable unicorn/consistent-destructuring */ -/* eslint-disable max-statements */ import { - AddressDiscovery, - BaseWallet, - ConnectionStatus, - ConnectionStatusTracker, - ObservableWallet, - PollingConfig, - SingleAddressDiscovery, - createPersonalWallet -} from '../../src'; -import { AddressType, AsyncKeyAgent, Bip32Account, GroupedAddress, util } from '@cardano-sdk/key-management'; + AddressType, + AsyncKeyAgent, + Bip32Account, + GroupedAddress, + KeyPurpose, + util +} from '@cardano-sdk/key-management'; import { AssetId, createStubStakePoolProvider, @@ -31,13 +27,24 @@ import { import { InvalidConfigurationError } from '@cardano-sdk/tx-construction'; import { InvalidStringError } from '@cardano-sdk/util'; import { ReplaySubject, firstValueFrom } from 'rxjs'; -import { WalletStores, createInMemoryWalletStores } from '../../src/persistence'; import { dummyLogger as logger } from 'ts-log'; import { stakeKeyDerivationPath, testAsyncKeyAgent } from '../../../key-management/test/mocks'; -import { waitForWalletStateSettle } from '../util'; import delay from 'delay'; import flatten from 'lodash/flatten'; import pick from 'lodash/pick'; +/* eslint-disable max-statements */ +import { + AddressDiscovery, + BaseWallet, + ConnectionStatus, + ConnectionStatusTracker, + ObservableWallet, + PollingConfig, + SingleAddressDiscovery, + createPersonalWallet +} from '../../src'; +import { WalletStores, createInMemoryWalletStores } from '../../src/persistence'; +import { waitForWalletStateSettle } from '../util'; const { currentEpoch, @@ -97,6 +104,7 @@ const createWallet = async (props: CreateWalletProps) => { address, index: 0, networkId: Cardano.NetworkId.Testnet, + purpose: KeyPurpose.STANDARD, rewardAccount, stakeKeyDerivationPath, type: AddressType.External @@ -120,7 +128,7 @@ const createWallet = async (props: CreateWalletProps) => { bip32Account.deriveAddress = jest.fn().mockResolvedValue(groupedAddress); return createPersonalWallet( - { name, polling: props.pollingConfig }, + { name, polling: props.pollingConfig, purpose: KeyPurpose.STANDARD }, { addressDiscovery: props?.addressDiscovery, assetProvider, @@ -490,6 +498,7 @@ describe('BaseWallet.AddressDiscovery', () => { address, index: 0, networkId: Cardano.NetworkId.Testnet, + purpose: KeyPurpose.STANDARD, rewardAccount, stakeKeyDerivationPath, type: AddressType.External diff --git a/packages/wallet/test/PersonalWallet/methods.test.ts b/packages/wallet/test/PersonalWallet/methods.test.ts index 4f3bbfc62f9..eea0b262924 100644 --- a/packages/wallet/test/PersonalWallet/methods.test.ts +++ b/packages/wallet/test/PersonalWallet/methods.test.ts @@ -1,7 +1,7 @@ /* eslint-disable unicorn/consistent-destructuring, sonarjs/no-duplicate-string, @typescript-eslint/no-floating-promises, promise/no-nesting, promise/always-return */ import * as Crypto from '@cardano-sdk/crypto'; import { AddressDiscovery, BaseWallet, TxInFlight, createPersonalWallet } from '../../src'; -import { AddressType, Bip32Account, GroupedAddress, Witnesser, util } from '@cardano-sdk/key-management'; +import { AddressType, Bip32Account, GroupedAddress, KeyPurpose, Witnesser, util } from '@cardano-sdk/key-management'; import { AssetId, createStubStakePoolProvider, mockProviders as mocks } from '@cardano-sdk/util-dev'; import { BehaviorSubject, Subscription, firstValueFrom, skip } from 'rxjs'; import { @@ -66,6 +66,7 @@ describe('BaseWallet methods', () => { address, index: 0, networkId: Cardano.NetworkId.Testnet, + purpose: KeyPurpose.STANDARD, rewardAccount: mocks.rewardAccount, stakeKeyDerivationPath, type: AddressType.External @@ -99,7 +100,7 @@ describe('BaseWallet methods', () => { bip32Account.deriveAddress = jest.fn().mockResolvedValue(groupedAddress); witnesser = util.createBip32Ed25519Witnesser(asyncKeyAgent); wallet = createPersonalWallet( - { name: 'Test Wallet' }, + { name: 'Test Wallet', purpose: KeyPurpose.STANDARD }, { addressDiscovery, assetProvider, @@ -468,7 +469,7 @@ describe('BaseWallet methods', () => { .mockRejectedValueOnce('error') .mockResolvedValue({ hex: () => 'string' }); wallet = createPersonalWallet( - { name: 'Test Wallet' }, + { name: 'Test Wallet', purpose: KeyPurpose.STANDARD }, { assetProvider, bip32Account, diff --git a/packages/wallet/test/PersonalWallet/rollback.test.ts b/packages/wallet/test/PersonalWallet/rollback.test.ts index 789a8246f8a..f6f4750b029 100644 --- a/packages/wallet/test/PersonalWallet/rollback.test.ts +++ b/packages/wallet/test/PersonalWallet/rollback.test.ts @@ -1,5 +1,5 @@ import * as Crypto from '@cardano-sdk/crypto'; -import { AddressType, Bip32Account, GroupedAddress, util } from '@cardano-sdk/key-management'; +import { AddressType, Bip32Account, GroupedAddress, KeyPurpose, util } from '@cardano-sdk/key-management'; import { Cardano, ChainHistoryProvider, @@ -35,6 +35,7 @@ const createWallet = async (stores: WalletStores, providers: Providers, pollingC address, index: 0, networkId: Cardano.NetworkId.Testnet, + purpose: KeyPurpose.STANDARD, rewardAccount, stakeKeyDerivationPath, type: AddressType.External @@ -54,7 +55,7 @@ const createWallet = async (stores: WalletStores, providers: Providers, pollingC const stakePoolProvider = createStubStakePoolProvider(); return createPersonalWallet( - { name, polling: pollingConfig }, + { name, polling: pollingConfig, purpose: KeyPurpose.STANDARD }, { addressDiscovery: new SingleAddressDiscovery(), assetProvider, diff --git a/packages/wallet/test/PersonalWallet/shutdown.test.ts b/packages/wallet/test/PersonalWallet/shutdown.test.ts index a1b6a33e7e2..cabd0322b20 100644 --- a/packages/wallet/test/PersonalWallet/shutdown.test.ts +++ b/packages/wallet/test/PersonalWallet/shutdown.test.ts @@ -1,6 +1,14 @@ /* eslint-disable max-statements */ +import { + Cardano, + ChainHistoryProvider, + NetworkInfoProvider, + RewardsProvider, + UtxoProvider, + coalesceValueQuantities +} from '@cardano-sdk/core'; /* eslint-disable @typescript-eslint/no-explicit-any */ -import { AddressType, Bip32Account, GroupedAddress, util } from '@cardano-sdk/key-management'; +import { AddressType, Bip32Account, GroupedAddress, KeyPurpose, util } from '@cardano-sdk/key-management'; import { AssetId, createStubStakePoolProvider, @@ -15,14 +23,6 @@ import { WalletNetworkInfoProviderStats, createPersonalWallet } from '../../src'; -import { - Cardano, - ChainHistoryProvider, - NetworkInfoProvider, - RewardsProvider, - UtxoProvider, - coalesceValueQuantities -} from '@cardano-sdk/core'; import { WalletStores, createInMemoryWalletStores } from '../../src/persistence'; import { firstValueFrom } from 'rxjs'; import { dummyLogger as logger } from 'ts-log'; @@ -47,6 +47,7 @@ const createWallet = async (stores: WalletStores, providers: Providers, pollingC address, index: 0, networkId: Cardano.NetworkId.Testnet, + purpose: KeyPurpose.STANDARD, rewardAccount, stakeKeyDerivationPath, type: AddressType.External @@ -61,7 +62,7 @@ const createWallet = async (stores: WalletStores, providers: Providers, pollingC const stakePoolProvider = createStubStakePoolProvider(); return createPersonalWallet( - { name, polling: pollingConfig }, + { name, polling: pollingConfig, purpose: KeyPurpose.STANDARD }, { assetProvider, bip32Account, diff --git a/packages/wallet/test/hardware/ledger/LedgerKeyAgent.test.ts b/packages/wallet/test/hardware/ledger/LedgerKeyAgent.test.ts index 1591769e689..ae5b8e8c3d8 100644 --- a/packages/wallet/test/hardware/ledger/LedgerKeyAgent.test.ts +++ b/packages/wallet/test/hardware/ledger/LedgerKeyAgent.test.ts @@ -4,6 +4,7 @@ import { AddressType, Bip32Account, CommunicationType, + KeyPurpose, SerializableLedgerKeyAgentData, util } from '@cardano-sdk/key-management'; @@ -77,7 +78,8 @@ describe('LedgerKeyAgent', () => { ledgerKeyAgent = await LedgerKeyAgent.createWithDevice( { chainId: Cardano.ChainIds.Preprod, - communicationType: CommunicationType.Node + communicationType: CommunicationType.Node, + purpose: KeyPurpose.STANDARD }, { bip32Ed25519: new Crypto.SodiumBip32Ed25519(), logger } ); @@ -93,7 +95,8 @@ describe('LedgerKeyAgent', () => { accountIndex: 5, chainId: Cardano.ChainIds.Preprod, communicationType: CommunicationType.Node, - deviceConnection: ledgerKeyAgent.deviceConnection + deviceConnection: ledgerKeyAgent.deviceConnection, + purpose: KeyPurpose.STANDARD }, mockKeyAgentDependencies() ); @@ -173,7 +176,7 @@ describe('LedgerKeyAgent', () => { const chainHistoryProvider = mocks.mockChainHistoryProvider({ rewardAccount }); const asyncKeyAgent = util.createAsyncKeyAgent(ledgerKeyAgent); wallet = createPersonalWallet( - { name: 'HW Wallet' }, + { name: 'HW Wallet', purpose: KeyPurpose.STANDARD }, { assetProvider, bip32Account: await Bip32Account.fromAsyncKeyAgent(asyncKeyAgent), @@ -206,7 +209,11 @@ describe('LedgerKeyAgent', () => { ...txInternals, hash: 'non-matching' as unknown as Cardano.TransactionId }, - { knownAddresses: await firstValueFrom(wallet.addresses$), txInKeyPathMap: {} } + { + knownAddresses: await firstValueFrom(wallet.addresses$), + purpose: KeyPurpose.STANDARD, + txInKeyPathMap: {} + } ) ).rejects.toThrow(); }); diff --git a/packages/wallet/test/integration/CustomObservableWallet.test.ts b/packages/wallet/test/integration/CustomObservableWallet.test.ts index 2dc26169d29..c0ebfecf192 100644 --- a/packages/wallet/test/integration/CustomObservableWallet.test.ts +++ b/packages/wallet/test/integration/CustomObservableWallet.test.ts @@ -1,8 +1,5 @@ /* eslint-disable @typescript-eslint/no-unused-expressions */ -/* eslint-disable sonarjs/no-extra-arguments */ -/* eslint-disable unicorn/consistent-function-scoping */ -import { BaseWallet, ObservableWallet, createPersonalWallet } from '../../src'; -import { Bip32Account, GroupedAddress, util } from '@cardano-sdk/key-management'; +import { Bip32Account, GroupedAddress, KeyPurpose, util } from '@cardano-sdk/key-management'; import { Cardano, Serialization } from '@cardano-sdk/core'; import { OutputValidator, @@ -15,6 +12,9 @@ import { createStubStakePoolProvider, mockProviders as mocks } from '@cardano-sd import { firstValueFrom, of, timer } from 'rxjs'; import { dummyLogger as logger } from 'ts-log'; import { testAsyncKeyAgent } from '../../../key-management/test/mocks'; +/* eslint-disable sonarjs/no-extra-arguments */ +/* eslint-disable unicorn/consistent-function-scoping */ +import { BaseWallet, ObservableWallet, createPersonalWallet } from '../../src'; describe('CustomObservableWallet', () => { describe('can create an application-specific subset of ObservableWallet interface', () => { @@ -41,7 +41,7 @@ describe('CustomObservableWallet', () => { it('can use BaseWallet to satisfy application-specific interface', async () => { // this compiles const extensionWallet: LaceObservableWallet = createPersonalWallet( - { name: 'Extension Wallet' }, + { name: 'Extension Wallet', purpose: KeyPurpose.STANDARD }, { assetProvider: mocks.mockAssetProvider(), bip32Account: await Bip32Account.fromAsyncKeyAgent(await testAsyncKeyAgent()), diff --git a/packages/wallet/test/integration/cip30mapping.test.ts b/packages/wallet/test/integration/cip30mapping.test.ts index c5879e3c263..bed14ddac59 100644 --- a/packages/wallet/test/integration/cip30mapping.test.ts +++ b/packages/wallet/test/integration/cip30mapping.test.ts @@ -1,4 +1,13 @@ /* eslint-disable unicorn/consistent-destructuring */ +import { + Cardano, + OutsideOfValidityIntervalData, + Serialization, + TxCBOR, + TxSubmissionError, + TxSubmissionErrorCode, + coalesceValueQuantities +} from '@cardano-sdk/core'; /* eslint-disable unicorn/consistent-function-scoping */ /* eslint-disable @typescript-eslint/no-explicit-any, sonarjs/no-duplicate-string */ import * as Crypto from '@cardano-sdk/crypto'; @@ -14,19 +23,10 @@ import { WalletApi, WithSenderContext } from '@cardano-sdk/dapp-connector'; -import { AddressType, Bip32Account, GroupedAddress, util } from '@cardano-sdk/key-management'; +import { AddressType, Bip32Account, GroupedAddress, KeyPurpose, util } from '@cardano-sdk/key-management'; import { AssetId, createStubStakePoolProvider, mockProviders as mocks } from '@cardano-sdk/util-dev'; import { BaseWallet, cip30, createPersonalWallet } from '../../src'; import { CallbackConfirmation, GetCollateralCallbackParams } from '../../src/cip30'; -import { - Cardano, - OutsideOfValidityIntervalData, - Serialization, - TxCBOR, - TxSubmissionError, - TxSubmissionErrorCode, - coalesceValueQuantities -} from '@cardano-sdk/core'; import { HexBlob, ManagedFreeableScope } from '@cardano-sdk/util'; import { InMemoryUnspendableUtxoStore, createInMemoryWalletStores } from '../../src/persistence'; import { InitializeTxProps, InitializeTxResult } from '@cardano-sdk/tx-construction'; @@ -739,6 +739,7 @@ describe('cip30', () => { address, index: 0, networkId: Cardano.NetworkId.Testnet, + purpose: KeyPurpose.STANDARD, rewardAccount: mocks.rewardAccount, stakeKeyDerivationPath, type: AddressType.External @@ -747,7 +748,7 @@ describe('cip30', () => { const bip32Account = await Bip32Account.fromAsyncKeyAgent(asyncKeyAgent); bip32Account.deriveAddress = jest.fn().mockResolvedValue(groupedAddress); mockWallet = createPersonalWallet( - { name: 'Test Wallet' }, + { name: 'Test Wallet', purpose: KeyPurpose.STANDARD }, { assetProvider, bip32Account, diff --git a/packages/wallet/test/integration/util.ts b/packages/wallet/test/integration/util.ts index 372af49cbff..6ca226e7ef5 100644 --- a/packages/wallet/test/integration/util.ts +++ b/packages/wallet/test/integration/util.ts @@ -1,4 +1,4 @@ -import { Bip32Account, util } from '@cardano-sdk/key-management'; +import { Bip32Account, KeyPurpose, util } from '@cardano-sdk/key-management'; import { WalletStores } from '../../src/persistence'; import { createPersonalWallet } from '../../src'; import { createStubStakePoolProvider, mockProviders } from '@cardano-sdk/util-dev'; @@ -32,7 +32,7 @@ export type Providers = { export const createWallet = async (stores?: WalletStores, providers: Providers = {}) => { const keyAgent = await testAsyncKeyAgent(); const wallet = createPersonalWallet( - { name: 'Test Wallet' }, + { name: 'Test Wallet', purpose: KeyPurpose.STANDARD }, { ...createDefaultProviders(), ...providers, diff --git a/packages/wallet/test/services/ChangeAddress/testData.ts b/packages/wallet/test/services/ChangeAddress/testData.ts index 251ff5d3924..cbeb1a3595e 100644 --- a/packages/wallet/test/services/ChangeAddress/testData.ts +++ b/packages/wallet/test/services/ChangeAddress/testData.ts @@ -1,4 +1,4 @@ -import { AddressType, GroupedAddress, KeyRole } from '@cardano-sdk/key-management'; +import { AddressType, GroupedAddress, KeyPurpose, KeyRole } from '@cardano-sdk/key-management'; import { BehaviorSubject } from 'rxjs'; import { Cardano } from '@cardano-sdk/core'; import { DelegatedStake, RewardsHistory } from '../../../src'; @@ -116,8 +116,9 @@ export const knownAddresses$ = new BehaviorSubject([ address: address_0_0, index: 0, networkId: Cardano.NetworkId.Testnet, + purpose: KeyPurpose.STANDARD, rewardAccount: rewardAccount_0, - stakeKeyDerivationPath: { index: 0, role: KeyRole.Stake }, + stakeKeyDerivationPath: { index: 0, purpose: KeyPurpose.STANDARD, role: KeyRole.Stake }, type: AddressType.External }, { @@ -125,8 +126,9 @@ export const knownAddresses$ = new BehaviorSubject([ address: address_0_1, index: 0, networkId: Cardano.NetworkId.Testnet, + purpose: KeyPurpose.STANDARD, rewardAccount: rewardAccount_1, - stakeKeyDerivationPath: { index: 1, role: KeyRole.Stake }, + stakeKeyDerivationPath: { index: 1, purpose: KeyPurpose.STANDARD, role: KeyRole.Stake }, type: AddressType.External }, { @@ -134,8 +136,9 @@ export const knownAddresses$ = new BehaviorSubject([ address: address_0_2, index: 0, networkId: Cardano.NetworkId.Testnet, + purpose: KeyPurpose.STANDARD, rewardAccount: rewardAccount_2, - stakeKeyDerivationPath: { index: 2, role: KeyRole.Stake }, + stakeKeyDerivationPath: { index: 2, purpose: KeyPurpose.STANDARD, role: KeyRole.Stake }, type: AddressType.External }, { @@ -143,8 +146,9 @@ export const knownAddresses$ = new BehaviorSubject([ address: address_0_3, index: 0, networkId: Cardano.NetworkId.Testnet, + purpose: KeyPurpose.STANDARD, rewardAccount: rewardAccount_3, - stakeKeyDerivationPath: { index: 3, role: KeyRole.Stake }, + stakeKeyDerivationPath: { index: 3, purpose: KeyPurpose.STANDARD, role: KeyRole.Stake }, type: AddressType.External }, { @@ -152,8 +156,9 @@ export const knownAddresses$ = new BehaviorSubject([ address: address_0_4, index: 0, networkId: Cardano.NetworkId.Testnet, + purpose: KeyPurpose.STANDARD, rewardAccount: rewardAccount_4, - stakeKeyDerivationPath: { index: 4, role: KeyRole.Stake }, + stakeKeyDerivationPath: { index: 4, purpose: KeyPurpose.STANDARD, role: KeyRole.Stake }, type: AddressType.External }, { @@ -161,8 +166,9 @@ export const knownAddresses$ = new BehaviorSubject([ address: address_0_5, index: 0, networkId: Cardano.NetworkId.Testnet, + purpose: KeyPurpose.STANDARD, rewardAccount: rewardAccount_5, - stakeKeyDerivationPath: { index: 5, role: KeyRole.Stake }, + stakeKeyDerivationPath: { index: 5, purpose: KeyPurpose.STANDARD, role: KeyRole.Stake }, type: AddressType.External }, { @@ -170,8 +176,9 @@ export const knownAddresses$ = new BehaviorSubject([ address: address_1_0, index: 1, networkId: Cardano.NetworkId.Testnet, + purpose: KeyPurpose.STANDARD, rewardAccount: rewardAccount_0, - stakeKeyDerivationPath: { index: 0, role: KeyRole.Stake }, + stakeKeyDerivationPath: { index: 0, purpose: KeyPurpose.STANDARD, role: KeyRole.Stake }, type: AddressType.External }, { @@ -179,8 +186,9 @@ export const knownAddresses$ = new BehaviorSubject([ address: address_2_0, index: 2, networkId: Cardano.NetworkId.Testnet, + purpose: KeyPurpose.STANDARD, rewardAccount: rewardAccount_0, - stakeKeyDerivationPath: { index: 0, role: KeyRole.Stake }, + stakeKeyDerivationPath: { index: 0, purpose: KeyPurpose.STANDARD, role: KeyRole.Stake }, type: AddressType.External }, { @@ -188,8 +196,9 @@ export const knownAddresses$ = new BehaviorSubject([ address: address_3_0, index: 3, networkId: Cardano.NetworkId.Testnet, + purpose: KeyPurpose.STANDARD, rewardAccount: rewardAccount_0, - stakeKeyDerivationPath: { index: 0, role: KeyRole.Stake }, + stakeKeyDerivationPath: { index: 0, purpose: KeyPurpose.STANDARD, role: KeyRole.Stake }, type: AddressType.External }, { @@ -197,8 +206,9 @@ export const knownAddresses$ = new BehaviorSubject([ address: address_4_0, index: 4, networkId: Cardano.NetworkId.Testnet, + purpose: KeyPurpose.STANDARD, rewardAccount: rewardAccount_0, - stakeKeyDerivationPath: { index: 0, role: KeyRole.Stake }, + stakeKeyDerivationPath: { index: 0, purpose: KeyPurpose.STANDARD, role: KeyRole.Stake }, type: AddressType.External }, { @@ -206,8 +216,9 @@ export const knownAddresses$ = new BehaviorSubject([ address: address_5_0, index: 5, networkId: Cardano.NetworkId.Testnet, + purpose: KeyPurpose.STANDARD, rewardAccount: rewardAccount_0, - stakeKeyDerivationPath: { index: 0, role: KeyRole.Stake }, + stakeKeyDerivationPath: { index: 0, purpose: KeyPurpose.STANDARD, role: KeyRole.Stake }, type: AddressType.External } ]); diff --git a/packages/wallet/test/services/DelegationTracker/DelegationDistributionTracker.test.ts b/packages/wallet/test/services/DelegationTracker/DelegationDistributionTracker.test.ts index bf40a25aa23..30c1fd57a86 100644 --- a/packages/wallet/test/services/DelegationTracker/DelegationDistributionTracker.test.ts +++ b/packages/wallet/test/services/DelegationTracker/DelegationDistributionTracker.test.ts @@ -1,4 +1,4 @@ -import { AddressType, GroupedAddress } from '@cardano-sdk/key-management'; +import { AddressType, GroupedAddress, KeyPurpose } from '@cardano-sdk/key-management'; import { Cardano } from '@cardano-sdk/core'; import { DelegatedStake, TransactionalObservables, UtxoTracker } from '../../../src'; import { EMPTY, Observable, map } from 'rxjs'; @@ -61,6 +61,7 @@ describe('createDelegationDistributionTracker', () => { address: mocks.utxo[0][1].address, index: 0, networkId: Cardano.NetworkId.Testnet, + purpose: KeyPurpose.STANDARD, rewardAccount: mocks.rewardAccount, stakeKeyDerivationPath, type: AddressType.External @@ -70,6 +71,7 @@ describe('createDelegationDistributionTracker', () => { address: utxos[1][1].address, index: 0, networkId: Cardano.NetworkId.Testnet, + purpose: KeyPurpose.STANDARD, rewardAccount: rewardAccounts[1].address, stakeKeyDerivationPath, type: AddressType.External diff --git a/packages/wallet/test/services/KeyAgent/restoreKeyAgent.test.ts b/packages/wallet/test/services/KeyAgent/restoreKeyAgent.test.ts index 94c046608c6..bc4bb676c69 100644 --- a/packages/wallet/test/services/KeyAgent/restoreKeyAgent.test.ts +++ b/packages/wallet/test/services/KeyAgent/restoreKeyAgent.test.ts @@ -5,6 +5,7 @@ import { GetPassphrase, KeyAgentDependencies, KeyAgentType, + KeyPurpose, SerializableInMemoryKeyAgentData, SerializableLedgerKeyAgentData, SerializableTrezorKeyAgentData, @@ -41,7 +42,8 @@ describe('KeyManagement/restoreKeyAgent', () => { accountIndex: 0, chainId: Cardano.ChainIds.Preview, encryptedRootPrivateKeyBytes, - extendedAccountPublicKey + extendedAccountPublicKey, + purpose: KeyPurpose.STANDARD }; // eslint-disable-next-line unicorn/consistent-function-scoping const getPassphrase: GetPassphrase = async () => Buffer.from('password'); @@ -76,7 +78,8 @@ describe('KeyManagement/restoreKeyAgent', () => { extendedAccountPublicKey: Crypto.Bip32PublicKeyHex( // eslint-disable-next-line max-len 'fc5ab25e830b67c47d0a17411bf7fdabf711a597fb6cf04102734b0a2934ceaaa65ff5e7c52498d52c07b8ddfcd436fc2b4d2775e2984a49d0c79f65ceee4779' - ) + ), + purpose: KeyPurpose.STANDARD }; it('can restore key manager from valid data', async () => { @@ -94,6 +97,7 @@ describe('KeyManagement/restoreKeyAgent', () => { // eslint-disable-next-line max-len 'fc5ab25e830b67c47d0a17411bf7fdabf711a597fb6cf04102734b0a2934ceaaa65ff5e7c52498d52c07b8ddfcd436fc2b4d2775e2984a49d0c79f65ceee4779' ), + purpose: KeyPurpose.STANDARD, trezorConfig: { communicationType: CommunicationType.Node, manifest: { diff --git a/packages/wallet/test/services/PublicStakeKeysTracker.test.ts b/packages/wallet/test/services/PublicStakeKeysTracker.test.ts index 68731f9324a..6beb42d1520 100644 --- a/packages/wallet/test/services/PublicStakeKeysTracker.test.ts +++ b/packages/wallet/test/services/PublicStakeKeysTracker.test.ts @@ -1,4 +1,10 @@ -import { AccountKeyDerivationPath, Bip32Account, GroupedAddress, KeyRole } from '@cardano-sdk/key-management'; +import { + AccountKeyDerivationPath, + Bip32Account, + GroupedAddress, + KeyPurpose, + KeyRole +} from '@cardano-sdk/key-management'; import { Cardano } from '@cardano-sdk/core'; import { ObservableWallet } from '../../src'; import { PubStakeKeyAndStatus, createPublicStakeKeysTracker } from '../../src/services/PublicStakeKeysTracker'; @@ -72,7 +78,8 @@ describe('PublicStakeKeysTracker', () => { chainId: Cardano.ChainIds.Preview, deriveAddress: jest.fn(), derivePublicKey, - extendedAccountPublicKey: '' as unknown + extendedAccountPublicKey: '' as unknown, + purpose: KeyPurpose.STANDARD } as Bip32Account; }); diff --git a/packages/wallet/test/services/WalletUtil.test.ts b/packages/wallet/test/services/WalletUtil.test.ts index bef28a7c523..3fe0ac56162 100644 --- a/packages/wallet/test/services/WalletUtil.test.ts +++ b/packages/wallet/test/services/WalletUtil.test.ts @@ -4,6 +4,7 @@ import { Bip32Account, GroupedAddress, util as KeyManagementUtil, + KeyPurpose, KeyRole } from '@cardano-sdk/key-management'; import { @@ -499,9 +500,11 @@ describe('WalletUtil', () => { address, index: 0, networkId: Cardano.NetworkId.Testnet, + purpose: KeyPurpose.MULTI_SIG, rewardAccount: mocks.rewardAccount, stakeKeyDerivationPath: { index: 0, + purpose: KeyPurpose.STANDARD, role: KeyRole.Stake }, type: AddressType.External @@ -510,7 +513,7 @@ describe('WalletUtil', () => { const bip32Account = await Bip32Account.fromAsyncKeyAgent(asyncKeyAgent); bip32Account.deriveAddress = jest.fn().mockResolvedValue(groupedAddress); wallet = createPersonalWallet( - { name: 'Test Wallet' }, + { name: 'Test Wallet', purpose: KeyPurpose.STANDARD }, { assetProvider, bip32Account, diff --git a/packages/wallet/test/services/addressDiscovery/SingleAddressDiscovery.test.ts b/packages/wallet/test/services/addressDiscovery/SingleAddressDiscovery.test.ts index 4a07c5d8e18..6e778c97daf 100644 --- a/packages/wallet/test/services/addressDiscovery/SingleAddressDiscovery.test.ts +++ b/packages/wallet/test/services/addressDiscovery/SingleAddressDiscovery.test.ts @@ -1,4 +1,4 @@ -import { Bip32Account, KeyRole } from '@cardano-sdk/key-management'; +import { Bip32Account, KeyPurpose, KeyRole } from '@cardano-sdk/key-management'; import { SingleAddressDiscovery } from '../../../src'; import { createAsyncKeyAgent } from '../../util'; @@ -15,8 +15,9 @@ describe('SingleAddressDiscovery', () => { address: expect.stringContaining('addr'), index: 0, networkId: 0, + purpose: KeyPurpose.STANDARD, rewardAccount: expect.stringContaining('stake'), - stakeKeyDerivationPath: { index: 0, role: KeyRole.Stake }, + stakeKeyDerivationPath: { index: 0, purpose: KeyPurpose.STANDARD, role: KeyRole.Stake }, type: 0 }); }); diff --git a/packages/wallet/test/util.ts b/packages/wallet/test/util.ts index 4ffe4bd3e20..7f8fa2b9f64 100644 --- a/packages/wallet/test/util.ts +++ b/packages/wallet/test/util.ts @@ -1,6 +1,6 @@ import * as Crypto from '@cardano-sdk/crypto'; import { Cardano, Serialization, TxCBOR } from '@cardano-sdk/core'; -import { GroupedAddress, InMemoryKeyAgent, WitnessedTx, util } from '@cardano-sdk/key-management'; +import { GroupedAddress, InMemoryKeyAgent, KeyPurpose, WitnessedTx, util } from '@cardano-sdk/key-management'; import { HexBlob } from '@cardano-sdk/util'; import { Observable, catchError, filter, firstValueFrom, throwError, timeout } from 'rxjs'; import { ObservableWallet, OutgoingTx, WalletUtil } from '../src'; @@ -74,7 +74,8 @@ export const createAsyncKeyAgent = async () => { chainId: Cardano.ChainIds.Preview, getPassphrase: async () => Buffer.from([]), - mnemonicWords: util.generateMnemonicWords() + mnemonicWords: util.generateMnemonicWords(), + purpose: KeyPurpose.STANDARD }, { bip32Ed25519: new SodiumBip32Ed25519(), @@ -103,6 +104,7 @@ export const signTx = async ({ ), { knownAddresses, + purpose: KeyPurpose.STANDARD, txInKeyPathMap: await util.createTxInKeyPathMap(tx.body, knownAddresses, walletUtil) } );