Skip to content

Commit

Permalink
fix: remove several unnecessary purpose
Browse files Browse the repository at this point in the history
  • Loading branch information
greatertomi committed Jun 5, 2024
1 parent 3ae0b1b commit 152a69f
Show file tree
Hide file tree
Showing 48 changed files with 142 additions and 246 deletions.
3 changes: 3 additions & 0 deletions packages/e2e/src/util/createMockKeyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export const createMockKeyAgent = (deriveAddressesReturn: GroupedAddress[] = [])
derivePublicKey: jest.fn(),
exportRootPrivateKey: jest.fn(),
extendedAccountPublicKey,
get purpose(): KeyPurpose {
return KeyPurpose.STANDARD;
},
serializableData: {
__typename: KeyAgentType.InMemory,
accountIndex,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AddressType, GroupedAddress, KeyPurpose, util } from '@cardano-sdk/key-management';
import { AddressType, GroupedAddress, util } from '@cardano-sdk/key-management';
import { AddressesModel, WalletVars } from './types';
import { Cardano } from '@cardano-sdk/core';
import { FunctionHook } from '../artillery';
Expand All @@ -12,7 +12,6 @@ export const mapToGroupedAddress = (addrModel: AddressesModel): GroupedAddress =
address: Cardano.PaymentAddress(addrModel.address),
index: 0,
networkId: addrModel.address.startsWith('addr_test') ? Cardano.NetworkId.Testnet : Cardano.NetworkId.Mainnet,
purpose: KeyPurpose.STANDARD,
rewardAccount: Cardano.RewardAccount(addrModel.stake_address),
type: AddressType.External
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const DUMMY_HEX_BYTES =

const DERIVATION_PATH: AccountKeyDerivationPath = {
index: 0,
purpose: KeyPurpose.MULTI_SIG,
role: KeyRole.External
};

Expand Down Expand Up @@ -413,7 +412,6 @@ export class MultiSigWallet {
address: baseAddress.toAddress().toBech32() as Cardano.PaymentAddress,
index: 0,
networkId,
purpose: KeyPurpose.MULTI_SIG,
rewardAccount: Cardano.RewardAddress.fromCredentials(networkId, scriptCredential)
.toAddress()
.toBech32() as Cardano.RewardAccount,
Expand Down
1 change: 0 additions & 1 deletion packages/hardware-ledger/src/LedgerKeyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,6 @@ export class LedgerKeyAgent extends KeyAgentBase {
result.witnesses.map(async (witness) => {
const publicKey = await this.derivePublicKey({
index: witness.path[Cip1852PathLevelIndexes.INDEX],
purpose: witness.path[Cip1852PathLevelIndexes.PURPOSE],
role: witness.path[Cip1852PathLevelIndexes.ROLE]
});
const signature = Crypto.Ed25519SignatureHex(witness.witnessSignatureHex);
Expand Down
6 changes: 3 additions & 3 deletions packages/hardware-ledger/src/transformers/certificates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, purpose: context.purpose });
const path = util.stakeKeyPathFromGroupedAddress(knownAddress);

return credentialMapper(credential, credentialType, path);
};
Expand Down Expand Up @@ -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, purpose: context.purpose });
return util.stakeKeyPathFromGroupedAddress(knownAddress);
};

export const poolRegistrationCertificate: Transform<
Expand Down Expand Up @@ -270,7 +270,7 @@ const poolRetirementCertificate: Transform<
)
);

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

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

Expand Down
13 changes: 4 additions & 9 deletions packages/hardware-ledger/src/transformers/requiredSigners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,11 @@ export const toRequiredSigner: Transform<
const stakeCredential = Cardano.RewardAccount.toHash(address.rewardAccount);
return !!stakeCredential && areStringsEqualInConstantTime(stakeCredential.toString(), keyHash);
});
const purpose = context?.purpose;

