@aztec/cli-wallet 0.0.0-test.1 → 0.0.1-commit.017a351
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -0
- package/dest/bin/index.d.ts +2 -1
- package/dest/bin/index.js +58 -26
- package/dest/cmds/authorize_action.d.ts +4 -3
- package/dest/cmds/authorize_action.d.ts.map +1 -1
- package/dest/cmds/authorize_action.js +11 -5
- package/dest/cmds/bridge_fee_juice.d.ts +4 -4
- package/dest/cmds/bridge_fee_juice.d.ts.map +1 -1
- package/dest/cmds/bridge_fee_juice.js +10 -8
- package/dest/cmds/check_tx.d.ts +5 -3
- package/dest/cmds/check_tx.d.ts.map +1 -1
- package/dest/cmds/check_tx.js +174 -6
- package/dest/cmds/create_account.d.ts +10 -7
- package/dest/cmds/create_account.d.ts.map +1 -1
- package/dest/cmds/create_account.js +74 -38
- package/dest/cmds/create_authwit.d.ts +4 -3
- package/dest/cmds/create_authwit.d.ts.map +1 -1
- package/dest/cmds/create_authwit.js +6 -6
- package/dest/cmds/deploy.d.ts +8 -4
- package/dest/cmds/deploy.d.ts.map +1 -1
- package/dest/cmds/deploy.js +108 -59
- package/dest/cmds/deploy_account.d.ts +7 -8
- package/dest/cmds/deploy_account.d.ts.map +1 -1
- package/dest/cmds/deploy_account.js +70 -36
- package/dest/cmds/get_fee_juice_balance.d.ts +5 -0
- package/dest/cmds/get_fee_juice_balance.d.ts.map +1 -0
- package/dest/cmds/get_fee_juice_balance.js +27 -0
- package/dest/cmds/import_test_accounts.d.ts +3 -3
- package/dest/cmds/import_test_accounts.d.ts.map +1 -1
- package/dest/cmds/import_test_accounts.js +6 -9
- package/dest/cmds/index.d.ts +3 -3
- package/dest/cmds/index.d.ts.map +1 -1
- package/dest/cmds/index.js +99 -115
- package/dest/cmds/profile.d.ts +8 -0
- package/dest/cmds/profile.d.ts.map +1 -0
- package/dest/cmds/profile.js +28 -0
- package/dest/cmds/register_contract.d.ts +7 -3
- package/dest/cmds/register_contract.d.ts.map +1 -1
- package/dest/cmds/register_contract.js +20 -7
- package/dest/cmds/register_sender.d.ts +4 -3
- package/dest/cmds/register_sender.d.ts.map +1 -1
- package/dest/cmds/send.d.ts +9 -9
- package/dest/cmds/send.d.ts.map +1 -1
- package/dest/cmds/send.js +54 -27
- package/dest/cmds/simulate.d.ts +7 -3
- package/dest/cmds/simulate.d.ts.map +1 -1
- package/dest/cmds/simulate.js +26 -20
- package/dest/storage/wallet_db.d.ts +12 -19
- package/dest/storage/wallet_db.d.ts.map +1 -1
- package/dest/storage/wallet_db.js +63 -55
- package/dest/utils/authorizations.d.ts +6 -0
- package/dest/utils/authorizations.d.ts.map +1 -0
- package/dest/utils/authorizations.js +28 -0
- package/dest/utils/cli_wallet_and_node_wrapper.d.ts +12 -0
- package/dest/utils/cli_wallet_and_node_wrapper.d.ts.map +1 -0
- package/dest/utils/cli_wallet_and_node_wrapper.js +25 -0
- package/dest/utils/constants.d.ts +4 -0
- package/dest/utils/constants.d.ts.map +1 -0
- package/dest/utils/constants.js +7 -0
- package/dest/utils/ecdsa.d.ts +1 -3
- package/dest/utils/ecdsa.d.ts.map +1 -1
- package/dest/utils/options/fees.d.ts +23 -28
- package/dest/utils/options/fees.d.ts.map +1 -1
- package/dest/utils/options/fees.js +112 -148
- package/dest/utils/options/index.d.ts +1 -1
- package/dest/utils/options/options.d.ts +9 -5
- package/dest/utils/options/options.d.ts.map +1 -1
- package/dest/utils/options/options.js +26 -14
- package/dest/utils/profiling.d.ts +5 -0
- package/dest/utils/profiling.d.ts.map +1 -0
- package/dest/utils/profiling.js +68 -0
- package/dest/utils/wallet.d.ts +44 -0
- package/dest/utils/wallet.d.ts.map +1 -0
- package/dest/utils/wallet.js +263 -0
- package/package.json +31 -23
- package/src/bin/index.ts +63 -33
- package/src/cmds/authorize_action.ts +16 -5
- package/src/cmds/bridge_fee_juice.ts +16 -12
- package/src/cmds/check_tx.ts +212 -5
- package/src/cmds/create_account.ts +89 -45
- package/src/cmds/create_authwit.ts +9 -5
- package/src/cmds/deploy.ts +115 -71
- package/src/cmds/deploy_account.ts +84 -36
- package/src/cmds/get_fee_juice_balance.ts +37 -0
- package/src/cmds/import_test_accounts.ts +7 -11
- package/src/cmds/index.ts +312 -222
- package/src/cmds/profile.ts +48 -0
- package/src/cmds/register_contract.ts +28 -5
- package/src/cmds/register_sender.ts +3 -2
- package/src/cmds/send.ts +59 -25
- package/src/cmds/simulate.ts +37 -24
- package/src/storage/wallet_db.ts +67 -68
- package/src/utils/authorizations.ts +51 -0
- package/src/utils/cli_wallet_and_node_wrapper.ts +35 -0
- package/src/utils/constants.ts +4 -0
- package/src/utils/options/fees.ts +169 -210
- package/src/utils/options/options.ts +41 -19
- package/src/utils/profiling.ts +172 -0
- package/src/utils/wallet.ts +330 -0
- package/dest/cmds/add_authwit.d.ts +0 -4
- package/dest/cmds/add_authwit.d.ts.map +0 -1
- package/dest/cmds/add_authwit.js +0 -4
- package/dest/cmds/cancel_tx.d.ts +0 -11
- package/dest/cmds/cancel_tx.d.ts.map +0 -1
- package/dest/cmds/cancel_tx.js +0 -38
- package/dest/utils/accounts.d.ts +0 -11
- package/dest/utils/accounts.d.ts.map +0 -1
- package/dest/utils/accounts.js +0 -87
- package/dest/utils/pxe_wrapper.d.ts +0 -10
- package/dest/utils/pxe_wrapper.d.ts.map +0 -1
- package/dest/utils/pxe_wrapper.js +0 -21
- package/src/cmds/add_authwit.ts +0 -13
- package/src/cmds/cancel_tx.ts +0 -62
- package/src/utils/accounts.ts +0 -102
- package/src/utils/pxe_wrapper.ts +0 -26
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import type { LogFn } from '@aztec/foundation/log';
|
|
2
|
+
import type { PrivateExecutionStep } from '@aztec/stdlib/kernel';
|
|
3
|
+
import type { ProvingStats, ProvingTimings, SimulationStats, SimulationTimings } from '@aztec/stdlib/tx';
|
|
4
|
+
|
|
5
|
+
import { format } from 'util';
|
|
6
|
+
|
|
7
|
+
const FN_NAME_PADDING = 60;
|
|
8
|
+
const COLUMN_MIN_WIDTH = 13;
|
|
9
|
+
const COLUMN_MAX_WIDTH = 15;
|
|
10
|
+
|
|
11
|
+
const ORACLE_NAME_PADDING = 50;
|
|
12
|
+
|
|
13
|
+
export function printProfileResult(
|
|
14
|
+
stats: ProvingStats | SimulationStats,
|
|
15
|
+
log: LogFn,
|
|
16
|
+
printOracles: boolean = false,
|
|
17
|
+
executionSteps?: PrivateExecutionStep[],
|
|
18
|
+
) {
|
|
19
|
+
log(format('\nPer circuit breakdown:\n'));
|
|
20
|
+
log(
|
|
21
|
+
format(
|
|
22
|
+
' ',
|
|
23
|
+
'Function name'.padEnd(FN_NAME_PADDING),
|
|
24
|
+
'Time'.padStart(COLUMN_MIN_WIDTH).padEnd(COLUMN_MAX_WIDTH),
|
|
25
|
+
executionSteps ? 'Gates'.padStart(COLUMN_MIN_WIDTH).padEnd(COLUMN_MAX_WIDTH) : '',
|
|
26
|
+
executionSteps ? 'Subtotal'.padStart(COLUMN_MIN_WIDTH).padEnd(COLUMN_MAX_WIDTH) : '',
|
|
27
|
+
),
|
|
28
|
+
);
|
|
29
|
+
log(
|
|
30
|
+
format(
|
|
31
|
+
''.padEnd(
|
|
32
|
+
FN_NAME_PADDING +
|
|
33
|
+
COLUMN_MAX_WIDTH +
|
|
34
|
+
COLUMN_MAX_WIDTH +
|
|
35
|
+
(executionSteps ? COLUMN_MAX_WIDTH + COLUMN_MAX_WIDTH : 0),
|
|
36
|
+
'-',
|
|
37
|
+
),
|
|
38
|
+
),
|
|
39
|
+
);
|
|
40
|
+
let acc = 0;
|
|
41
|
+
let biggest: PrivateExecutionStep | undefined = executionSteps?.[0];
|
|
42
|
+
|
|
43
|
+
const timings = stats.timings;
|
|
44
|
+
timings.perFunction.forEach((fn, i) => {
|
|
45
|
+
const currentExecutionStep = executionSteps?.[i];
|
|
46
|
+
if (currentExecutionStep && biggest && currentExecutionStep.gateCount! > biggest.gateCount!) {
|
|
47
|
+
biggest = currentExecutionStep;
|
|
48
|
+
}
|
|
49
|
+
acc += currentExecutionStep ? currentExecutionStep.gateCount! : 0;
|
|
50
|
+
|
|
51
|
+
log(
|
|
52
|
+
format(
|
|
53
|
+
' - ',
|
|
54
|
+
fn.functionName.padEnd(FN_NAME_PADDING),
|
|
55
|
+
`${fn.time.toFixed(2)}ms`.padStart(COLUMN_MIN_WIDTH).padEnd(COLUMN_MAX_WIDTH),
|
|
56
|
+
currentExecutionStep
|
|
57
|
+
? currentExecutionStep.gateCount!.toLocaleString().padStart(COLUMN_MIN_WIDTH).padEnd(COLUMN_MAX_WIDTH)
|
|
58
|
+
: '',
|
|
59
|
+
currentExecutionStep ? acc.toLocaleString().padStart(COLUMN_MAX_WIDTH) : '',
|
|
60
|
+
),
|
|
61
|
+
);
|
|
62
|
+
if (printOracles && fn.oracles) {
|
|
63
|
+
log('');
|
|
64
|
+
for (const [oracleName, { times }] of Object.entries(fn.oracles)) {
|
|
65
|
+
const calls = times.length;
|
|
66
|
+
const min = Math.min(...times);
|
|
67
|
+
const max = Math.max(...times);
|
|
68
|
+
const total = times.reduce((acc, time) => acc + time, 0);
|
|
69
|
+
const avg = total / calls;
|
|
70
|
+
log(
|
|
71
|
+
format(
|
|
72
|
+
' ',
|
|
73
|
+
oracleName.padEnd(ORACLE_NAME_PADDING),
|
|
74
|
+
`${calls} calls`.padStart(COLUMN_MIN_WIDTH).padEnd(COLUMN_MAX_WIDTH),
|
|
75
|
+
`${total.toFixed(2)}ms`.padStart(COLUMN_MIN_WIDTH).padEnd(COLUMN_MAX_WIDTH),
|
|
76
|
+
`min: ${min.toFixed(2)}ms`.padStart(COLUMN_MIN_WIDTH).padEnd(COLUMN_MAX_WIDTH),
|
|
77
|
+
`avg: ${avg.toFixed(2)}ms`.padStart(COLUMN_MIN_WIDTH).padEnd(COLUMN_MAX_WIDTH),
|
|
78
|
+
`max: ${max.toFixed(2)}ms`.padStart(COLUMN_MIN_WIDTH).padEnd(COLUMN_MAX_WIDTH),
|
|
79
|
+
),
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
log('');
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
if (biggest) {
|
|
87
|
+
log(
|
|
88
|
+
format(
|
|
89
|
+
'\nTotal gates:',
|
|
90
|
+
acc.toLocaleString(),
|
|
91
|
+
`(Biggest circuit: ${biggest.functionName} -> ${biggest.gateCount!.toLocaleString()})`,
|
|
92
|
+
),
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (stats.nodeRPCCalls) {
|
|
97
|
+
log(format('\nRPC calls:\n'));
|
|
98
|
+
for (const [method, { times }] of Object.entries(stats.nodeRPCCalls.perMethod)) {
|
|
99
|
+
const calls = times.length;
|
|
100
|
+
const total = times.reduce((acc, time) => acc + time, 0);
|
|
101
|
+
const avg = total / calls;
|
|
102
|
+
const min = Math.min(...times);
|
|
103
|
+
const max = Math.max(...times);
|
|
104
|
+
log(
|
|
105
|
+
format(
|
|
106
|
+
method.padEnd(ORACLE_NAME_PADDING),
|
|
107
|
+
`${calls} calls`.padStart(COLUMN_MIN_WIDTH).padEnd(COLUMN_MAX_WIDTH),
|
|
108
|
+
`${total.toFixed(2)}ms`.padStart(COLUMN_MIN_WIDTH).padEnd(COLUMN_MAX_WIDTH),
|
|
109
|
+
`min: ${min.toFixed(2)}ms`.padStart(COLUMN_MIN_WIDTH).padEnd(COLUMN_MAX_WIDTH),
|
|
110
|
+
`avg: ${avg.toFixed(2)}ms`.padStart(COLUMN_MIN_WIDTH).padEnd(COLUMN_MAX_WIDTH),
|
|
111
|
+
`max: ${max.toFixed(2)}ms`.padStart(COLUMN_MIN_WIDTH).padEnd(COLUMN_MAX_WIDTH),
|
|
112
|
+
),
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const { roundTrips } = stats.nodeRPCCalls;
|
|
117
|
+
log(format('\nRound trips (actual blocking waits):\n'));
|
|
118
|
+
log(format('Round trips:'.padEnd(25), `${roundTrips.roundTrips}`.padStart(COLUMN_MAX_WIDTH)));
|
|
119
|
+
log(
|
|
120
|
+
format(
|
|
121
|
+
'Total blocking time:'.padEnd(25),
|
|
122
|
+
`${roundTrips.totalBlockingTime.toFixed(2)}ms`.padStart(COLUMN_MAX_WIDTH),
|
|
123
|
+
),
|
|
124
|
+
);
|
|
125
|
+
if (roundTrips.roundTrips > 0) {
|
|
126
|
+
const avgRoundTrip = roundTrips.totalBlockingTime / roundTrips.roundTrips;
|
|
127
|
+
log(format('Avg round trip:'.padEnd(25), `${avgRoundTrip.toFixed(2)}ms`.padStart(COLUMN_MAX_WIDTH)));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
log(format('\nSync time:'.padEnd(25), `${timings.sync?.toFixed(2)}ms`.padStart(16)));
|
|
132
|
+
log(
|
|
133
|
+
format(
|
|
134
|
+
'Private simulation time:'.padEnd(25),
|
|
135
|
+
`${timings.perFunction.reduce((acc, { time }) => acc + time, 0).toFixed(2)}ms`.padStart(COLUMN_MAX_WIDTH),
|
|
136
|
+
),
|
|
137
|
+
);
|
|
138
|
+
if ((timings as ProvingTimings).proving) {
|
|
139
|
+
log(
|
|
140
|
+
format(
|
|
141
|
+
'Proving time:'.padEnd(25),
|
|
142
|
+
`${(timings as ProvingTimings).proving?.toFixed(2)}ms`.padStart(COLUMN_MAX_WIDTH),
|
|
143
|
+
),
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
if ((timings as SimulationTimings).publicSimulation) {
|
|
147
|
+
log(
|
|
148
|
+
format(
|
|
149
|
+
'Public simulation time:'.padEnd(25),
|
|
150
|
+
`${(timings as SimulationTimings).publicSimulation?.toFixed(2)}ms`.padStart(COLUMN_MAX_WIDTH),
|
|
151
|
+
),
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if ((timings as SimulationTimings).validation) {
|
|
156
|
+
log(
|
|
157
|
+
format(
|
|
158
|
+
'Validation time:'.padEnd(25),
|
|
159
|
+
`${(timings as SimulationTimings).validation?.toFixed(2)}ms`.padStart(COLUMN_MAX_WIDTH),
|
|
160
|
+
),
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
log(
|
|
165
|
+
format(
|
|
166
|
+
'Total time:'.padEnd(25),
|
|
167
|
+
`${timings.total.toFixed(2)}ms`.padStart(COLUMN_MAX_WIDTH),
|
|
168
|
+
`(${timings.unaccounted.toFixed(2)}ms unaccounted)`,
|
|
169
|
+
),
|
|
170
|
+
);
|
|
171
|
+
log('\n');
|
|
172
|
+
}
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import { EcdsaRAccountContract, EcdsaRSSHAccountContract } from '@aztec/accounts/ecdsa';
|
|
2
|
+
import { SchnorrAccountContract } from '@aztec/accounts/schnorr';
|
|
3
|
+
import { StubEcdsaAccountContractArtifact, createStubEcdsaAccount } from '@aztec/accounts/stub/ecdsa';
|
|
4
|
+
import { StubSchnorrAccountContractArtifact, createStubSchnorrAccount } from '@aztec/accounts/stub/schnorr';
|
|
5
|
+
import { getIdentities } from '@aztec/accounts/utils';
|
|
6
|
+
import { type Account, type AccountContract, NO_FROM } from '@aztec/aztec.js/account';
|
|
7
|
+
import { type InteractionFeeOptions, getContractClassFromArtifact, getGasLimits } from '@aztec/aztec.js/contracts';
|
|
8
|
+
import type { AztecNode } from '@aztec/aztec.js/node';
|
|
9
|
+
import { AccountManager, type Aliased, type SimulateOptions } from '@aztec/aztec.js/wallet';
|
|
10
|
+
import { TxSimulationResultWithAppOffset } from '@aztec/aztec.js/wallet';
|
|
11
|
+
import type { DefaultAccountEntrypointOptions } from '@aztec/entrypoints/account';
|
|
12
|
+
import { DefaultEntrypoint } from '@aztec/entrypoints/default';
|
|
13
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
14
|
+
import type { LogFn } from '@aztec/foundation/log';
|
|
15
|
+
import type { NotesFilter } from '@aztec/pxe/client/lazy';
|
|
16
|
+
import type { PXEConfig } from '@aztec/pxe/config';
|
|
17
|
+
import type { PXE } from '@aztec/pxe/server';
|
|
18
|
+
import { createPXE, getPXEConfig } from '@aztec/pxe/server';
|
|
19
|
+
import { getStandardAuthRegistry } from '@aztec/standard-contracts/auth-registry';
|
|
20
|
+
import { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
21
|
+
import { deriveSigningKey } from '@aztec/stdlib/keys';
|
|
22
|
+
import { NoteDao } from '@aztec/stdlib/note';
|
|
23
|
+
import type { SimulationOverrides, TxExecutionRequest, TxProvingResult } from '@aztec/stdlib/tx';
|
|
24
|
+
import { ExecutionPayload, mergeExecutionPayloads } from '@aztec/stdlib/tx';
|
|
25
|
+
import { BaseWallet, type SimulateViaEntrypointOptions } from '@aztec/wallet-sdk/base-wallet';
|
|
26
|
+
|
|
27
|
+
import type { WalletDB } from '../storage/wallet_db.js';
|
|
28
|
+
import type { AccountType } from './constants.js';
|
|
29
|
+
import { extractECDSAPublicKeyFromBase64String } from './ecdsa.js';
|
|
30
|
+
import { printGasEstimates } from './options/fees.js';
|
|
31
|
+
|
|
32
|
+
export class CLIWallet extends BaseWallet {
|
|
33
|
+
private accountCache = new Map<string, Account>();
|
|
34
|
+
// Stub class ids, populated on wallet startup
|
|
35
|
+
// to avoid redundant work per simulation
|
|
36
|
+
private stubClassIds = new Map<AccountType, Fr>();
|
|
37
|
+
|
|
38
|
+
constructor(
|
|
39
|
+
pxe: PXE,
|
|
40
|
+
node: AztecNode,
|
|
41
|
+
private userLog: LogFn,
|
|
42
|
+
private db?: WalletDB,
|
|
43
|
+
) {
|
|
44
|
+
super(pxe, node);
|
|
45
|
+
this.cancellableTransactions = true;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
static async create(
|
|
49
|
+
node: AztecNode,
|
|
50
|
+
log: LogFn,
|
|
51
|
+
db?: WalletDB,
|
|
52
|
+
overridePXEConfig?: Partial<PXEConfig>,
|
|
53
|
+
): Promise<CLIWallet> {
|
|
54
|
+
const pxeConfig = Object.assign(getPXEConfig(), overridePXEConfig);
|
|
55
|
+
const pxe = await createPXE(node, pxeConfig);
|
|
56
|
+
const wallet = new CLIWallet(pxe, node, log, db);
|
|
57
|
+
await wallet.initStubClasses();
|
|
58
|
+
await wallet.registerAuthRegistry();
|
|
59
|
+
return wallet;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
private async registerAuthRegistry(): Promise<void> {
|
|
63
|
+
const { instance, artifact } = await getStandardAuthRegistry();
|
|
64
|
+
await this.pxe.registerContract({ instance, artifact });
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Hashes and registers the stub class for every supported account type with PXE, populating
|
|
69
|
+
* stubClassIds. Called on wallet initialization.
|
|
70
|
+
*/
|
|
71
|
+
private async initStubClasses(): Promise<void> {
|
|
72
|
+
const { id: schnorrClassId } = await getContractClassFromArtifact(StubSchnorrAccountContractArtifact);
|
|
73
|
+
await this.pxe.registerContractClass(StubSchnorrAccountContractArtifact);
|
|
74
|
+
|
|
75
|
+
// ecdsa stubs share the same class id
|
|
76
|
+
const { id: ecdsaClassId } = await getContractClassFromArtifact(StubEcdsaAccountContractArtifact);
|
|
77
|
+
await this.pxe.registerContractClass(StubEcdsaAccountContractArtifact);
|
|
78
|
+
|
|
79
|
+
this.stubClassIds.set('schnorr', schnorrClassId);
|
|
80
|
+
this.stubClassIds.set('ecdsasecp256k1', ecdsaClassId);
|
|
81
|
+
this.stubClassIds.set('ecdsasecp256r1', ecdsaClassId);
|
|
82
|
+
this.stubClassIds.set('ecdsasecp256r1ssh', ecdsaClassId);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
override async getAccounts(): Promise<Aliased<AztecAddress>[]> {
|
|
86
|
+
const accounts = (await this.db?.listAliases('accounts')) ?? [];
|
|
87
|
+
return Promise.resolve(
|
|
88
|
+
accounts.map(({ key, value }) => {
|
|
89
|
+
const alias = key.includes(':') ? key.slice(key.indexOf(':') + 1) : key;
|
|
90
|
+
return { alias, item: AztecAddress.fromString(value) };
|
|
91
|
+
}),
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
private async createCancellationTxExecutionRequest(
|
|
96
|
+
from: AztecAddress,
|
|
97
|
+
txNonce: Fr,
|
|
98
|
+
increasedFee: InteractionFeeOptions,
|
|
99
|
+
) {
|
|
100
|
+
const executionPayload = ExecutionPayload.empty();
|
|
101
|
+
const feeOptions = await this.completeFeeOptions({
|
|
102
|
+
from,
|
|
103
|
+
feePayer: executionPayload.feePayer,
|
|
104
|
+
gasSettings: increasedFee.gasSettings,
|
|
105
|
+
});
|
|
106
|
+
const feeExecutionPayload = await feeOptions.walletFeePaymentMethod?.getExecutionPayload();
|
|
107
|
+
const fromAccount = await this.getAccountFromAddress(from);
|
|
108
|
+
const chainInfo = await this.getChainInfo();
|
|
109
|
+
const executionOptions: DefaultAccountEntrypointOptions = {
|
|
110
|
+
txNonce,
|
|
111
|
+
cancellable: this.cancellableTransactions,
|
|
112
|
+
// If from is an address, feeOptions include the way the account contract should handle the fee payment
|
|
113
|
+
feePaymentMethodOptions: feeOptions.accountFeePaymentMethodOptions!,
|
|
114
|
+
};
|
|
115
|
+
return await fromAccount.createTxExecutionRequest(
|
|
116
|
+
feeExecutionPayload ?? executionPayload,
|
|
117
|
+
feeOptions.gasSettings,
|
|
118
|
+
chainInfo,
|
|
119
|
+
executionOptions,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async proveCancellationTx(
|
|
124
|
+
from: AztecAddress,
|
|
125
|
+
txNonce: Fr,
|
|
126
|
+
increasedFee: InteractionFeeOptions,
|
|
127
|
+
): Promise<TxProvingResult> {
|
|
128
|
+
const cancellationTxRequest = await this.createCancellationTxExecutionRequest(from, txNonce, increasedFee);
|
|
129
|
+
return await this.pxe.proveTx(cancellationTxRequest, { scopes: this.scopesFrom(from), senderForTags: from });
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
override async getAccountFromAddress(address: AztecAddress) {
|
|
133
|
+
let account: Account | undefined;
|
|
134
|
+
if (this.accountCache.has(address.toString())) {
|
|
135
|
+
return this.accountCache.get(address.toString())!;
|
|
136
|
+
} else {
|
|
137
|
+
const accountManager = await this.createOrRetrieveAccount(address);
|
|
138
|
+
account = await accountManager.getAccount();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (!account) {
|
|
142
|
+
throw new Error(`Account not found in wallet for address: ${address}`);
|
|
143
|
+
}
|
|
144
|
+
return account;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
private async createAccount(secret: Fr, salt: Fr, contract: AccountContract): Promise<AccountManager> {
|
|
148
|
+
const accountManager = await AccountManager.create(this, secret, contract, { salt });
|
|
149
|
+
|
|
150
|
+
const instance = accountManager.getInstance();
|
|
151
|
+
const artifact = await contract.getContractArtifact();
|
|
152
|
+
|
|
153
|
+
await this.registerContract(instance, artifact, secret);
|
|
154
|
+
this.accountCache.set(accountManager.address.toString(), await accountManager.getAccount());
|
|
155
|
+
return accountManager;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async createOrRetrieveAccount(
|
|
159
|
+
address?: AztecAddress,
|
|
160
|
+
secretKey?: Fr,
|
|
161
|
+
type: AccountType = 'schnorr',
|
|
162
|
+
salt?: Fr,
|
|
163
|
+
publicKey?: string,
|
|
164
|
+
): Promise<AccountManager> {
|
|
165
|
+
let account;
|
|
166
|
+
|
|
167
|
+
salt ??= Fr.ZERO;
|
|
168
|
+
|
|
169
|
+
if (this.db && address) {
|
|
170
|
+
({ type, secretKey, salt } = await this.db.retrieveAccount(address));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (!secretKey) {
|
|
174
|
+
throw new Error('Cannot retrieve/create wallet without secret key');
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
switch (type) {
|
|
178
|
+
case 'schnorr': {
|
|
179
|
+
account = await this.createAccount(secretKey, salt, new SchnorrAccountContract(deriveSigningKey(secretKey)));
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
case 'ecdsasecp256r1': {
|
|
183
|
+
account = await this.createAccount(
|
|
184
|
+
secretKey,
|
|
185
|
+
salt,
|
|
186
|
+
new EcdsaRAccountContract(deriveSigningKey(secretKey).toBuffer()),
|
|
187
|
+
);
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
case 'ecdsasecp256r1ssh': {
|
|
191
|
+
let publicSigningKey;
|
|
192
|
+
if (this.db && address) {
|
|
193
|
+
publicSigningKey = await this.db.retrieveAccountMetadata(address, 'publicSigningKey');
|
|
194
|
+
} else if (publicKey) {
|
|
195
|
+
const identities = await getIdentities();
|
|
196
|
+
const foundIdentity = identities.find(
|
|
197
|
+
identity => identity.type === 'ecdsa-sha2-nistp256' && identity.publicKey === publicKey,
|
|
198
|
+
);
|
|
199
|
+
if (!foundIdentity) {
|
|
200
|
+
throw new Error(`Identity for public key ${publicKey} not found in the SSH agent`);
|
|
201
|
+
}
|
|
202
|
+
publicSigningKey = extractECDSAPublicKeyFromBase64String(foundIdentity.publicKey);
|
|
203
|
+
} else {
|
|
204
|
+
throw new Error('Public key must be provided for ECDSA SSH account');
|
|
205
|
+
}
|
|
206
|
+
account = await this.createAccount(secretKey, salt, new EcdsaRSSHAccountContract(publicSigningKey));
|
|
207
|
+
break;
|
|
208
|
+
}
|
|
209
|
+
default: {
|
|
210
|
+
throw new Error(`Unsupported account type: ${type}`);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return account;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Creates a stub account that impersonates the given address, allowing kernelless simulations
|
|
219
|
+
* to bypass the account's authorization mechanisms via contract overrides.
|
|
220
|
+
* @param address - The address of the account to impersonate
|
|
221
|
+
* @returns The stub account, contract instance, and artifact for simulation
|
|
222
|
+
*/
|
|
223
|
+
private async getFakeAccountDataFor(address: AztecAddress) {
|
|
224
|
+
const originalAccount = await this.getAccountFromAddress(address);
|
|
225
|
+
const originalAddress = originalAccount.getCompleteAddress();
|
|
226
|
+
const contractInstance = await this.pxe.getContractInstance(originalAddress.address);
|
|
227
|
+
if (!contractInstance) {
|
|
228
|
+
throw new Error(`No contract instance found for address: ${originalAddress.address}`);
|
|
229
|
+
}
|
|
230
|
+
const { type } = await this.db!.retrieveAccount(address);
|
|
231
|
+
const stubAccount =
|
|
232
|
+
type === 'schnorr' ? createStubSchnorrAccount(originalAddress) : createStubEcdsaAccount(originalAddress);
|
|
233
|
+
const stubClassId = this.stubClassIds.get(type);
|
|
234
|
+
if (!stubClassId) {
|
|
235
|
+
throw new Error(
|
|
236
|
+
`Stub class for account type '${type}' was not registered at wallet init. This is a bug — initStubClasses should cover every supported AccountType.`,
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
const instance = { ...contractInstance, currentContractClassId: stubClassId };
|
|
240
|
+
return { account: stubAccount, instance };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
override async simulateTx(
|
|
244
|
+
executionPayload: ExecutionPayload,
|
|
245
|
+
opts: SimulateOptions,
|
|
246
|
+
): Promise<TxSimulationResultWithAppOffset> {
|
|
247
|
+
const simulationResults = await super.simulateTx(executionPayload, opts);
|
|
248
|
+
|
|
249
|
+
if (opts.fee?.estimateGas) {
|
|
250
|
+
const feeOptions = await this.completeFeeOptions({
|
|
251
|
+
from: opts.from,
|
|
252
|
+
feePayer: executionPayload.feePayer,
|
|
253
|
+
gasSettings: opts.fee?.gasSettings,
|
|
254
|
+
});
|
|
255
|
+
const limits = getGasLimits(simulationResults, opts.fee?.estimatedGasPadding);
|
|
256
|
+
printGasEstimates(feeOptions, limits, this.userLog);
|
|
257
|
+
}
|
|
258
|
+
return simulationResults;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Uses a stub account for kernelless simulation, bypassing real account authorization.
|
|
263
|
+
* Uses DefaultEntrypoint directly for NO_FROM transactions.
|
|
264
|
+
*/
|
|
265
|
+
protected override async simulateViaEntrypoint(
|
|
266
|
+
executionPayload: ExecutionPayload,
|
|
267
|
+
opts: SimulateViaEntrypointOptions,
|
|
268
|
+
): Promise<TxSimulationResultWithAppOffset> {
|
|
269
|
+
const { from, feeOptions, additionalScopes, sendMessagesAs } = opts;
|
|
270
|
+
const scopes = this.scopesFrom(from, additionalScopes);
|
|
271
|
+
const feeExecutionPayload = await feeOptions.walletFeePaymentMethod?.getExecutionPayload();
|
|
272
|
+
const finalExecutionPayload = feeExecutionPayload
|
|
273
|
+
? mergeExecutionPayloads([feeExecutionPayload, executionPayload])
|
|
274
|
+
: executionPayload;
|
|
275
|
+
const chainInfo = await this.getChainInfo();
|
|
276
|
+
|
|
277
|
+
let overrides: SimulationOverrides | undefined;
|
|
278
|
+
let txRequest: TxExecutionRequest;
|
|
279
|
+
if (from === NO_FROM) {
|
|
280
|
+
const entrypoint = new DefaultEntrypoint();
|
|
281
|
+
txRequest = await entrypoint.createTxExecutionRequest(finalExecutionPayload, feeOptions.gasSettings, chainInfo);
|
|
282
|
+
} else {
|
|
283
|
+
const { account, instance } = await this.getFakeAccountDataFor(from);
|
|
284
|
+
overrides = {
|
|
285
|
+
contracts: { [from.toString()]: { instance } },
|
|
286
|
+
};
|
|
287
|
+
const executionOptions: DefaultAccountEntrypointOptions = {
|
|
288
|
+
txNonce: Fr.random(),
|
|
289
|
+
cancellable: this.cancellableTransactions,
|
|
290
|
+
// If from is an address, feeOptions include the way the account contract should handle the fee payment
|
|
291
|
+
feePaymentMethodOptions: feeOptions.accountFeePaymentMethodOptions!,
|
|
292
|
+
};
|
|
293
|
+
txRequest = await account.createTxExecutionRequest(
|
|
294
|
+
finalExecutionPayload,
|
|
295
|
+
feeOptions.gasSettings,
|
|
296
|
+
chainInfo,
|
|
297
|
+
executionOptions,
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const result = await this.pxe.simulateTx(txRequest, {
|
|
302
|
+
simulatePublic: true,
|
|
303
|
+
skipFeeEnforcement: true,
|
|
304
|
+
skipTxValidation: true,
|
|
305
|
+
overrides,
|
|
306
|
+
scopes,
|
|
307
|
+
senderForTags: this.senderForTagsFrom(from, sendMessagesAs),
|
|
308
|
+
});
|
|
309
|
+
const appCallOffset = await this.computeAppCallOffset(from, feeOptions);
|
|
310
|
+
return TxSimulationResultWithAppOffset.fromResultAndOffset(result, appCallOffset);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// Exposed because of the `aztec-wallet get-tx` command. It has been decided that it's fine to keep around because
|
|
314
|
+
// this is just a CLI wallet.
|
|
315
|
+
getContracts(): Promise<AztecAddress[]> {
|
|
316
|
+
return this.pxe.getContracts();
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// Exposed because of the `aztec-wallet get-tx` command. It has been decided that it's fine to keep around because
|
|
320
|
+
// this is just a CLI wallet.
|
|
321
|
+
getNotes(filter: NotesFilter): Promise<NoteDao[]> {
|
|
322
|
+
return this.pxe.debug.getNotes(filter);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// Exposed because of the `aztec-wallet get-tx` command. It has been decided that it's fine to keep around because
|
|
326
|
+
// this is just a CLI wallet.
|
|
327
|
+
getContractArtifact(id: Fr) {
|
|
328
|
+
return this.pxe.getContractArtifact(id);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import type { AccountWalletWithSecretKey, AuthWitness, AztecAddress } from '@aztec/aztec.js';
|
|
2
|
-
import type { LogFn } from '@aztec/foundation/log';
|
|
3
|
-
export declare function addAuthwit(wallet: AccountWalletWithSecretKey, authwit: AuthWitness, authorizer: AztecAddress, log: LogFn): Promise<void>;
|
|
4
|
-
//# sourceMappingURL=add_authwit.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"add_authwit.d.ts","sourceRoot":"","sources":["../../src/cmds/add_authwit.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0BAA0B,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC7F,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAEnD,wBAAsB,UAAU,CAC9B,MAAM,EAAE,0BAA0B,EAClC,OAAO,EAAE,WAAW,EACpB,UAAU,EAAE,YAAY,EACxB,GAAG,EAAE,KAAK,iBAKX"}
|
package/dest/cmds/add_authwit.js
DELETED
package/dest/cmds/cancel_tx.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { type AccountWalletWithSecretKey, type FeePaymentMethod, type TxHash } from '@aztec/aztec.js';
|
|
2
|
-
import { Fr } from '@aztec/foundation/fields';
|
|
3
|
-
import type { LogFn } from '@aztec/foundation/log';
|
|
4
|
-
import { GasFees, GasSettings } from '@aztec/stdlib/gas';
|
|
5
|
-
export declare function cancelTx(wallet: AccountWalletWithSecretKey, { txHash, gasSettings: prevTxGasSettings, nonce, cancellable, }: {
|
|
6
|
-
txHash: TxHash;
|
|
7
|
-
gasSettings: GasSettings;
|
|
8
|
-
nonce: Fr;
|
|
9
|
-
cancellable: boolean;
|
|
10
|
-
}, paymentMethod: FeePaymentMethod, increasedFees: GasFees, maxFeesPerGas: GasFees | undefined, log: LogFn): Promise<void>;
|
|
11
|
-
//# sourceMappingURL=cancel_tx.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cancel_tx.d.ts","sourceRoot":"","sources":["../../src/cmds/cancel_tx.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,0BAA0B,EAAE,KAAK,gBAAgB,EAAU,KAAK,MAAM,EAAY,MAAM,iBAAiB,CAAC;AAExH,OAAO,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AAC9C,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEzD,wBAAsB,QAAQ,CAC5B,MAAM,EAAE,0BAA0B,EAClC,EACE,MAAM,EACN,WAAW,EAAE,iBAAiB,EAC9B,KAAK,EACL,WAAW,GACZ,EAAE;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,WAAW,CAAC;IAAC,KAAK,EAAE,EAAE,CAAC;IAAC,WAAW,EAAE,OAAO,CAAA;CAAE,EAChF,aAAa,EAAE,gBAAgB,EAC/B,aAAa,EAAE,OAAO,EACtB,aAAa,EAAE,OAAO,GAAG,SAAS,EAClC,GAAG,EAAE,KAAK,iBA4CX"}
|
package/dest/cmds/cancel_tx.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { SentTx, TxStatus } from '@aztec/aztec.js';
|
|
2
|
-
import { GasFees, GasSettings } from '@aztec/stdlib/gas';
|
|
3
|
-
export async function cancelTx(wallet, { txHash, gasSettings: prevTxGasSettings, nonce, cancellable }, paymentMethod, increasedFees, maxFeesPerGas, log) {
|
|
4
|
-
const receipt = await wallet.getTxReceipt(txHash);
|
|
5
|
-
if (receipt.status !== TxStatus.PENDING || !cancellable) {
|
|
6
|
-
log(`Transaction is in status ${receipt.status} and cannot be cancelled`);
|
|
7
|
-
return;
|
|
8
|
-
}
|
|
9
|
-
const maxPriorityFeesPerGas = new GasFees(prevTxGasSettings.maxPriorityFeesPerGas.feePerDaGas.add(increasedFees.feePerDaGas), prevTxGasSettings.maxPriorityFeesPerGas.feePerL2Gas.add(increasedFees.feePerL2Gas));
|
|
10
|
-
const fee = {
|
|
11
|
-
paymentMethod,
|
|
12
|
-
gasSettings: GasSettings.from({
|
|
13
|
-
...prevTxGasSettings,
|
|
14
|
-
maxPriorityFeesPerGas,
|
|
15
|
-
maxFeesPerGas: maxFeesPerGas ?? prevTxGasSettings.maxFeesPerGas
|
|
16
|
-
})
|
|
17
|
-
};
|
|
18
|
-
const txRequest = await wallet.createTxExecutionRequest({
|
|
19
|
-
calls: [],
|
|
20
|
-
fee,
|
|
21
|
-
nonce,
|
|
22
|
-
cancellable: true
|
|
23
|
-
});
|
|
24
|
-
const txSimulationResult = await wallet.simulateTx(txRequest, true);
|
|
25
|
-
const txProvingResult = await wallet.proveTx(txRequest, txSimulationResult.privateExecutionResult);
|
|
26
|
-
const sentTx = new SentTx(wallet, wallet.sendTx(txProvingResult.toTx()));
|
|
27
|
-
try {
|
|
28
|
-
await sentTx.wait();
|
|
29
|
-
log('Transaction has been cancelled');
|
|
30
|
-
const cancelReceipt = await sentTx.getReceipt();
|
|
31
|
-
log(` Tx fee: ${cancelReceipt.transactionFee}`);
|
|
32
|
-
log(` Status: ${cancelReceipt.status}`);
|
|
33
|
-
log(` Block number: ${cancelReceipt.blockNumber}`);
|
|
34
|
-
log(` Block hash: ${cancelReceipt.blockHash?.toString()}`);
|
|
35
|
-
} catch (err) {
|
|
36
|
-
log(`Could not cancel transaction\n ${err.message}`);
|
|
37
|
-
}
|
|
38
|
-
}
|
package/dest/utils/accounts.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { AccountManager, AccountWalletWithSecretKey } from '@aztec/aztec.js';
|
|
2
|
-
import { Fr } from '@aztec/foundation/fields';
|
|
3
|
-
import { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
4
|
-
import type { PXE } from '@aztec/stdlib/interfaces/client';
|
|
5
|
-
import type { WalletDB } from '../storage/wallet_db.js';
|
|
6
|
-
export declare const AccountTypes: readonly ["schnorr", "ecdsasecp256r1ssh", "ecdsasecp256k1"];
|
|
7
|
-
export type AccountType = (typeof AccountTypes)[number];
|
|
8
|
-
export declare function createOrRetrieveAccount(pxe: PXE, address?: AztecAddress, db?: WalletDB, secretKey?: Fr, type?: AccountType, salt?: Fr, publicKey?: string | undefined): Promise<AccountManager>;
|
|
9
|
-
export declare function addScopeToWallet(wallet: AccountWalletWithSecretKey, scope: AztecAddress, db?: WalletDB): Promise<void>;
|
|
10
|
-
export declare function getWalletWithScopes(account: AccountManager, db?: WalletDB): Promise<AccountWalletWithSecretKey>;
|
|
11
|
-
//# sourceMappingURL=accounts.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"accounts.d.ts","sourceRoot":"","sources":["../../src/utils/accounts.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,0BAA0B,EAAE,MAAM,iBAAiB,CAAC;AAClF,OAAO,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAC3D,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iCAAiC,CAAC;AAG3D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AAGxD,eAAO,MAAM,YAAY,6DAA8D,CAAC;AACxF,MAAM,MAAM,WAAW,GAAG,CAAC,OAAO,YAAY,CAAC,CAAC,MAAM,CAAC,CAAC;AAExD,wBAAsB,uBAAuB,CAC3C,GAAG,EAAE,GAAG,EACR,OAAO,CAAC,EAAE,YAAY,EACtB,EAAE,CAAC,EAAE,QAAQ,EACb,SAAS,CAAC,EAAE,EAAE,EACd,IAAI,GAAE,WAAuB,EAC7B,IAAI,CAAC,EAAE,EAAE,EACT,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,GAC7B,OAAO,CAAC,cAAc,CAAC,CAkDzB;AAED,wBAAsB,gBAAgB,CAAC,MAAM,EAAE,0BAA0B,EAAE,KAAK,EAAE,YAAY,EAAE,EAAE,CAAC,EAAE,QAAQ,iBAU5G;AAED,wBAAsB,mBAAmB,CAAC,OAAO,EAAE,cAAc,EAAE,EAAE,CAAC,EAAE,QAAQ,uCAgB/E"}
|
package/dest/utils/accounts.js
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import { getIdentities } from '@aztec/accounts/utils';
|
|
2
|
-
import { Fr } from '@aztec/foundation/fields';
|
|
3
|
-
import { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
4
|
-
import { deriveSigningKey } from '@aztec/stdlib/keys';
|
|
5
|
-
import { extractECDSAPublicKeyFromBase64String } from './ecdsa.js';
|
|
6
|
-
export const AccountTypes = [
|
|
7
|
-
'schnorr',
|
|
8
|
-
'ecdsasecp256r1ssh',
|
|
9
|
-
'ecdsasecp256k1'
|
|
10
|
-
];
|
|
11
|
-
export async function createOrRetrieveAccount(pxe, address, db, secretKey, type = 'schnorr', salt, publicKey) {
|
|
12
|
-
let account;
|
|
13
|
-
salt ??= Fr.ZERO;
|
|
14
|
-
if (db && address) {
|
|
15
|
-
({ type, secretKey, salt } = await db.retrieveAccount(address));
|
|
16
|
-
}
|
|
17
|
-
if (!salt) {
|
|
18
|
-
throw new Error('Cannot retrieve/create wallet without salt');
|
|
19
|
-
}
|
|
20
|
-
if (!secretKey) {
|
|
21
|
-
throw new Error('Cannot retrieve/create wallet without secret key');
|
|
22
|
-
}
|
|
23
|
-
switch(type){
|
|
24
|
-
case 'schnorr':
|
|
25
|
-
{
|
|
26
|
-
const { getSchnorrAccount } = await import('@aztec/accounts/schnorr');
|
|
27
|
-
account = getSchnorrAccount(pxe, secretKey, deriveSigningKey(secretKey), salt);
|
|
28
|
-
break;
|
|
29
|
-
}
|
|
30
|
-
case 'ecdsasecp256r1ssh':
|
|
31
|
-
{
|
|
32
|
-
let publicSigningKey;
|
|
33
|
-
if (db && address) {
|
|
34
|
-
publicSigningKey = await db.retrieveAccountMetadata(address, 'publicSigningKey');
|
|
35
|
-
} else if (publicKey) {
|
|
36
|
-
const identities = await getIdentities();
|
|
37
|
-
const foundIdentity = identities.find((identity)=>identity.type === 'ecdsa-sha2-nistp256' && identity.publicKey === publicKey);
|
|
38
|
-
if (!foundIdentity) {
|
|
39
|
-
throw new Error(`Identity for public key ${publicKey} not found in the SSH agent`);
|
|
40
|
-
}
|
|
41
|
-
publicSigningKey = extractECDSAPublicKeyFromBase64String(foundIdentity.publicKey);
|
|
42
|
-
} else {
|
|
43
|
-
throw new Error('Public key must be provided for ECDSA SSH account');
|
|
44
|
-
}
|
|
45
|
-
const { getEcdsaRSSHAccount } = await import('@aztec/accounts/ecdsa');
|
|
46
|
-
account = getEcdsaRSSHAccount(pxe, secretKey, publicSigningKey, salt);
|
|
47
|
-
break;
|
|
48
|
-
}
|
|
49
|
-
default:
|
|
50
|
-
{
|
|
51
|
-
throw new Error(`Unsupported account type: ${type}`);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
return account;
|
|
55
|
-
}
|
|
56
|
-
export async function addScopeToWallet(wallet, scope, db) {
|
|
57
|
-
const address = wallet.getAddress().toString();
|
|
58
|
-
const currentScopes = wallet.getScopes() ?? [];
|
|
59
|
-
const deduplicatedScopes = Array.from(new Set([
|
|
60
|
-
address,
|
|
61
|
-
...currentScopes,
|
|
62
|
-
scope
|
|
63
|
-
].map((scope)=>scope.toString())).values());
|
|
64
|
-
if (db) {
|
|
65
|
-
await db.storeAccountMetadata(wallet.getAddress(), 'scopes', Buffer.from(deduplicatedScopes.join(',')));
|
|
66
|
-
}
|
|
67
|
-
wallet.setScopes(deduplicatedScopes.map((scope)=>AztecAddress.fromString(scope)));
|
|
68
|
-
}
|
|
69
|
-
export async function getWalletWithScopes(account, db) {
|
|
70
|
-
const wallet = await account.getWallet();
|
|
71
|
-
if (db) {
|
|
72
|
-
const address = wallet.getAddress().toString();
|
|
73
|
-
let storedScopes = [];
|
|
74
|
-
try {
|
|
75
|
-
storedScopes = (await db.retrieveAccountMetadata(wallet.getAddress(), 'scopes') ?? '').toString().split(',');
|
|
76
|
-
// eslint-disable-next-line no-empty
|
|
77
|
-
} catch {}
|
|
78
|
-
const currentScopes = wallet.getScopes()?.map((scopes)=>scopes.toString()) ?? [];
|
|
79
|
-
const deduplicatedScopes = Array.from(new Set([
|
|
80
|
-
address,
|
|
81
|
-
...currentScopes,
|
|
82
|
-
...storedScopes
|
|
83
|
-
]).values()).map((scope)=>AztecAddress.fromString(scope));
|
|
84
|
-
wallet.setScopes(deduplicatedScopes);
|
|
85
|
-
}
|
|
86
|
-
return wallet;
|
|
87
|
-
}
|