Skip to content

Commit

Permalink
fix: extend deriveAccountPrivateKey parameter to accept purpose
Browse files Browse the repository at this point in the history
  • Loading branch information
greatertomi committed May 28, 2024
1 parent 83e010c commit fac71bd
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 224 deletions.
47 changes: 0 additions & 47 deletions packages/core/src/Cardano/Address/ScriptAddress.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/core/src/Cardano/Address/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ export * from './PaymentAddress';
export * from './PointerAddress';
export * from './RewardAccount';
export * from './RewardAddress';
export * from './ScriptAddress';
38 changes: 0 additions & 38 deletions packages/core/test/Cardano/Address/ScriptAddress.test.ts

This file was deleted.

41 changes: 1 addition & 40 deletions packages/key-management/src/Bip32Account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import {
AccountKeyDerivationPath,
AsyncKeyAgent,
GroupedAddress,
KeyPurpose,
KeyRole,
MultiSigAccountKeyDerivationPath,
MultiSigKeyRole,
SharedAddress,
SharedWalletAddressDerivationPath
KeyRole
} from './types';
import { Cardano } from '@cardano-sdk/core';
import { Hash28ByteBase16 } from '@cardano-sdk/crypto';
Expand Down Expand Up @@ -38,11 +33,6 @@ export class Bip32Account {
return key.toRawKey();
}

async deriveMultiSigPublicKey({ role, index }: MultiSigAccountKeyDerivationPath) {
const key = await this.extendedAccountPublicKey.derive([KeyPurpose.MULTI_SIG, role, index]);
return key.toRawKey();
}

async deriveAddress(
paymentKeyDerivationPath: AccountAddressDerivationPath,
stakeKeyDerivationIndex: number
Expand Down Expand Up @@ -82,35 +72,6 @@ export class Bip32Account {
};
}

async deriveSharedWalletAddress({
paymentKeyDerivationIndex,
stakeKeyDerivationIndex
}: SharedWalletAddressDerivationPath): Promise<SharedAddress> {
const derivedPublicPaymentKey = await this.deriveMultiSigPublicKey({
index: paymentKeyDerivationIndex,
role: MultiSigKeyRole.Payment
});
const publicPaymentKeyHash = await derivedPublicPaymentKey.hash();

const publicStakeKey = await this.deriveMultiSigPublicKey({
index: stakeKeyDerivationIndex,
role: MultiSigKeyRole.Stake
});
const publicStakeKeyHash = await publicStakeKey.hash();

const address = Cardano.BaseAddress.fromCredentials(
this.chainId.networkId,
{ hash: Hash28ByteBase16(publicPaymentKeyHash.hex()), type: Cardano.CredentialType.ScriptHash },
{ hash: Hash28ByteBase16(publicStakeKeyHash.hex()), type: Cardano.CredentialType.ScriptHash }
).toAddress();

return {
accountIndex: this.accountIndex,
address: Cardano.ScriptAddress(address.toBech32()),
networkId: this.chainId.networkId
};
}

/**
* Creates a new instance of the Bip32Ed25519AddressManager class.
*
Expand Down
6 changes: 5 additions & 1 deletion packages/key-management/src/InMemoryKeyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
KeyAgentDependencies,
KeyAgentType,
KeyPair,
KeyPurpose,
SerializableInMemoryKeyAgentData,
SignBlobResult,
SignTransactionContext,
Expand Down Expand Up @@ -37,6 +38,7 @@ export interface FromBip39MnemonicWordsProps {
mnemonic2ndFactorPassphrase?: string;
getPassphrase: GetPassphrase;
accountIndex?: number;
purpose?: KeyPurpose;
}

const getPassphraseRethrowTypedError = async (getPassphrase: GetPassphrase) => {
Expand Down Expand Up @@ -89,7 +91,8 @@ export class InMemoryKeyAgent extends KeyAgentBase implements KeyAgent {
getPassphrase,
mnemonicWords,
mnemonic2ndFactorPassphrase = '',
accountIndex = 0
accountIndex = 0,
purpose = KeyPurpose.STANDARD
}: FromBip39MnemonicWordsProps,
dependencies: KeyAgentDependencies
): Promise<InMemoryKeyAgent> {
Expand All @@ -103,6 +106,7 @@ export class InMemoryKeyAgent extends KeyAgentBase implements KeyAgent {
const accountPrivateKey = await deriveAccountPrivateKey({
accountIndex,
bip32Ed25519: dependencies.bip32Ed25519,
purpose,
rootPrivateKey
});

Expand Down
11 changes: 4 additions & 7 deletions packages/key-management/src/KeyAgentBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import {
GroupedAddress,
KeyAgent,
KeyAgentDependencies,
MultiSigAccountKeyDerivationPath,
SerializableKeyAgentData,
SharedAddress,
SharedWalletAddressDerivationPath,
SignBlobResult,
SignTransactionContext,
SignTransactionOptions
Expand Down Expand Up @@ -60,18 +57,18 @@ export abstract class KeyAgentBase implements KeyAgent {
return this.#account.deriveAddress(paymentKeyDerivationPath, stakeKeyDerivationIndex);
}

async deriveSharedWalletAddress({
/* async deriveSharedWalletAddress({
paymentKeyDerivationIndex,
stakeKeyDerivationIndex
}: SharedWalletAddressDerivationPath): Promise<SharedAddress> {
return this.#account.deriveSharedWalletAddress({ paymentKeyDerivationIndex, stakeKeyDerivationIndex });
}
} */

