Skip to content

Commit

Permalink
feat!: personal wallet now takes a Bip32 address manager and a witnes…
Browse files Browse the repository at this point in the history
…ser instead of key agent

BREAKING CHANGES: Personal wallet now take as dependencies an Bip32Ed25519AddressManager and a Witnesser
  • Loading branch information
AngelCastilloB committed Nov 28, 2023
1 parent a133f8c commit 8308bf1
Show file tree
Hide file tree
Showing 49 changed files with 506 additions and 183 deletions.
11 changes: 10 additions & 1 deletion packages/e2e/src/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,16 @@ export const getWallet = async (props: GetWalletProps) => {
? () => Promise.resolve(keyAgent)
: await keyManagementFactory.create(env.KEY_MANAGEMENT_PROVIDER, keyManagementParams, logger),
createWallet: async (asyncKeyAgent: AsyncKeyAgent) =>
new PersonalWallet({ name, polling }, { ...providers, keyAgent: asyncKeyAgent, logger, stores }),
new PersonalWallet(
{ name, polling },
{
...providers,
addressManager: util.createBip32Ed25519AddressManager(asyncKeyAgent),
logger,
stores,
witnesser: util.createBip32Ed25519Witnesser(asyncKeyAgent)
}
),
logger
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
waitForWalletStateSettle,
walletVariables
} from '../../../src';
import { util } from '@cardano-sdk/key-management';

// 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 Expand Up @@ -84,7 +85,15 @@ const createWallet = async (accountIndex: number): Promise<PersonalWallet> => {
measurementUtil.addMeasureMarker(MeasureTarget.keyAgent, accountIndex);

