Skip to content

Commit

Permalink
fix(web-extension): signing coordinator sign with 1854 purpose account (
Browse files Browse the repository at this point in the history
  • Loading branch information
mkazlauskas authored Jul 25, 2024
1 parent 533620d commit dec29f0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,16 @@ export class SigningCoordinator<WalletMetadata extends {}, AccountMetadata exten
return reject(new WrongTargetError('Not expecting sign requests at this time'));
}
const account = request.requestContext.wallet.accounts.find(
({ accountIndex }) => accountIndex === request.requestContext.accountIndex
({ accountIndex, purpose = KeyPurpose.STANDARD }) =>
accountIndex === request.requestContext.accountIndex && request.requestContext.purpose === purpose
);

if (!account) {
return reject(new errors.ProofGenerationError(`Account not found: ${request.requestContext.accountIndex}`));
return reject(
new errors.ProofGenerationError(
`Account not found: index=${request.requestContext.accountIndex}, purpose=${request.requestContext.purpose}`
)
);
}

const commonRequestProps = {
Expand Down Expand Up @@ -192,13 +197,13 @@ export class SigningCoordinator<WalletMetadata extends {}, AccountMetadata exten
chainId: request.requestContext.chainId,
communicationType: this.#hwOptions.communicationType,
extendedAccountPublicKey: account.extendedAccountPublicKey,
purpose: KeyPurpose.STANDARD
purpose: account.purpose || KeyPurpose.STANDARD
})
: this.#keyAgentFactory.Trezor({
accountIndex: request.requestContext.accountIndex,
chainId: request.requestContext.chainId,
extendedAccountPublicKey: account.extendedAccountPublicKey,
purpose: KeyPurpose.STANDARD,
purpose: account.purpose || KeyPurpose.STANDARD,
trezorConfig: this.#hwOptions
})
).catch((error) => throwMaybeWrappedWithNoRejectError(error, options)),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
AccountKeyDerivationPath,
KeyPurpose,
SignBlobResult,
SignDataContext,
SignTransactionContext,
Expand All @@ -13,6 +14,7 @@ import { Observable } from 'rxjs';
export type RequestContext<WalletMetadata extends {}, AccountMetadata extends {}> = {
wallet: AnyBip32Wallet<WalletMetadata, AccountMetadata>;
accountIndex: number;
purpose: KeyPurpose;
chainId: Cardano.ChainId;
};

Expand Down
4 changes: 4 additions & 0 deletions packages/web-extension/src/walletManager/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Crypto from '@cardano-sdk/crypto';
import {
AccountKeyDerivationPath,
AsyncKeyAgent,
KeyPurpose,
SignDataContext,
SignTransactionContext,
TransactionSigner,
Expand Down Expand Up @@ -283,6 +284,7 @@ const createScriptWitness = async <WalletMetadata extends {}, AccountMetadata ex
{
accountIndex: signer.accountIndex,
chainId,
purpose: signer.purpose,
wallet: signerWallet
}
);
Expand Down Expand Up @@ -315,6 +317,7 @@ export const buildBip32Witnesser = <WalletMetadata extends { name: string }, Acc
{
accountIndex: accountIndex!,
chainId,
purpose: KeyPurpose.STANDARD,
wallet
}
),
Expand All @@ -341,6 +344,7 @@ export const buildBip32Witnesser = <WalletMetadata extends { name: string }, Acc
{
accountIndex: accountIndex!,
chainId,
purpose: KeyPurpose.STANDARD,
wallet
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
AccountKeyDerivationPath,
CommunicationType,
InMemoryKeyAgent,
KeyPurpose,
KeyRole,
SignBlobResult,
SignDataContext,
Expand All @@ -11,7 +12,14 @@ import {
import { Cardano, TxCBOR } from '@cardano-sdk/core';
import { Ed25519PublicKeyHex, Ed25519SignatureHex, Hash28ByteBase16 } from '@cardano-sdk/crypto';
import { HexBlob } from '@cardano-sdk/util';
import { InMemoryWallet, KeyAgentFactory, SigningCoordinator, WalletType, WrongTargetError } from '../../src';
import {
InMemoryWallet,
KeyAgentFactory,
RequestContext,
SigningCoordinator,
WalletType,
WrongTargetError
} from '../../src';
import { createAccount } from './util';
import { dummyLogger } from 'ts-log';
import { firstValueFrom } from 'rxjs';
Expand All @@ -21,7 +29,7 @@ describe('SigningCoordinator', () => {
let keyAgentFactory: jest.Mocked<KeyAgentFactory>;
let keyAgent: jest.Mocked<InMemoryKeyAgent>;
const wallet: InMemoryWallet<{}, {}> = {
accounts: [createAccount(0, 0)],
accounts: [createAccount(0, 0), createAccount(0, 0, KeyPurpose.MULTI_SIG)],
encryptedSecrets: {
keyMaterial: HexBlob('abc'),
rootPrivateKeyBytes: HexBlob('123')
Expand All @@ -30,9 +38,10 @@ describe('SigningCoordinator', () => {
type: WalletType.InMemory,
walletId: Hash28ByteBase16('ad63f855e831d937457afc52a21a7f351137e4a9fff26c217817335a')
};
const requestContext = {
const requestContext: RequestContext<{}, {}> = {
accountIndex: 0,
chainId: Cardano.ChainIds.Preprod,
purpose: KeyPurpose.STANDARD,
wallet
};
let passphrase: Uint8Array;
Expand Down Expand Up @@ -102,6 +111,19 @@ describe('SigningCoordinator', () => {
expect(passphrase).toEqual(new Uint8Array([0, 0, 0]));
});

it('creates a key agent with requested purpose account', async () => {
keyAgent.signTransaction.mockResolvedValueOnce(signatures);
const reqEmitted = firstValueFrom(signingCoordinator.transactionWitnessRequest$);
const signed = signingCoordinator.signTransaction(
{ signContext, tx },
{ ...requestContext, purpose: KeyPurpose.MULTI_SIG }
);
const req = await reqEmitted;
await req.sign(passphrase);
await signed;
expect(keyAgentFactory.InMemory).toBeCalledWith(expect.objectContaining({ purpose: KeyPurpose.MULTI_SIG }));
});

it('rejects with AuthenticationError when subscriber calls reject()', async () => {
const reqEmitted = firstValueFrom(signingCoordinator.transactionWitnessRequest$);
const signed = signingCoordinator.signTransaction({ signContext, tx }, requestContext);
Expand Down

0 comments on commit dec29f0

Please sign in to comment.