async derivePublicKey(derivationPath: AccountKeyDerivationPath): Promise<Crypto.Ed25519PublicKeyHex> {
return (await this.#account.derivePublicKey(derivationPath)).hex();
}

async deriveMultiSigPublicKey(derivationPath: MultiSigAccountKeyDerivationPath): Promise<Crypto.Ed25519PublicKeyHex> {
/* async deriveMultiSigPublicKey(derivationPath: MultiSigAccountKeyDerivationPath): Promise<Crypto.Ed25519PublicKeyHex> {
return (await this.#account.deriveMultiSigPublicKey(derivationPath)).hex();
}
} */
}
14 changes: 2 additions & 12 deletions packages/key-management/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,8 @@ export enum KeyRole {
DRep = 3
}

export enum MultiSigKeyRole {
Payment = 0,
Stake = 2
}

export enum KeyPurpose {
INDIVIDUAL = 1852,
STANDARD = 1852,
MULTI_SIG = 1854
}

Expand All @@ -53,11 +48,6 @@ export interface AccountKeyDerivationPath {
index: number;
}

export interface MultiSigAccountKeyDerivationPath {
role: MultiSigKeyRole;
index: number;
}

/** Internal = change address & External = receipt address */
export enum AddressType {
/** Change address */
Expand Down Expand Up @@ -102,7 +92,7 @@ export interface GroupedAddress {

export interface SharedAddress {
networkId: Cardano.NetworkId;
address: Cardano.ScriptAddress;
address: Cardano.PaymentAddress;
accountIndex: number;
}

Expand Down
16 changes: 13 additions & 3 deletions packages/key-management/src/util/key.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import * as Crypto from '@cardano-sdk/crypto';
import { AccountKeyDerivationPath, CardanoKeyConst, Ed25519KeyPair, GroupedAddress, KeyPair, KeyRole } from '../types';
import {
AccountKeyDerivationPath,
CardanoKeyConst,
Ed25519KeyPair,
GroupedAddress,
KeyPair,
KeyPurpose,
KeyRole
} from '../types';
import { BIP32Path } from '@cardano-sdk/crypto';

export const harden = (num: number): number => 0x80_00_00_00 + num;
Expand All @@ -26,15 +34,17 @@ export interface DeriveAccountPrivateKeyProps {
rootPrivateKey: Crypto.Bip32PrivateKeyHex;
accountIndex: number;
bip32Ed25519: Crypto.Bip32Ed25519;
purpose?: KeyPurpose;
}

export const deriveAccountPrivateKey = async ({
rootPrivateKey,
accountIndex,
bip32Ed25519
bip32Ed25519,
purpose = KeyPurpose.STANDARD
}: DeriveAccountPrivateKeyProps): Promise<Crypto.Bip32PrivateKeyHex> =>
await bip32Ed25519.derivePrivateKey(rootPrivateKey, [
harden(CardanoKeyConst.PURPOSE),
harden(purpose),
harden(CardanoKeyConst.COIN_TYPE),
harden(accountIndex)
]);
Expand Down
Loading

0 comments on commit fac71bd

Please sign in to comment.