const paymentKeyPath =
paymentCredKnownAddress && purpose
? util.paymentKeyPathFromGroupedAddress({ address: paymentCredKnownAddress, purpose })
: null;
const stakeKeyPath =
stakeCredKnownAddress && purpose
? util.stakeKeyPathFromGroupedAddress({ address: stakeCredKnownAddress, purpose })
: null;
const paymentKeyPath = paymentCredKnownAddress
? util.paymentKeyPathFromGroupedAddress(paymentCredKnownAddress)
: null;
const stakeKeyPath = stakeCredKnownAddress ? util.stakeKeyPathFromGroupedAddress(stakeCredKnownAddress) : null;

if (paymentKeyPath) {
return {
Expand Down
16 changes: 10 additions & 6 deletions packages/hardware-ledger/src/transformers/txIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import { TxInId, util } from '@cardano-sdk/key-management';

const resolveKeyPath = (
txIn: Cardano.TxIn,
{ accountIndex, txInKeyPathMap }: LedgerTxTransformerContext
{ accountIndex, txInKeyPathMap, purpose }: LedgerTxTransformerContext
): Ledger.BIP32Path | null => {
const utxoKeyPath = txInKeyPathMap[TxInId(txIn)];
if (utxoKeyPath) {
return util.accountKeyDerivationPathToBip32Path(accountIndex, utxoKeyPath);
}
const txInKeyPath = txInKeyPathMap[TxInId(txIn)];

return null;
if (!txInKeyPath || txInKeyPath.role === undefined || txInKeyPath.index === undefined) return null;

const utxoKeyPath = {
...txInKeyPath,
purpose
};

return util.accountKeyDerivationPathToBip32Path(accountIndex, utxoKeyPath);
};

export const toTxIn: Transform<Cardano.TxIn, Ledger.TxInput, LedgerTxTransformerContext> = (txIn, context) => ({
Expand Down
13 changes: 3 additions & 10 deletions packages/hardware-ledger/src/transformers/txOut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,10 @@ const toDestination: Transform<Cardano.TxOut, Ledger.TxOutputDestination, Ledger
context
) => {
const knownAddress = context?.knownAddresses.find((address) => address.address === txOut.address);
const purpose = context?.purpose;

if (knownAddress && purpose) {
const paymentKeyPath = util.paymentKeyPathFromGroupedAddress({
address: knownAddress,
purpose
});
const stakeKeyPath = util.stakeKeyPathFromGroupedAddress({
address: knownAddress,
purpose
});
if (knownAddress) {
const paymentKeyPath = util.paymentKeyPathFromGroupedAddress(knownAddress);
const stakeKeyPath = util.stakeKeyPathFromGroupedAddress(knownAddress);

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

Expand Down
4 changes: 1 addition & 3 deletions packages/hardware-ledger/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Cardano } from '@cardano-sdk/core';
import { HID } from 'node-hid';
import { KeyPurpose, SignTransactionContext } from '@cardano-sdk/key-management';
import { SignTransactionContext } from '@cardano-sdk/key-management';
import TransportNodeHid from '@ledgerhq/hw-transport-node-hid-noevents';
import TransportWebUSB from '@ledgerhq/hw-transport-webusb';

Expand All @@ -21,6 +21,4 @@ export type LedgerTxTransformerContext = {
chainId: Cardano.ChainId;
/** Non-hardened account index */
accountIndex: number;
/** purpose of cip1852 or cip1854 */
purpose: KeyPurpose;
} & SignTransactionContext;
2 changes: 0 additions & 2 deletions packages/hardware-ledger/test/testData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,9 @@ export const CONTEXT_WITH_KNOWN_ADDRESSES: LedgerTxTransformerContext = {
address: paymentAddress,
index: 0,
networkId: Cardano.NetworkId.Testnet,
purpose: KeyPurpose.STANDARD,
rewardAccount,
stakeKeyDerivationPath: {
index: 0,
purpose: KeyPurpose.STANDARD,
role: KeyRole.Stake
},
type: AddressType.Internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ const createGroupedAddress = ({
rewardAccount,
stakeKeyDerivationPath,
index,
purpose,
type
}: Omit<GroupedAddress, 'networkId' | 'accountIndex'>): GroupedAddress =>
({
address,
index,
purpose,
rewardAccount,
stakeKeyDerivationPath,
type
Expand All @@ -67,7 +65,6 @@ 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
};
}
Expand All @@ -85,15 +82,13 @@ const mockContext: LedgerTxTransformerContext = {
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
Expand All @@ -105,15 +100,13 @@ const mockContext: LedgerTxTransformerContext = {
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
Expand Down
4 changes: 2 additions & 2 deletions packages/hardware-ledger/test/transformers/tx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { toLedgerTx } from '../../src';
describe('tx', () => {
describe('toLedgerTx', () => {
test('can map a transaction with scripts', async () => {
const paymentKeyPath = { index: 0, purpose: KeyPurpose.STANDARD, role: 1 };
const stakeKeyPath = { index: 0, purpose: KeyPurpose.STANDARD, role: 2 };
const paymentKeyPath = { index: 0, role: 1 };
const stakeKeyPath = { index: 0, role: 2 };
expect(
await toLedgerTx(tx.body, {
...CONTEXT_WITH_KNOWN_ADDRESSES,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ export const mapAdditionalWitnessRequests: Transform<
const additionalWitnessPaths: Trezor.DerivationPath[] = [...paymentKeyPaths];

if (context?.knownAddresses?.length) {
const stakeKeyPath = util.stakeKeyPathFromGroupedAddress({
address: context.knownAddresses[0],
purpose: context.purpose
});
const stakeKeyPath = util.stakeKeyPathFromGroupedAddress(context.knownAddresses[0]);
if (stakeKeyPath) additionalWitnessPaths.push(stakeKeyPath);
}
return additionalWitnessPaths;
Expand Down
15 changes: 6 additions & 9 deletions packages/hardware-trezor/src/transformers/certificates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, KeyPurpose, util } from '@cardano-sdk/key-management';
import { GroupedAddress, util } from '@cardano-sdk/key-management';
import {
InvalidArgumentError,
Transform,
Expand All @@ -19,8 +19,7 @@ type CertCredentialsType = {

const getCertCredentials = (
stakeKeyHash: Crypto.Ed25519KeyHashHex,
knownAddresses: GroupedAddress[] | undefined,
purpose: KeyPurpose | undefined
knownAddresses: GroupedAddress[] | undefined
): CertCredentialsType => {
const knownAddress = knownAddresses?.find((address) =>
areStringsEqualInConstantTime(Cardano.RewardAccount.toHash(address.rewardAccount), stakeKeyHash)
Expand All @@ -31,7 +30,7 @@ const getCertCredentials = (
!!rewardAddress &&
areNumbersEqualInConstantTime(rewardAddress?.getPaymentCredential().type, Cardano.CredentialType.KeyHash)
) {
const path = util.stakeKeyPathFromGroupedAddress({ address: knownAddress, purpose });
const path = util.stakeKeyPathFromGroupedAddress(knownAddress);
return path ? { path } : { keyHash: stakeKeyHash };
}
return {
Expand All @@ -49,7 +48,7 @@ const getPoolOperatorKeyPath = (
context: TrezorTxTransformerContext
): BIP32Path | null => {
const knownAddress = context?.knownAddresses.find((address) => address.rewardAccount === operator);
return util.stakeKeyPathFromGroupedAddress({ address: knownAddress, purpose: context.purpose });
return util.stakeKeyPathFromGroupedAddress(knownAddress);
};

export const getStakeAddressCertificate: Transform<
Expand All @@ -59,8 +58,7 @@ export const getStakeAddressCertificate: Transform<
> = (certificate, context) => {
const credentials = getCertCredentials(
certificate.stakeCredential.hash as unknown as Crypto.Ed25519KeyHashHex,
context?.knownAddresses,
context?.purpose
context?.knownAddresses
);
const certificateType =
certificate.__typename === Cardano.CertificateType.StakeRegistration
Expand All @@ -84,8 +82,7 @@ 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?.purpose
context?.knownAddresses
);
return {
keyHash: credentials.keyHash,
Expand Down
24 changes: 13 additions & 11 deletions packages/hardware-trezor/src/transformers/keyPaths.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BIP32Path } from '@cardano-sdk/crypto';
import { Cardano } from '@cardano-sdk/core';
import { GroupedAddress, KeyPurpose, TxInId, util } from '@cardano-sdk/key-management';
import { GroupedAddress, 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. */
Expand All @@ -9,22 +9,24 @@ export const resolvePaymentKeyPathForTxIn = (
context?: TrezorTxTransformerContext
): BIP32Path | undefined => {
if (!context) return;
const txInKeyPath = context?.txInKeyPathMap[TxInId(txIn)];
if (txInKeyPath) {
return util.accountKeyDerivationPathToBip32Path(context.accountIndex, txInKeyPath);
}

const txInKeyPath = context.txInKeyPathMap[TxInId(txIn)];

if (!txInKeyPath || txInKeyPath.role === undefined || txInKeyPath.index === undefined) return;
const utxoKeyPath = {
...txInKeyPath,
purpose: context.purpose
};

return util.accountKeyDerivationPathToBip32Path(context.accountIndex, utxoKeyPath);
};

export const resolveStakeKeyPath = (
rewardAddress: Cardano.RewardAddress,
knownAddresses: GroupedAddress[],
purpose: KeyPurpose
knownAddresses: GroupedAddress[]
): BIP32Path | null => {
const knownAddress = knownAddresses.find(
({ rewardAccount }) => rewardAccount === rewardAddress.toAddress().toBech32()
);
return util.stakeKeyPathFromGroupedAddress({
address: knownAddress,
purpose
});
return util.stakeKeyPathFromGroupedAddress(knownAddress);
};
13 changes: 4 additions & 9 deletions packages/hardware-trezor/src/transformers/requiredSigners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,16 @@ export const toRequiredSigner: Transform<
const paymentCredential = Cardano.Address.fromBech32(address.address)?.asBase()?.getPaymentCredential().hash;
return !!paymentCredential && areStringsEqualInConstantTime(paymentCredential.toString(), keyHash);
});
const purpose = context?.purpose;

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

const paymentKeyPath =
paymentCredKnownAddress && purpose
? util.paymentKeyPathFromGroupedAddress({ address: paymentCredKnownAddress, purpose })
: null;
const stakeKeyPath =
stakeCredKnownAddress && purpose
? util.stakeKeyPathFromGroupedAddress({ address: stakeCredKnownAddress, purpose })
: null;
const paymentKeyPath = paymentCredKnownAddress
? util.paymentKeyPathFromGroupedAddress(paymentCredKnownAddress)
: null;
const stakeKeyPath = stakeCredKnownAddress ? util.stakeKeyPathFromGroupedAddress(stakeCredKnownAddress) : null;

if (paymentKeyPath) {
return {
Expand Down
7 changes: 3 additions & 4 deletions packages/hardware-trezor/src/transformers/txOut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ const toDestination: Transform<Cardano.TxOut, TrezorTxOutputDestination, TrezorT
context
) => {
const knownAddress = context?.knownAddresses.find((address: GroupedAddress) => address.address === txOut.address);
const purpose = knownAddress?.purpose;

if (!knownAddress || !purpose) {
if (!knownAddress) {
return {
address: txOut.address
};
}

const paymentPath = util.paymentKeyPathFromGroupedAddress({ address: knownAddress, purpose });
const stakingPath = util.stakeKeyPathFromGroupedAddress({ address: knownAddress, purpose });
const paymentPath = util.paymentKeyPathFromGroupedAddress(knownAddress);
const stakingPath = util.stakeKeyPathFromGroupedAddress(knownAddress);

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

Expand Down
Loading

0 comments on commit 152a69f

Please sign in to comment.