Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cip-1854 support to keyagent #1301

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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/));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an odd change for this PR, was this test flaky?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it was. It fails locally as it receives this error message "Unexpected token 'e', "test" is not valid JSON". Maybe updating the expected error message to match the received message would work better than regex? wdyt?

it('throws if value is not an object', () =>
expect(() => validateFuzzyOptions('"test"')).toThrow('must be an object'));
it('throws without threshold', () =>
Expand Down
7 changes: 5 additions & 2 deletions packages/e2e/src/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ keyManagementFactory.register('inMemory', async (params: any): Promise<CreateKey
accountIndex: params.accountIndex,
chainId: params.chainId,
getPassphrase: async () => Buffer.from(params.passphrase),
mnemonicWords
mnemonicWords,
purpose: params.purpose
},
dependencies
)
Expand All @@ -220,7 +221,8 @@ keyManagementFactory.register('ledger', async (params: any): Promise<CreateKeyAg
accountIndex: params.accountIndex,
chainId: params.chainId,
communicationType: CommunicationType.Node,
deviceConnection
deviceConnection,
purpose: params.purpose
},
dependencies
);
Expand All @@ -238,6 +240,7 @@ keyManagementFactory.register(
{
accountIndex: params.accountIndex,
chainId: params.chainId,
purpose: params.purpose,
trezorConfig: {
communicationType: CommunicationType.Node,
manifest: {
Expand Down
5 changes: 3 additions & 2 deletions packages/e2e/src/scripts/mnemonic.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-floating-promises */
import * as Crypto from '@cardano-sdk/crypto';
import { AddressType, InMemoryKeyAgent, util } from '@cardano-sdk/key-management';
import { AddressType, InMemoryKeyAgent, KeyPurpose, util } from '@cardano-sdk/key-management';
import { localNetworkChainId } from '../util';

/** Generates a new set of Mnemonic words and prints them to the console. */
Expand All @@ -14,7 +14,8 @@ import { localNetworkChainId } from '../util';
{
chainId: localNetworkChainId,
getPassphrase: async () => Buffer.from(''),
mnemonicWords: mnemonicArray
mnemonicWords: mnemonicArray,
purpose: KeyPurpose.STANDARD
},
{
bip32Ed25519: new Crypto.SodiumBip32Ed25519(),
Expand Down
19 changes: 13 additions & 6 deletions packages/e2e/src/tools/multi-delegation-data-gen/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint-disable no-console, max-statements, max-params, @typescript-eslint/no-floating-promises */
import { ValueTransferConfig, configLoader } from './config';
import { Cardano } from '@cardano-sdk/core';
import { logger } from '@cardano-sdk/util-dev';
import { util } from '@cardano-sdk/key-management';

import { BaseWallet } from '@cardano-sdk/wallet';
import { Cardano } from '@cardano-sdk/core';
import { Files, Paths } from './files';
import {
KeyAgentFactoryProps,
Expand All @@ -16,8 +17,7 @@ import {
} from '../../../';
import { Observable, filter, firstValueFrom, map } from 'rxjs';
import { TaskResult, TerminalProgressMonitor } from './terminal-progress-monitor';
import { logger } from '@cardano-sdk/util-dev';
import { util } from '@cardano-sdk/key-management';
import { ValueTransferConfig, configLoader } from './config';
import chalk from 'chalk';

/**
Expand Down Expand Up @@ -96,8 +96,15 @@ export const loadConfiguration = async (monitor: TerminalProgressMonitor) => {
export const waitForFundingWallet = async (monitor: TerminalProgressMonitor): Promise<BaseWallet> => {
monitor.startTask('Waiting for funding wallet to be ready.');

const fundingWallet = (await getWallet({ env, idx: 0, logger, name: 'Funding wallet', polling: { interval: 500 } }))
.wallet;
const fundingWallet = (
await getWallet({
env,
idx: 0,
logger,
name: 'Funding wallet',
polling: { interval: 500 }
})
).wallet;

await walletReady(fundingWallet);

Expand Down
5 changes: 3 additions & 2 deletions packages/e2e/src/util/createMockKeyAgent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Bip32PublicKeyHex, SodiumBip32Ed25519 } from '@cardano-sdk/crypto';
import { Cardano } from '@cardano-sdk/core';
import { GroupedAddress, KeyAgent, KeyAgentType } from '@cardano-sdk/key-management';
import { GroupedAddress, KeyAgent, KeyAgentType, KeyPurpose } from '@cardano-sdk/key-management';

const accountIndex = 0;
const chainId = Cardano.ChainIds.Preview;
Expand All @@ -23,7 +23,8 @@ export const createMockKeyAgent = (deriveAddressesReturn: GroupedAddress[] = [])
accountIndex,
chainId,
encryptedRootPrivateKeyBytes: [],
extendedAccountPublicKey
extendedAccountPublicKey,
purpose: KeyPurpose.STANDARD
},
signBlob: jest.fn(),
signTransaction: jest.fn()
Expand Down
3 changes: 2 additions & 1 deletion packages/e2e/src/util/handle-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Asset, Cardano, Handle, Serialization, metadatum, nativeScriptPolicyId,
import { FinalizeTxProps, ObservableWallet } from '@cardano-sdk/wallet';
import { HexBlob } from '@cardano-sdk/util';
import { InitializeTxProps } from '@cardano-sdk/tx-construction';
import { KeyAgent, KeyRole, util as keyManagementUtil } from '@cardano-sdk/key-management';
import { KeyAgent, KeyPurpose, KeyRole, util as keyManagementUtil } from '@cardano-sdk/key-management';
import { firstValueFrom } from 'rxjs';
import { readFile } from 'fs/promises';
import { submitAndConfirm } from './util';
Expand Down Expand Up @@ -79,6 +79,7 @@ export const createHandleMetadata = (handlePolicyId: string, handleNames: string
export const createHandlePolicy = async (keyAgent: KeyAgent) => {
const derivationPath = {
index: 0,
purpose: KeyPurpose.STANDARD,
role: KeyRole.External
};
const pubKey = await keyAgent.derivePublicKey(derivationPath);
Expand Down
9 changes: 6 additions & 3 deletions packages/e2e/src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
timeout
} from 'rxjs';
import { FAST_OPERATION_TIMEOUT_DEFAULT, SYNC_TIMEOUT_DEFAULT } from '../defaults';
import { InMemoryKeyAgent, TransactionSigner } from '@cardano-sdk/key-management';
import { InMemoryKeyAgent, KeyPurpose, TransactionSigner } from '@cardano-sdk/key-management';
import { InitializeTxProps } from '@cardano-sdk/tx-construction';
import { TestWallet, networkInfoProviderFactory } from '../factories';
import { getEnv, walletVariables } from '../environment';
Expand Down Expand Up @@ -228,17 +228,20 @@ export const submitCertificate = async (certificate: Cardano.Certificate, wallet
* @param mnemonics The random set of mnemonics.
* @param genesis Network genesis parameters
* @param bip32Ed25519 The Ed25519 cryptography implementation.
* @param purpose they key derivation purpose of either 1852 or 1854
*/
export const createStandaloneKeyAgent = async (
mnemonics: string[],
genesis: Cardano.CompactGenesis,
bip32Ed25519: Crypto.Bip32Ed25519
bip32Ed25519: Crypto.Bip32Ed25519,
purpose: KeyPurpose
) =>
await InMemoryKeyAgent.fromBip39MnemonicWords(
{
chainId: genesis,
getPassphrase: async () => Buffer.from(''),
mnemonicWords: mnemonics
mnemonicWords: mnemonics,
purpose
},
{ bip32Ed25519, logger }
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AddressType, GroupedAddress, util } from '@cardano-sdk/key-management';
import { AddressType, GroupedAddress, KeyPurpose, util } from '@cardano-sdk/key-management';
import { AddressesModel, WalletVars } from './types';
import { Cardano } from '@cardano-sdk/core';
import { FunctionHook } from '../artillery';
Expand All @@ -12,6 +12,7 @@ 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
@@ -1,16 +1,8 @@
/* eslint-disable import/imports-first */
import * as dotenv from 'dotenv';
import path from 'path';

// This line must come before loading the env, to configure the location of the .env file
dotenv.config({ path: path.join(__dirname, '../../../.env') });

import { BaseWallet, createPersonalWallet } from '@cardano-sdk/wallet';
import { Logger } from 'ts-log';
import { bufferCount, bufferTime, from, mergeAll, tap } from 'rxjs';
import { logger } from '@cardano-sdk/util-dev';

import { Bip32Account, util } from '@cardano-sdk/key-management';
import { Logger } from 'ts-log';
import {
MeasurementUtil,
assetProviderFactory,
Expand All @@ -27,6 +19,12 @@ import {
waitForWalletStateSettle,
walletVariables
} from '../../../src';
import { bufferCount, bufferTime, from, mergeAll, tap } from 'rxjs';
import { logger } from '@cardano-sdk/util-dev';
import path from 'path';

// This line must come before loading the env, to configure the location of the .env file
dotenv.config({ path: path.join(__dirname, '../../../.env') });

// Example call that creates 5000 wallets in 10 minutes:
// VIRTUAL_USERS_GENERATE_DURATION=600 VIRTUAL_USERS_COUNT=5000 yarn load-test-custom:wallet-init
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e/test/local-network/register-pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import {
walletReady,
walletVariables
} from '../../src';
import { firstValueFrom } from 'rxjs';
import { logger } from '@cardano-sdk/util-dev';

import * as Crypto from '@cardano-sdk/crypto';
import { AddressType, KeyRole } from '@cardano-sdk/key-management';
import { firstValueFrom } from 'rxjs';

const env = getEnv(walletVariables);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
AddressType,
GroupedAddress,
InMemoryKeyAgent,
KeyPurpose,
KeyRole
} from '@cardano-sdk/key-management';
import {
Expand Down Expand Up @@ -33,6 +34,7 @@ const DUMMY_HEX_BYTES =

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

Expand Down Expand Up @@ -215,7 +217,7 @@ export class MultiSigWallet {
body: multiSigTx.getTransaction().body,
hash: multiSigTx.getTransaction().id
},
{ knownAddresses: [this.#address], txInKeyPathMap: {} },
{ knownAddresses: [this.#address], purpose: KeyPurpose.MULTI_SIG, txInKeyPathMap: {} },
{ additionalKeyPaths: [DERIVATION_PATH] }
);

Expand Down Expand Up @@ -411,6 +413,7 @@ 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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Crypto from '@cardano-sdk/crypto';
import { BaseWallet } from '@cardano-sdk/wallet';
import { Cardano, EraSummary, StakePoolProvider, createSlotEpochCalc } from '@cardano-sdk/core';
import { InMemoryKeyAgent, KeyRole } from '@cardano-sdk/key-management';
import { InMemoryKeyAgent, KeyPurpose, KeyRole } from '@cardano-sdk/key-management';
import { MultiSigTx } from './MultiSigTx';
import { MultiSigWallet } from './MultiSigWallet';
import { Observable, filter, firstValueFrom, map, take } from 'rxjs';
Expand Down Expand Up @@ -34,6 +34,7 @@ const charlotteMnemonics =

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

Expand Down Expand Up @@ -63,7 +64,7 @@ const fundMultiSigWallet = async (sendingWallet: BaseWallet, address: Cardano.Pa
const getKeyAgent = async (mnemonics: string, faucetWallet: BaseWallet, bip32Ed25519: Crypto.Bip32Ed25519) => {
const genesis = await firstValueFrom(faucetWallet.genesisParameters$);

const keyAgent = await createStandaloneKeyAgent(mnemonics.split(' '), genesis, bip32Ed25519);
const keyAgent = await createStandaloneKeyAgent(mnemonics.split(' '), genesis, bip32Ed25519, KeyPurpose.STANDARD);

const pubKey = await keyAgent.derivePublicKey(DERIVATION_PATH);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable sonarjs/no-duplicate-string */
import { BaseWallet } from '@cardano-sdk/wallet';
import { Cardano, metadatum } from '@cardano-sdk/core';
import { KeyAgent, TransactionSigner } from '@cardano-sdk/key-management';
import { KeyAgent, KeyPurpose, TransactionSigner } from '@cardano-sdk/key-management';
import {
bip32Ed25519Factory,
burnTokens,
Expand Down Expand Up @@ -50,7 +50,8 @@ describe('Ada handle', () => {
keyAgent = await createStandaloneKeyAgent(
env.KEY_MANAGEMENT_PARAMS.mnemonic.split(' '),
await firstValueFrom(wallet.genesisParameters$),
await bip32Ed25519Factory.create(env.KEY_MANAGEMENT_PARAMS.bip32Ed25519, null, logger)
await bip32Ed25519Factory.create(env.KEY_MANAGEMENT_PARAMS.bip32Ed25519, null, logger),
KeyPurpose.STANDARD
);
({ policyScript, policySigner, policyId } = await createHandlePolicy(keyAgent));
const handleProviderPolicyId = await getHandlePolicyId(
Expand Down
6 changes: 4 additions & 2 deletions packages/e2e/test/wallet_epoch_0/PersonalWallet/mint.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseWallet, FinalizeTxProps } from '@cardano-sdk/wallet';
import { Cardano, nativeScriptPolicyId } from '@cardano-sdk/core';
import { InitializeTxProps } from '@cardano-sdk/tx-construction';
import { KeyRole, util } from '@cardano-sdk/key-management';
import { KeyPurpose, KeyRole, util } from '@cardano-sdk/key-management';
import {
bip32Ed25519Factory,
burnTokens,
Expand Down Expand Up @@ -40,11 +40,13 @@ describe('PersonalWallet/mint', () => {
const aliceKeyAgent = await createStandaloneKeyAgent(
env.KEY_MANAGEMENT_PARAMS.mnemonic.split(' '),
genesis,
await bip32Ed25519Factory.create(env.KEY_MANAGEMENT_PARAMS.bip32Ed25519, null, logger)
await bip32Ed25519Factory.create(env.KEY_MANAGEMENT_PARAMS.bip32Ed25519, null, logger),
KeyPurpose.STANDARD
);

const derivationPath = {
index: 2,
purpose: KeyPurpose.STANDARD,
role: KeyRole.External
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable sonarjs/no-duplicate-string */
import { AddressType, GroupedAddress, util } from '@cardano-sdk/key-management';
import { AddressType, GroupedAddress, KeyPurpose, util } from '@cardano-sdk/key-management';
import { BaseWallet } from '@cardano-sdk/wallet';
import { Cardano } from '@cardano-sdk/core';
import {
Expand Down Expand Up @@ -43,7 +43,8 @@ describe('PersonalWallet/multiAddress', () => {
const multiAddressKeyAgent = await createStandaloneKeyAgent(
mnemonics,
genesis,
await bip32Ed25519Factory.create(env.KEY_MANAGEMENT_PARAMS.bip32Ed25519, null, logger)
await bip32Ed25519Factory.create(env.KEY_MANAGEMENT_PARAMS.bip32Ed25519, null, logger),
KeyPurpose.STANDARD
);

let txBuilder = wallet.createTxBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { BaseWallet, FinalizeTxProps } from '@cardano-sdk/wallet';
import { Cardano, nativeScriptPolicyId } from '@cardano-sdk/core';
import { InitializeTxProps } from '@cardano-sdk/tx-construction';
import { KeyRole, util } from '@cardano-sdk/key-management';
import { KeyPurpose, KeyRole, util } from '@cardano-sdk/key-management';
import {
bip32Ed25519Factory,
burnTokens,
Expand Down Expand Up @@ -48,21 +48,25 @@ describe('PersonalWallet/multisignature', () => {
const aliceKeyAgent = await createStandaloneKeyAgent(
env.KEY_MANAGEMENT_PARAMS.mnemonic.split(' '),
genesis,
bip32Ed25519
bip32Ed25519,
KeyPurpose.MULTI_SIG
);
const bobKeyAgent = await createStandaloneKeyAgent(
env.KEY_MANAGEMENT_PARAMS.mnemonic.split(' '),
genesis,
bip32Ed25519
bip32Ed25519,
KeyPurpose.MULTI_SIG
);

const aliceDerivationPath = {
index: 2,
purpose: KeyPurpose.MULTI_SIG,
role: KeyRole.External
};

const bobDerivationPath = {
index: 3,
purpose: KeyPurpose.MULTI_SIG,
role: KeyRole.External
};

Expand Down
Loading
Loading