Skip to content

Commit 152a69f

Browse files
committed
fix: remove several unnecessary purpose
1 parent 3ae0b1b commit 152a69f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+142
-246
lines changed

packages/e2e/src/util/createMockKeyAgent.ts

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ export const createMockKeyAgent = (deriveAddressesReturn: GroupedAddress[] = [])
1818
derivePublicKey: jest.fn(),
1919
exportRootPrivateKey: jest.fn(),
2020
extendedAccountPublicKey,
21+
get purpose(): KeyPurpose {
22+
return KeyPurpose.STANDARD;
23+
},
2124
serializableData: {
2225
__typename: KeyAgentType.InMemory,
2326
accountIndex,

packages/e2e/test/artillery/wallet-restoration/WalletRestoration.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AddressType, GroupedAddress, KeyPurpose, util } from '@cardano-sdk/key-management';
1+
import { AddressType, GroupedAddress, util } from '@cardano-sdk/key-management';
22
import { AddressesModel, WalletVars } from './types';
33
import { Cardano } from '@cardano-sdk/core';
44
import { FunctionHook } from '../artillery';
@@ -12,7 +12,6 @@ export const mapToGroupedAddress = (addrModel: AddressesModel): GroupedAddress =
1212
address: Cardano.PaymentAddress(addrModel.address),
1313
index: 0,
1414
networkId: addrModel.address.startsWith('addr_test') ? Cardano.NetworkId.Testnet : Cardano.NetworkId.Mainnet,
15-
purpose: KeyPurpose.STANDARD,
1615
rewardAccount: Cardano.RewardAccount(addrModel.stake_address),
1716
type: AddressType.External
1817
});

packages/e2e/test/long-running/multisig-wallet/MultiSigWallet.ts

-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ const DUMMY_HEX_BYTES =
3434

3535
const DERIVATION_PATH: AccountKeyDerivationPath = {
3636
index: 0,
37-
purpose: KeyPurpose.MULTI_SIG,
3837
role: KeyRole.External
3938
};
4039