measurementUtil.addStartMarker(MeasureTarget.wallet, accountIndex);
const wallet = new PersonalWallet({ name: `Wallet ${accountIndex}` }, { ...providers, keyAgent, logger });
const wallet = new PersonalWallet(
{ name: `Wallet ${accountIndex}` },
{
...providers,
addressManager: util.createBip32Ed25519AddressManager(keyAgent),
logger,
witnesser: util.createBip32Ed25519Witnesser(keyAgent)
}
);
walletUtil.initialize(wallet);
return wallet;
};
Expand Down
17 changes: 9 additions & 8 deletions packages/e2e/test/local-network/register-pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Cardano } from '@cardano-sdk/core';
import {
KeyAgentFactoryProps,
TestWallet,
bip32Ed25519Factory,
getEnv,
getWallet,
submitCertificate,
Expand Down Expand Up @@ -41,6 +42,7 @@ const wallet2Params: KeyAgentFactoryProps = {
describe('local-network/register-pool', () => {
let wallet1: TestWallet;
let wallet2: TestWallet;
let bip32Ed25519: Crypto.Bip32Ed25519;

beforeAll(async () => {
jest.setTimeout(180_000);
Expand All @@ -64,6 +66,7 @@ describe('local-network/register-pool', () => {

await waitForWalletStateSettle(wallet1.wallet);
await waitForWalletStateSettle(wallet2.wallet);
bip32Ed25519 = await bip32Ed25519Factory.create(env.KEY_MANAGEMENT_PARAMS.bip32Ed25519, null, logger);
});

afterAll(() => {
Expand All @@ -76,18 +79,17 @@ describe('local-network/register-pool', () => {

await walletReady(wallet);

const poolKeyAgent = wallet.keyAgent;
const poolAddressManager = wallet.addressManager;

const poolPubKey = await poolKeyAgent.derivePublicKey({
const poolPubKey = await poolAddressManager.derivePublicKey({
index: 0,
role: KeyRole.External
});

const bip32Ed25519 = await poolKeyAgent.getBip32Ed25519();
const poolKeyHash = await bip32Ed25519.getPubKeyHash(poolPubKey);
const poolId = Cardano.PoolId.fromKeyHash(poolKeyHash);
const poolRewardAccount = (
await poolKeyAgent.deriveAddress(
await poolAddressManager.deriveAddress(
{
index: 0,
type: AddressType.External
Expand Down Expand Up @@ -165,18 +167,17 @@ describe('local-network/register-pool', () => {

await walletReady(wallet);

const poolKeyAgent = wallet.keyAgent;
const poolAddressManager = wallet.addressManager;

const poolPubKey = await poolKeyAgent.derivePublicKey({
const poolPubKey = await poolAddressManager.derivePublicKey({
index: 0,
role: KeyRole.External
});

const bip32Ed25519 = await poolKeyAgent.getBip32Ed25519();
const poolKeyHash = await bip32Ed25519.getPubKeyHash(poolPubKey);
const poolId = Cardano.PoolId.fromKeyHash(poolKeyHash);
const poolRewardAccount = (
await poolKeyAgent.deriveAddress(
await poolAddressManager.deriveAddress(
{
index: 0,
type: AddressType.External
Expand Down
11 changes: 7 additions & 4 deletions packages/e2e/test/long-running/cache-invalidation.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import * as Crypto from '@cardano-sdk/crypto';
import { AddressType, KeyRole } from '@cardano-sdk/key-management';
import { Cardano } from '@cardano-sdk/core';
import {
KeyAgentFactoryProps,
TestWallet,
bip32Ed25519Factory,
getEnv,
getTxConfirmationEpoch,
getWallet,
Expand All @@ -23,6 +25,7 @@ const vrf = Cardano.VrfVkHex('2ee5a4c423224bb9c42107fc18a60556d6a83cec1d9dd37a71
describe('cache invalidation', () => {
let testProviderServer: Docker.Container;
let wallet1: TestWallet;
let bip32Ed25519: Crypto.Bip32Ed25519;

beforeAll(async () => {
const port = await getRandomPort();
Expand Down Expand Up @@ -90,6 +93,7 @@ describe('cache invalidation', () => {
});

await waitForWalletStateSettle(wallet1.wallet);
bip32Ed25519 = await bip32Ed25519Factory.create(env.KEY_MANAGEMENT_PARAMS.bip32Ed25519, null, logger);
});

afterAll(async () => {
Expand All @@ -103,18 +107,17 @@ describe('cache invalidation', () => {

await walletReady(wallet);

const poolKeyAgent = wallet.keyAgent;
const poolAddressManager = wallet.addressManager;

const poolPubKey = await poolKeyAgent.derivePublicKey({
const poolPubKey = await poolAddressManager.derivePublicKey({
index: 0,
role: KeyRole.External
});

const bip32Ed25519 = await poolKeyAgent.getBip32Ed25519();
const poolKeyHash = await bip32Ed25519.getPubKeyHash(poolPubKey);
const poolId = Cardano.PoolId.fromKeyHash(poolKeyHash);
const poolRewardAccount = (
await poolKeyAgent.deriveAddress(
await poolAddressManager.deriveAddress(
{
index: 0,
type: AddressType.External
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ import { MultiSigWallet } from './MultiSigWallet';
import { Observable, filter, firstValueFrom, map, take } from 'rxjs';
import { PersonalWallet } from '@cardano-sdk/wallet';
import { TrackerSubject } from '@cardano-sdk/util-rxjs';
import { createStandaloneKeyAgent, getEnv, getWallet, waitForEpoch, walletReady, walletVariables } from '../../../src';
import {
bip32Ed25519Factory,
createStandaloneKeyAgent,
getEnv,
getWallet,
waitForEpoch,
walletReady,
walletVariables
} from '../../../src';
import { isNotNil } from '@cardano-sdk/util';
import { logger } from '@cardano-sdk/util-dev';

Expand Down Expand Up @@ -52,14 +60,10 @@ const fundMultiSigWallet = async (sendingWallet: PersonalWallet, address: Cardan
await sendingWallet.submitTx(signedTx);
};

const getKeyAgent = async (mnemonics: string, faucetWallet: PersonalWallet) => {
const getKeyAgent = async (mnemonics: string, faucetWallet: PersonalWallet, bip32Ed25519: Crypto.Bip32Ed25519) => {
const genesis = await firstValueFrom(faucetWallet.genesisParameters$);

const keyAgent = await createStandaloneKeyAgent(
mnemonics.split(' '),
genesis,
await faucetWallet.keyAgent.getBip32Ed25519()
);
const keyAgent = await createStandaloneKeyAgent(mnemonics.split(' '), genesis, bip32Ed25519);

const pubKey = await keyAgent.derivePublicKey(DERIVATION_PATH);

Expand Down Expand Up @@ -139,9 +143,15 @@ describe('multi signature wallet', () => {

beforeAll(async () => {
await initializeFaucet();
({ keyAgent: aliceKeyAgent, pubKey: alicePubKey } = await getKeyAgent(aliceMnemonics, faucetWallet));
({ keyAgent: bobKeyAgent, pubKey: bobPubKey } = await getKeyAgent(bobMnemonics, faucetWallet));
({ keyAgent: charlotteKeyAgent, pubKey: charlottePubKey } = await getKeyAgent(charlotteMnemonics, faucetWallet));
const bip32Ed25519 = await bip32Ed25519Factory.create(env.KEY_MANAGEMENT_PARAMS.bip32Ed25519, null, logger);

({ keyAgent: aliceKeyAgent, pubKey: alicePubKey } = await getKeyAgent(aliceMnemonics, faucetWallet, bip32Ed25519));
({ keyAgent: bobKeyAgent, pubKey: bobPubKey } = await getKeyAgent(bobMnemonics, faucetWallet, bip32Ed25519));
({ keyAgent: charlotteKeyAgent, pubKey: charlottePubKey } = await getKeyAgent(
charlotteMnemonics,
faucetWallet,
bip32Ed25519
));

faucetAddress = (await firstValueFrom(faucetWallet.addresses$))[0].address;

Expand Down
9 changes: 6 additions & 3 deletions packages/e2e/test/wallet/PersonalWallet/delegation.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* eslint-disable max-statements */
import * as Crypto from '@cardano-sdk/crypto';
import { BigIntMath } from '@cardano-sdk/util';
import { Cardano } from '@cardano-sdk/core';
import { ObservableWallet, PersonalWallet } from '@cardano-sdk/wallet';
import {
TX_TIMEOUT_DEFAULT,
TestWallet,
bip32Ed25519Factory,
firstValueFromTimed,
getEnv,
getWallet,
Expand All @@ -27,6 +29,7 @@ const getWalletStateSnapshot = async (wallet: ObservableWallet) => {
const utxoTotal = await firstValueFrom(wallet.utxo.total$);
const utxoAvailable = await firstValueFrom(wallet.utxo.available$);
const rewardsBalance = await firstValueFrom(wallet.balance.rewardAccounts.rewards$);

return {
balance: { available: balanceAvailable, deposit, total: balanceTotal },
epoch: epoch.epochNo,
Expand Down Expand Up @@ -54,13 +57,15 @@ const waitForTx = async (wallet: ObservableWallet, hash: Cardano.TransactionId)
describe('PersonalWallet/delegation', () => {
let wallet1: TestWallet;
let wallet2: TestWallet;
let bip32Ed25519: Crypto.Bip32Ed25519;

beforeAll(async () => {
jest.setTimeout(180_000);
wallet1 = await getWallet({ env, idx: 0, logger, name: 'Test Wallet 1', polling: { interval: 500 } });
wallet2 = await getWallet({ env, idx: 1, logger, name: 'Test Wallet 2', polling: { interval: 500 } });

await Promise.all([waitForWalletStateSettle(wallet1.wallet), waitForWalletStateSettle(wallet2.wallet)]);
bip32Ed25519 = await bip32Ed25519Factory.create(env.KEY_MANAGEMENT_PARAMS.bip32Ed25519, null, logger);
});

afterAll(() => {
Expand Down Expand Up @@ -164,9 +169,7 @@ describe('PersonalWallet/delegation', () => {
expect(tx1ConfirmedState.rewardAccount.delegatee?.currentEpoch?.id).toEqual(poolId);
}

const stakeKeyHash = await (
await sourceWallet.keyAgent.getBip32Ed25519()
).getPubKeyHash(tx1ConfirmedState.publicStakeKey.publicStakeKey);
const stakeKeyHash = await bip32Ed25519.getPubKeyHash(tx1ConfirmedState.publicStakeKey.publicStakeKey);
expect(stakeKeyHash).toEqual(Cardano.RewardAccount.toHash(tx1ConfirmedState.rewardAccount.address));
expect(tx1ConfirmedState.publicStakeKey.keyStatus).toBe(Cardano.StakeKeyStatus.Registered);

Expand Down
3 changes: 2 additions & 1 deletion packages/e2e/test/wallet/PersonalWallet/handle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Cardano, metadatum } from '@cardano-sdk/core';
import { KeyAgent, TransactionSigner } from '@cardano-sdk/key-management';
import { PersonalWallet } from '@cardano-sdk/wallet';
import {
bip32Ed25519Factory,
burnTokens,
coinsRequiredByHandleMint,
createHandleMetadata,
Expand Down Expand Up @@ -49,7 +50,7 @@ describe('Ada handle', () => {
keyAgent = await createStandaloneKeyAgent(
env.KEY_MANAGEMENT_PARAMS.mnemonic.split(' '),
await firstValueFrom(wallet.genesisParameters$),
await wallet.keyAgent.getBip32Ed25519()
await bip32Ed25519Factory.create(env.KEY_MANAGEMENT_PARAMS.bip32Ed25519, null, logger)
);
({ policyScript, policySigner, policyId } = await createHandlePolicy(keyAgent));
const handleProviderPolicyId = await getHandlePolicyId(
Expand Down
3 changes: 2 additions & 1 deletion packages/e2e/test/wallet/PersonalWallet/mint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FinalizeTxProps, PersonalWallet } from '@cardano-sdk/wallet';
import { InitializeTxProps } from '@cardano-sdk/tx-construction';
import { KeyRole, util } from '@cardano-sdk/key-management';
import {
bip32Ed25519Factory,
burnTokens,
createStandaloneKeyAgent,
getEnv,
Expand Down Expand Up @@ -36,7 +37,7 @@ describe('PersonalWallet/mint', () => {
const aliceKeyAgent = await createStandaloneKeyAgent(
env.KEY_MANAGEMENT_PARAMS.mnemonic.split(' '),
genesis,
await wallet.keyAgent.getBip32Ed25519()
await bip32Ed25519Factory.create(env.KEY_MANAGEMENT_PARAMS.bip32Ed25519, null, logger)
);

const derivationPath = {
Expand Down
3 changes: 2 additions & 1 deletion packages/e2e/test/wallet/PersonalWallet/multiAddress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AddressType, GroupedAddress, util } from '@cardano-sdk/key-management';
import { Cardano } from '@cardano-sdk/core';
import {
KeyAgentFactoryProps,
bip32Ed25519Factory,
createStandaloneKeyAgent,
firstValueFromTimed,
getWallet,
Expand Down Expand Up @@ -42,7 +43,7 @@ describe('PersonalWallet/multiAddress', () => {
const multiAddressKeyAgent = await createStandaloneKeyAgent(
mnemonics,
genesis,
await wallet.keyAgent.getBip32Ed25519()
await bip32Ed25519Factory.create(env.KEY_MANAGEMENT_PARAMS.bip32Ed25519, null, logger)
);

let txBuilder = wallet.createTxBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { FinalizeTxProps, PersonalWallet } from '@cardano-sdk/wallet';
import { InitializeTxProps } from '@cardano-sdk/tx-construction';
import { KeyRole, util } from '@cardano-sdk/key-management';
import {
bip32Ed25519Factory,
burnTokens,
createStandaloneKeyAgent,
getEnv,
Expand Down Expand Up @@ -34,15 +35,16 @@ describe('PersonalWallet/multisignature', () => {

const genesis = await firstValueFrom(wallet.genesisParameters$);

const bip32Ed25519 = await bip32Ed25519Factory.create(env.KEY_MANAGEMENT_PARAMS.bip32Ed25519, null, logger);
const aliceKeyAgent = await createStandaloneKeyAgent(
env.KEY_MANAGEMENT_PARAMS.mnemonic.split(' '),
genesis,
await wallet.keyAgent.getBip32Ed25519()
bip32Ed25519
);
const bobKeyAgent = await createStandaloneKeyAgent(
env.KEY_MANAGEMENT_PARAMS.mnemonic.split(' '),
genesis,
await wallet.keyAgent.getBip32Ed25519()
bip32Ed25519
);

const aliceDerivationPath = {
Expand Down
3 changes: 2 additions & 1 deletion packages/e2e/test/wallet/PersonalWallet/nft.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Assets, FinalizeTxProps, PersonalWallet } from '@cardano-sdk/wallet';
import { InitializeTxProps } from '@cardano-sdk/tx-construction';
import { KeyRole, TransactionSigner, util } from '@cardano-sdk/key-management';
import {
bip32Ed25519Factory,
burnTokens,
createStandaloneKeyAgent,
firstValueFromTimed,
Expand Down Expand Up @@ -60,7 +61,7 @@ describe('PersonalWallet.assets/nft', () => {
const keyAgent = await createStandaloneKeyAgent(
env.KEY_MANAGEMENT_PARAMS.mnemonic.split(' '),
genesis,
await wallet.keyAgent.getBip32Ed25519()
await bip32Ed25519Factory.create(env.KEY_MANAGEMENT_PARAMS.bip32Ed25519, null, logger)
);

const derivationPath = {
Expand Down
Loading

0 comments on commit 8308bf1

Please sign in to comment.