@@ -413,7 +412,6 @@ export class MultiSigWallet {
413412
address: baseAddress.toAddress().toBech32() as Cardano.PaymentAddress,
414413
index: 0,
415414
networkId,
416-
purpose: KeyPurpose.MULTI_SIG,
417415
rewardAccount: Cardano.RewardAddress.fromCredentials(networkId, scriptCredential)
418416
.toAddress()
419417
.toBech32() as Cardano.RewardAccount,

packages/hardware-ledger/src/LedgerKeyAgent.ts

-1
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,6 @@ export class LedgerKeyAgent extends KeyAgentBase {
577577
result.witnesses.map(async (witness) => {
578578
const publicKey = await this.derivePublicKey({
579579
index: witness.path[Cip1852PathLevelIndexes.INDEX],
580-
purpose: witness.path[Cip1852PathLevelIndexes.PURPOSE],
581580
role: witness.path[Cip1852PathLevelIndexes.ROLE]
582581
});
583582
const signature = Crypto.Ed25519SignatureHex(witness.witnessSignatureHex);

packages/hardware-ledger/src/transformers/certificates.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ const getCredentialType = (knownAddress: GroupedAddress | undefined) => {
104104
const stakeCredentialMapper = (credential: Cardano.Credential, context: LedgerTxTransformerContext) => {
105105
const knownAddress = getKnownAddress(credential, context);
106106
const credentialType = getCredentialType(knownAddress);
107-
const path = util.stakeKeyPathFromGroupedAddress({ address: knownAddress, purpose: context.purpose });
107+
const path = util.stakeKeyPathFromGroupedAddress(knownAddress);
108108

109109
return credentialMapper(credential, credentialType, path);
110110
};
@@ -160,7 +160,7 @@ const getPoolOperatorKeyPath = (
160160
context: LedgerTxTransformerContext
161161
): Ledger.BIP32Path | null => {
162162
const knownAddress = context?.knownAddresses.find((address) => address.rewardAccount === operator);
163-
return util.stakeKeyPathFromGroupedAddress({ address: knownAddress, purpose: context.purpose });
163+
return util.stakeKeyPathFromGroupedAddress(knownAddress);
164164
};
165165

166166
export const poolRegistrationCertificate: Transform<
@@ -270,7 +270,7 @@ const poolRetirementCertificate: Transform<
270270
)
271271
);
272272

273-
const poolKeyPath = util.stakeKeyPathFromGroupedAddress({ address: knownAddress, purpose: context?.purpose });
273+
const poolKeyPath = util.stakeKeyPathFromGroupedAddress(knownAddress);
274274

275275
if (!poolKeyPath) throw new InvalidArgumentError('certificate', 'Missing key matching pool retirement certificate.');
276276

packages/hardware-ledger/src/transformers/requiredSigners.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,11 @@ export const toRequiredSigner: Transform<
1919
const stakeCredential = Cardano.RewardAccount.toHash(address.rewardAccount);
2020
return !!stakeCredential && areStringsEqualInConstantTime(stakeCredential.toString(), keyHash);
2121
});
22-
const purpose = context?.purpose;
2322

24-
const paymentKeyPath =
25-
paymentCredKnownAddress && purpose
26-
? util.paymentKeyPathFromGroupedAddress({ address: paymentCredKnownAddress, purpose })
27-
: null;
28-
const stakeKeyPath =
29-
stakeCredKnownAddress && purpose
30-
? util.stakeKeyPathFromGroupedAddress({ address: stakeCredKnownAddress, purpose })
31-
: null;
23+
const paymentKeyPath = paymentCredKnownAddress
24+
? util.paymentKeyPathFromGroupedAddress(paymentCredKnownAddress)
25+
: null;
26+
const stakeKeyPath = stakeCredKnownAddress ? util.stakeKeyPathFromGroupedAddress(stakeCredKnownAddress) : null;
3227

3328
if (paymentKeyPath) {
3429
return {

packages/hardware-ledger/src/transformers/txIn.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ import { TxInId, util } from '@cardano-sdk/key-management';
66

77
const resolveKeyPath = (
88
txIn: Cardano.TxIn,
9-
{ accountIndex, txInKeyPathMap }: LedgerTxTransformerContext
9+
{ accountIndex, txInKeyPathMap, purpose }: LedgerTxTransformerContext
1010
): Ledger.BIP32Path | null => {
11-
const utxoKeyPath = txInKeyPathMap[TxInId(txIn)];
12-
if (utxoKeyPath) {
13-
return util.accountKeyDerivationPathToBip32Path(accountIndex, utxoKeyPath);
14-
}
11+
const txInKeyPath = txInKeyPathMap[TxInId(txIn)];
1512

16-
return null;
13+
if (!txInKeyPath || txInKeyPath.role === undefined || txInKeyPath.index === undefined) return null;
14+
15+
const utxoKeyPath = {
16+
...txInKeyPath,
17+
purpose
18+
};
19+
20+
return util.accountKeyDerivationPathToBip32Path(accountIndex, utxoKeyPath);
1721
};
1822

1923
export const toTxIn: Transform<Cardano.TxIn, Ledger.TxInput, LedgerTxTransformerContext> = (txIn, context) => ({

packages/hardware-ledger/src/transformers/txOut.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,10 @@ const toDestination: Transform<Cardano.TxOut, Ledger.TxOutputDestination, Ledger
2020
context
2121
) => {
2222
const knownAddress = context?.knownAddresses.find((address) => address.address === txOut.address);
23-
const purpose = context?.purpose;
2423

25-
if (knownAddress && purpose) {
26-
const paymentKeyPath = util.paymentKeyPathFromGroupedAddress({
27-
address: knownAddress,
28-
purpose
29-
});
30-
const stakeKeyPath = util.stakeKeyPathFromGroupedAddress({
31-
address: knownAddress,
32-
purpose
33-
});
24+
if (knownAddress) {
25+
const paymentKeyPath = util.paymentKeyPathFromGroupedAddress(knownAddress);
26+
const stakeKeyPath = util.stakeKeyPathFromGroupedAddress(knownAddress);
3427

3528
if (!stakeKeyPath) throw new InvalidArgumentError('txOut', 'Missing stake key key path.');
3629

packages/hardware-ledger/src/types.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Cardano } from '@cardano-sdk/core';
22
import { HID } from 'node-hid';
3-
import { KeyPurpose, SignTransactionContext } from '@cardano-sdk/key-management';
3+
import { SignTransactionContext } from '@cardano-sdk/key-management';
44
import TransportNodeHid from '@ledgerhq/hw-transport-node-hid-noevents';
55
import TransportWebUSB from '@ledgerhq/hw-transport-webusb';
66

@@ -21,6 +21,4 @@ export type LedgerTxTransformerContext = {
2121
chainId: Cardano.ChainId;
2222
/** Non-hardened account index */
2323
accountIndex: number;
24-
/** purpose of cip1852 or cip1854 */
25-
purpose: KeyPurpose;
2624
} & SignTransactionContext;

packages/hardware-ledger/test/testData.ts

-2
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,9 @@ export const CONTEXT_WITH_KNOWN_ADDRESSES: LedgerTxTransformerContext = {
349349
address: paymentAddress,
350350
index: 0,
351351
networkId: Cardano.NetworkId.Testnet,
352-
purpose: KeyPurpose.STANDARD,
353352
rewardAccount,
354353
stakeKeyDerivationPath: {
355354
index: 0,
356-
purpose: KeyPurpose.STANDARD,
357355
role: KeyRole.Stake
358356
},
359357
type: AddressType.Internal

packages/hardware-ledger/test/transformers/certificates.test.ts

-7
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,11 @@ const createGroupedAddress = ({
3535
rewardAccount,
3636
stakeKeyDerivationPath,
3737
index,
38-
purpose,
3938
type
4039
}: Omit<GroupedAddress, 'networkId' | 'accountIndex'>): GroupedAddress =>
4140
({
4241
address,
4342
index,
44-
purpose,
4543
rewardAccount,
4644
stakeKeyDerivationPath,
4745
type
@@ -67,7 +65,6 @@ export const createTxInKeyPathMapMock = (knownAddresses: GroupedAddress[]): TxIn
6765
const txInId: TxInId = `MockTxIn_${index}` as TxInId; // Mock TxInId creation
6866
result[txInId] = {
6967
index: address.index,
70-
purpose: address.purpose,
7168
role: KeyRole.Internal
7269
};
7370
}
@@ -85,15 +82,13 @@ const mockContext: LedgerTxTransformerContext = {
8582
createGroupedAddress({
8683
address: address1,
8784
index: 0,
88-
purpose: KeyPurpose.STANDARD,
8985
rewardAccount: ownRewardAccount,
9086
stakeKeyDerivationPath: stakeKeyPath,
9187
type: AddressType.External
9288
}),
9389
createGroupedAddress({
9490
address: address2,
9591
index: 1,
96-
purpose: KeyPurpose.STANDARD,
9792
rewardAccount: ownRewardAccount,
9893
stakeKeyDerivationPath: stakeKeyPath,
9994
type: AddressType.External
@@ -105,15 +100,13 @@ const mockContext: LedgerTxTransformerContext = {
105100
createGroupedAddress({
106101
address: address1,
107102
index: 0,
108-
purpose: KeyPurpose.STANDARD,
109103
rewardAccount: ownRewardAccount,
110104
stakeKeyDerivationPath: stakeKeyPath,
111105
type: AddressType.External
112106
}),
113107
createGroupedAddress({
114108
address: address2,
115109
index: 1,
116-
purpose: KeyPurpose.STANDARD,
117110
rewardAccount: ownRewardAccount,
118111
stakeKeyDerivationPath: stakeKeyPath,
119112
type: AddressType.External

packages/hardware-ledger/test/transformers/tx.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import { toLedgerTx } from '../../src';
77
describe('tx', () => {
88
describe('toLedgerTx', () => {
99
test('can map a transaction with scripts', async () => {
10-
const paymentKeyPath = { index: 0, purpose: KeyPurpose.STANDARD, role: 1 };
11-
const stakeKeyPath = { index: 0, purpose: KeyPurpose.STANDARD, role: 2 };
10+
const paymentKeyPath = { index: 0, role: 1 };
11+
const stakeKeyPath = { index: 0, role: 2 };
1212
expect(
1313
await toLedgerTx(tx.body, {
1414
...CONTEXT_WITH_KNOWN_ADDRESSES,

packages/hardware-trezor/src/transformers/additionalWitnessRequests.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ export const mapAdditionalWitnessRequests: Transform<
2222
const additionalWitnessPaths: Trezor.DerivationPath[] = [...paymentKeyPaths];
2323

2424
if (context?.knownAddresses?.length) {
25-
const stakeKeyPath = util.stakeKeyPathFromGroupedAddress({
26-
address: context.knownAddresses[0],
27-
purpose: context.purpose
28-
});
25+
const stakeKeyPath = util.stakeKeyPathFromGroupedAddress(context.knownAddresses[0]);
2926
if (stakeKeyPath) additionalWitnessPaths.push(stakeKeyPath);
3027
}
3128
return additionalWitnessPaths;

packages/hardware-trezor/src/transformers/certificates.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Crypto from '@cardano-sdk/crypto';
22
import * as Trezor from '@trezor/connect';
33
import { BIP32Path } from '@cardano-sdk/crypto';
44
import { Cardano } from '@cardano-sdk/core';
5-
import { GroupedAddress, KeyPurpose, util } from '@cardano-sdk/key-management';
5+
import { GroupedAddress, util } from '@cardano-sdk/key-management';
66
import {
77
InvalidArgumentError,
88
Transform,
@@ -19,8 +19,7 @@ type CertCredentialsType = {
1919

2020
const getCertCredentials = (
2121
stakeKeyHash: Crypto.Ed25519KeyHashHex,
22-
knownAddresses: GroupedAddress[] | undefined,
23-
purpose: KeyPurpose | undefined
22+
knownAddresses: GroupedAddress[] | undefined
2423
): CertCredentialsType => {
2524
const knownAddress = knownAddresses?.find((address) =>
2625
areStringsEqualInConstantTime(Cardano.RewardAccount.toHash(address.rewardAccount), stakeKeyHash)
@@ -31,7 +30,7 @@ const getCertCredentials = (
3130
!!rewardAddress &&
3231
areNumbersEqualInConstantTime(rewardAddress?.getPaymentCredential().type, Cardano.CredentialType.KeyHash)
3332
) {
34-
const path = util.stakeKeyPathFromGroupedAddress({ address: knownAddress, purpose });
33+
const path = util.stakeKeyPathFromGroupedAddress(knownAddress);
3534
return path ? { path } : { keyHash: stakeKeyHash };
3635
}
3736
return {
@@ -49,7 +48,7 @@ const getPoolOperatorKeyPath = (
4948
context: TrezorTxTransformerContext
5049
): BIP32Path | null => {
5150
const knownAddress = context?.knownAddresses.find((address) => address.rewardAccount === operator);
52-
return util.stakeKeyPathFromGroupedAddress({ address: knownAddress, purpose: context.purpose });
51+
return util.stakeKeyPathFromGroupedAddress(knownAddress);
5352
};
5453

5554
export const getStakeAddressCertificate: Transform<
@@ -59,8 +58,7 @@ export const getStakeAddressCertificate: Transform<
5958
> = (certificate, context) => {
6059
const credentials = getCertCredentials(
6160
certificate.stakeCredential.hash as unknown as Crypto.Ed25519KeyHashHex,
62-
context?.knownAddresses,
63-
context?.purpose
61+
context?.knownAddresses
6462
);
6563
const certificateType =
6664
certificate.__typename === Cardano.CertificateType.StakeRegistration
@@ -84,8 +82,7 @@ export const getStakeDelegationCertificate: Transform<
8482
const poolIdKeyHash = Cardano.PoolId.toKeyHash(certificate.poolId);
8583
const credentials = getCertCredentials(
8684
certificate.stakeCredential.hash as unknown as Crypto.Ed25519KeyHashHex,
87-
context?.knownAddresses,
88-
context?.purpose
85+
context?.knownAddresses
8986
);
9087
return {
9188
keyHash: credentials.keyHash,
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BIP32Path } from '@cardano-sdk/crypto';
22
import { Cardano } from '@cardano-sdk/core';
3-
import { GroupedAddress, KeyPurpose, TxInId, util } from '@cardano-sdk/key-management';
3+
import { GroupedAddress, TxInId, util } from '@cardano-sdk/key-management';
44
import { TrezorTxTransformerContext } from '../types';
55

66
/** Uses the given Trezor input resolver to resolve the payment key path for known addresses for given input transaction. */
@@ -9,22 +9,24 @@ export const resolvePaymentKeyPathForTxIn = (
99
context?: TrezorTxTransformerContext
1010
): BIP32Path | undefined => {
1111
if (!context) return;
12-
const txInKeyPath = context?.txInKeyPathMap[TxInId(txIn)];
13-
if (txInKeyPath) {
14-
return util.accountKeyDerivationPathToBip32Path(context.accountIndex, txInKeyPath);
15-
}
12+
13+
const txInKeyPath = context.txInKeyPathMap[TxInId(txIn)];
14+
15+
if (!txInKeyPath || txInKeyPath.role === undefined || txInKeyPath.index === undefined) return;
16+
const utxoKeyPath = {
17+
...txInKeyPath,
18+
purpose: context.purpose
19+
};
20+
21+
return util.accountKeyDerivationPathToBip32Path(context.accountIndex, utxoKeyPath);
1622
};
1723

1824
export const resolveStakeKeyPath = (
1925
rewardAddress: Cardano.RewardAddress,
20-
knownAddresses: GroupedAddress[],
21-
purpose: KeyPurpose
26+
knownAddresses: GroupedAddress[]
2227
): BIP32Path | null => {
2328
const knownAddress = knownAddresses.find(
2429
({ rewardAccount }) => rewardAccount === rewardAddress.toAddress().toBech32()
2530
);
26-
return util.stakeKeyPathFromGroupedAddress({
27-
address: knownAddress,
28-
purpose
29-
});
31+
return util.stakeKeyPathFromGroupedAddress(knownAddress);
3032
};

packages/hardware-trezor/src/transformers/requiredSigners.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,16 @@ export const toRequiredSigner: Transform<
1414
const paymentCredential = Cardano.Address.fromBech32(address.address)?.asBase()?.getPaymentCredential().hash;
1515
return !!paymentCredential && areStringsEqualInConstantTime(paymentCredential.toString(), keyHash);
1616
});
17-
const purpose = context?.purpose;
1817

1918
const stakeCredKnownAddress = context?.knownAddresses.find((address) => {
2019
const stakeCredential = Cardano.RewardAccount.toHash(address.rewardAccount);
2120
return !!stakeCredential && areStringsEqualInConstantTime(stakeCredential.toString(), keyHash);
2221
});
2322

24-
const paymentKeyPath =
25-
paymentCredKnownAddress && purpose
26-
? util.paymentKeyPathFromGroupedAddress({ address: paymentCredKnownAddress, purpose })
27-
: null;
28-
const stakeKeyPath =
29-
stakeCredKnownAddress && purpose
30-
? util.stakeKeyPathFromGroupedAddress({ address: stakeCredKnownAddress, purpose })
31-
: null;
23+
const paymentKeyPath = paymentCredKnownAddress
24+
? util.paymentKeyPathFromGroupedAddress(paymentCredKnownAddress)
25+
: null;
26+
const stakeKeyPath = stakeCredKnownAddress ? util.stakeKeyPathFromGroupedAddress(stakeCredKnownAddress) : null;
3227

3328
if (paymentKeyPath) {
3429
return {

packages/hardware-trezor/src/transformers/txOut.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@ const toDestination: Transform<Cardano.TxOut, TrezorTxOutputDestination, TrezorT
1010
context
1111
) => {
1212
const knownAddress = context?.knownAddresses.find((address: GroupedAddress) => address.address === txOut.address);
13-
const purpose = knownAddress?.purpose;
1413

15-
if (!knownAddress || !purpose) {
14+
if (!knownAddress) {
1615
return {
1716
address: txOut.address
1817
};
1918
}
2019

21-
const paymentPath = util.paymentKeyPathFromGroupedAddress({ address: knownAddress, purpose });
22-
const stakingPath = util.stakeKeyPathFromGroupedAddress({ address: knownAddress, purpose });
20+
const paymentPath = util.paymentKeyPathFromGroupedAddress(knownAddress);
21+
const stakingPath = util.stakeKeyPathFromGroupedAddress(knownAddress);
2322

2423
if (!stakingPath) throw new InvalidArgumentError('txOut', 'Missing staking key key path.');
2524

0 commit comments

Comments
 (0)