@aztec/wallet-sdk 0.0.1-commit.35158ae7e → 0.0.1-commit.3750d92a7
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/dest/base-wallet/base_wallet.d.ts +35 -31
- package/dest/base-wallet/base_wallet.d.ts.map +1 -1
- package/dest/base-wallet/base_wallet.js +69 -35
- package/dest/base-wallet/index.d.ts +2 -2
- package/dest/base-wallet/index.d.ts.map +1 -1
- package/dest/base-wallet/utils.d.ts +3 -2
- package/dest/base-wallet/utils.d.ts.map +1 -1
- package/dest/base-wallet/utils.js +3 -1
- package/dest/crypto.d.ts +39 -1
- package/dest/crypto.d.ts.map +1 -1
- package/dest/crypto.js +88 -0
- package/dest/extension/provider/extension_wallet.d.ts +2 -5
- package/dest/extension/provider/extension_wallet.d.ts.map +1 -1
- package/dest/extension/provider/index.d.ts +2 -2
- package/dest/extension/provider/index.d.ts.map +1 -1
- package/dest/iframe/handlers/iframe_connection_handler.d.ts +118 -0
- package/dest/iframe/handlers/iframe_connection_handler.d.ts.map +1 -0
- package/dest/iframe/handlers/iframe_connection_handler.js +228 -0
- package/dest/iframe/handlers/index.d.ts +2 -0
- package/dest/iframe/handlers/index.d.ts.map +1 -0
- package/dest/iframe/handlers/index.js +1 -0
- package/dest/iframe/provider/iframe_discovery.d.ts +25 -0
- package/dest/iframe/provider/iframe_discovery.d.ts.map +1 -0
- package/dest/iframe/provider/iframe_discovery.js +167 -0
- package/dest/iframe/provider/iframe_provider.d.ts +65 -0
- package/dest/iframe/provider/iframe_provider.d.ts.map +1 -0
- package/dest/iframe/provider/iframe_provider.js +257 -0
- package/dest/iframe/provider/iframe_wallet.d.ts +68 -0
- package/dest/iframe/provider/iframe_wallet.d.ts.map +1 -0
- package/dest/iframe/provider/iframe_wallet.js +200 -0
- package/dest/iframe/provider/index.d.ts +4 -0
- package/dest/iframe/provider/index.d.ts.map +1 -0
- package/dest/iframe/provider/index.js +3 -0
- package/dest/manager/types.d.ts +3 -2
- package/dest/manager/types.d.ts.map +1 -1
- package/dest/manager/wallet_manager.d.ts +1 -1
- package/dest/manager/wallet_manager.d.ts.map +1 -1
- package/dest/manager/wallet_manager.js +46 -16
- package/dest/types.d.ts +14 -2
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +4 -0
- package/package.json +12 -8
- package/src/base-wallet/base_wallet.ts +98 -59
- package/src/base-wallet/index.ts +6 -1
- package/src/base-wallet/utils.ts +6 -4
- package/src/crypto.ts +104 -0
- package/src/extension/provider/extension_wallet.ts +1 -6
- package/src/extension/provider/index.ts +1 -1
- package/src/iframe/handlers/iframe_connection_handler.ts +328 -0
- package/src/iframe/handlers/index.ts +7 -0
- package/src/iframe/provider/iframe_discovery.ts +185 -0
- package/src/iframe/provider/iframe_provider.ts +331 -0
- package/src/iframe/provider/iframe_wallet.ts +229 -0
- package/src/iframe/provider/index.ts +3 -0
- package/src/manager/types.ts +2 -1
- package/src/manager/wallet_manager.ts +48 -14
- package/src/types.ts +13 -0
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { promiseWithResolvers } from '@aztec/foundation/promise';
|
|
2
2
|
import { ExtensionProvider, ExtensionWallet } from '../extension/provider/index.js';
|
|
3
|
+
import { discoverWebWallets } from '../iframe/provider/iframe_discovery.js';
|
|
3
4
|
import { WalletMessageType } from '../types.js';
|
|
4
5
|
/**
|
|
5
6
|
* Manager for wallet discovery, configuration, and connection.
|
|
@@ -75,6 +76,20 @@ import { WalletMessageType } from '../types.js';
|
|
|
75
76
|
let pendingResolve = null;
|
|
76
77
|
let completed = false;
|
|
77
78
|
const { promise: donePromise, resolve: resolveDone } = promiseWithResolvers();
|
|
79
|
+
const pendingSources = new Set();
|
|
80
|
+
const emit = (provider)=>{
|
|
81
|
+
options.onWalletDiscovered?.(provider);
|
|
82
|
+
if (pendingResolve) {
|
|
83
|
+
const resolve = pendingResolve;
|
|
84
|
+
pendingResolve = null;
|
|
85
|
+
resolve({
|
|
86
|
+
value: provider,
|
|
87
|
+
done: false
|
|
88
|
+
});
|
|
89
|
+
} else {
|
|
90
|
+
pendingProviders.push(provider);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
78
93
|
const markComplete = ()=>{
|
|
79
94
|
completed = true;
|
|
80
95
|
resolveDone();
|
|
@@ -87,7 +102,14 @@ import { WalletMessageType } from '../types.js';
|
|
|
87
102
|
});
|
|
88
103
|
}
|
|
89
104
|
};
|
|
105
|
+
const sourceComplete = (source)=>{
|
|
106
|
+
pendingSources.delete(source);
|
|
107
|
+
if (pendingSources.size === 0) {
|
|
108
|
+
markComplete();
|
|
109
|
+
}
|
|
110
|
+
};
|
|
90
111
|
if (this.config.extensions?.enabled) {
|
|
112
|
+
pendingSources.add('extensions');
|
|
91
113
|
const extensionConfig = this.config.extensions;
|
|
92
114
|
void ExtensionProvider.discoverWallets(chainInfo, {
|
|
93
115
|
appId,
|
|
@@ -95,25 +117,33 @@ import { WalletMessageType } from '../types.js';
|
|
|
95
117
|
signal: abortController.signal,
|
|
96
118
|
onWalletDiscovered: (discoveredWallet)=>{
|
|
97
119
|
const provider = this.createProviderFromDiscoveredWallet(discoveredWallet, chainInfo, extensionConfig);
|
|
98
|
-
if (
|
|
99
|
-
|
|
120
|
+
if (provider) {
|
|
121
|
+
emit(provider);
|
|
100
122
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
123
|
+
}
|
|
124
|
+
}).then(()=>sourceComplete('extensions'));
|
|
125
|
+
}
|
|
126
|
+
if (this.config.webWallets?.urls && this.config.webWallets.urls.length > 0) {
|
|
127
|
+
pendingSources.add('webWallets');
|
|
128
|
+
const webSession = discoverWebWallets(this.config.webWallets.urls, chainInfo);
|
|
129
|
+
// Forward discovered web wallets into the shared iterator
|
|
130
|
+
void (async ()=>{
|
|
131
|
+
try {
|
|
132
|
+
for await (const provider of webSession.wallets){
|
|
133
|
+
if (abortController.signal.aborted) {
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
emit(provider);
|
|
113
137
|
}
|
|
138
|
+
} finally{
|
|
139
|
+
sourceComplete('webWallets');
|
|
114
140
|
}
|
|
115
|
-
})
|
|
116
|
-
|
|
141
|
+
})();
|
|
142
|
+
abortController.signal.addEventListener('abort', ()=>webSession.cancel(), {
|
|
143
|
+
once: true
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
if (pendingSources.size === 0) {
|
|
117
147
|
markComplete();
|
|
118
148
|
}
|
|
119
149
|
const wallets = {
|
package/dest/types.d.ts
CHANGED
|
@@ -14,7 +14,15 @@ export declare enum WalletMessageType {
|
|
|
14
14
|
/** Key exchange request sent over MessageChannel */
|
|
15
15
|
KEY_EXCHANGE_REQUEST = "aztec-wallet-key-exchange-request",
|
|
16
16
|
/** Key exchange response sent over MessageChannel */
|
|
17
|
-
KEY_EXCHANGE_RESPONSE = "aztec-wallet-key-exchange-response"
|
|
17
|
+
KEY_EXCHANGE_RESPONSE = "aztec-wallet-key-exchange-response",
|
|
18
|
+
/** Wallet ready signal */
|
|
19
|
+
WALLET_READY = "aztec-wallet-ready",
|
|
20
|
+
/** Encrypted wallet message wrapper */
|
|
21
|
+
SECURE_MESSAGE = "aztec-wallet-secure-message",
|
|
22
|
+
/** Encrypted wallet response wrapper */
|
|
23
|
+
SECURE_RESPONSE = "aztec-wallet-secure-response",
|
|
24
|
+
/** Session disconnected notification */
|
|
25
|
+
SESSION_DISCONNECTED = "aztec-wallet-session-disconnected"
|
|
18
26
|
}
|
|
19
27
|
/**
|
|
20
28
|
* Information about an installed Aztec wallet.
|
|
@@ -120,4 +128,8 @@ export interface KeyExchangeResponse {
|
|
|
120
128
|
/** Wallet's ECDH public key for deriving shared secret */
|
|
121
129
|
publicKey: ExportedPublicKey;
|
|
122
130
|
}
|
|
123
|
-
|
|
131
|
+
/**
|
|
132
|
+
* Callback invoked when a wallet connection is disconnected.
|
|
133
|
+
*/
|
|
134
|
+
export type DisconnectCallback = () => void;
|
|
135
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHlwZXMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy90eXBlcy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEtBQUssRUFBRSxTQUFTLEVBQUUsTUFBTSx5QkFBeUIsQ0FBQztBQUV6RCxPQUFPLEtBQUssRUFBRSxpQkFBaUIsRUFBRSxNQUFNLGFBQWEsQ0FBQztBQUVyRDs7O0dBR0c7QUFDSCxvQkFBWSxpQkFBaUI7SUFDM0Isa0RBQWtEO0lBQ2xELFNBQVMsMkJBQTJCO0lBQ3BDLHVDQUF1QztJQUN2QyxrQkFBa0Isb0NBQW9DO0lBQ3RELHNFQUFzRTtJQUN0RSxVQUFVLDRCQUE0QjtJQUN0QyxvREFBb0Q7SUFDcEQsb0JBQW9CLHNDQUFzQztJQUMxRCxxREFBcUQ7SUFDckQscUJBQXFCLHVDQUF1QztJQUM1RCwwQkFBMEI7SUFDMUIsWUFBWSx1QkFBdUI7SUFDbkMsdUNBQXVDO0lBQ3ZDLGNBQWMsZ0NBQWdDO0lBQzlDLHdDQUF3QztJQUN4QyxlQUFlLGlDQUFpQztJQUNoRCx3Q0FBd0M7SUFDeEMsb0JBQW9CLHNDQUFzQztDQUMzRDtBQUVEOzs7R0FHRztBQUNILE1BQU0sV0FBVyxVQUFVO0lBQ3pCLHVDQUF1QztJQUN2QyxFQUFFLEVBQUUsTUFBTSxDQUFDO0lBQ1gsaUNBQWlDO0lBQ2pDLElBQUksRUFBRSxNQUFNLENBQUM7SUFDYiwrQkFBK0I7SUFDL0IsSUFBSSxDQUFDLEVBQUUsTUFBTSxDQUFDO0lBQ2QscUJBQXFCO0lBQ3JCLE9BQU8sRUFBRSxNQUFNLENBQU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AM,CAAC;IAClB,0DAA0D;IAC1D,SAAS,EAAE,iBAAiB,CAAC;CAC9B;AAED;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAAG,MAAM,IAAI,CAAC"}
|
package/dest/types.js
CHANGED
|
@@ -7,5 +7,9 @@
|
|
|
7
7
|
/** Disconnect message (unencrypted control message, bidirectional) */ WalletMessageType["DISCONNECT"] = "aztec-wallet-disconnect";
|
|
8
8
|
/** Key exchange request sent over MessageChannel */ WalletMessageType["KEY_EXCHANGE_REQUEST"] = "aztec-wallet-key-exchange-request";
|
|
9
9
|
/** Key exchange response sent over MessageChannel */ WalletMessageType["KEY_EXCHANGE_RESPONSE"] = "aztec-wallet-key-exchange-response";
|
|
10
|
+
/** Wallet ready signal */ WalletMessageType["WALLET_READY"] = "aztec-wallet-ready";
|
|
11
|
+
/** Encrypted wallet message wrapper */ WalletMessageType["SECURE_MESSAGE"] = "aztec-wallet-secure-message";
|
|
12
|
+
/** Encrypted wallet response wrapper */ WalletMessageType["SECURE_RESPONSE"] = "aztec-wallet-secure-response";
|
|
13
|
+
/** Session disconnected notification */ WalletMessageType["SESSION_DISCONNECTED"] = "aztec-wallet-session-disconnected";
|
|
10
14
|
return WalletMessageType;
|
|
11
15
|
}({});
|
package/package.json
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/wallet-sdk",
|
|
3
3
|
"homepage": "https://github.com/AztecProtocol/aztec-packages/tree/master/yarn-project/wallet-sdk",
|
|
4
|
-
"version": "0.0.1-commit.
|
|
4
|
+
"version": "0.0.1-commit.3750d92a7",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
7
|
"./base-wallet": "./dest/base-wallet/index.js",
|
|
8
8
|
"./extension/handlers": "./dest/extension/handlers/index.js",
|
|
9
9
|
"./extension/provider": "./dest/extension/provider/index.js",
|
|
10
|
+
"./iframe/handlers": "./dest/iframe/handlers/index.js",
|
|
11
|
+
"./iframe/provider": "./dest/iframe/provider/index.js",
|
|
10
12
|
"./crypto": "./dest/crypto.js",
|
|
11
13
|
"./types": "./dest/types.js",
|
|
12
14
|
"./manager": "./dest/manager/index.js"
|
|
@@ -16,6 +18,8 @@
|
|
|
16
18
|
"./src/base-wallet/index.ts",
|
|
17
19
|
"./src/extension/handlers/index.ts",
|
|
18
20
|
"./src/extension/provider/index.ts",
|
|
21
|
+
"./src/iframe/handlers/index.ts",
|
|
22
|
+
"./src/iframe/provider/index.ts",
|
|
19
23
|
"./src/crypto.ts",
|
|
20
24
|
"./src/types.ts",
|
|
21
25
|
"./src/manager/index.ts"
|
|
@@ -71,15 +75,15 @@
|
|
|
71
75
|
]
|
|
72
76
|
},
|
|
73
77
|
"dependencies": {
|
|
74
|
-
"@aztec/aztec.js": "0.0.1-commit.
|
|
75
|
-
"@aztec/constants": "0.0.1-commit.
|
|
76
|
-
"@aztec/entrypoints": "0.0.1-commit.
|
|
77
|
-
"@aztec/foundation": "0.0.1-commit.
|
|
78
|
-
"@aztec/pxe": "0.0.1-commit.
|
|
79
|
-
"@aztec/stdlib": "0.0.1-commit.
|
|
78
|
+
"@aztec/aztec.js": "0.0.1-commit.3750d92a7",
|
|
79
|
+
"@aztec/constants": "0.0.1-commit.3750d92a7",
|
|
80
|
+
"@aztec/entrypoints": "0.0.1-commit.3750d92a7",
|
|
81
|
+
"@aztec/foundation": "0.0.1-commit.3750d92a7",
|
|
82
|
+
"@aztec/pxe": "0.0.1-commit.3750d92a7",
|
|
83
|
+
"@aztec/stdlib": "0.0.1-commit.3750d92a7"
|
|
80
84
|
},
|
|
81
85
|
"devDependencies": {
|
|
82
|
-
"@aztec/noir-contracts.js": "0.0.1-commit.
|
|
86
|
+
"@aztec/noir-contracts.js": "0.0.1-commit.3750d92a7",
|
|
83
87
|
"@jest/globals": "^30.0.0",
|
|
84
88
|
"@types/jest": "^30.0.0",
|
|
85
89
|
"@types/node": "^22.15.17",
|
|
@@ -21,22 +21,17 @@ import {
|
|
|
21
21
|
type ProfileOptions,
|
|
22
22
|
type SendOptions,
|
|
23
23
|
type SimulateOptions,
|
|
24
|
+
TxSimulationResultWithAppOffset,
|
|
24
25
|
type Wallet,
|
|
25
26
|
type WalletCapabilities,
|
|
26
27
|
} from '@aztec/aztec.js/wallet';
|
|
27
|
-
import {
|
|
28
|
-
GAS_ESTIMATION_DA_GAS_LIMIT,
|
|
29
|
-
GAS_ESTIMATION_L2_GAS_LIMIT,
|
|
30
|
-
GAS_ESTIMATION_TEARDOWN_DA_GAS_LIMIT,
|
|
31
|
-
GAS_ESTIMATION_TEARDOWN_L2_GAS_LIMIT,
|
|
32
|
-
} from '@aztec/constants';
|
|
33
28
|
import { AccountFeePaymentMethodOptions, type DefaultAccountEntrypointOptions } from '@aztec/entrypoints/account';
|
|
34
29
|
import { DefaultEntrypoint } from '@aztec/entrypoints/default';
|
|
35
30
|
import type { ChainInfo } from '@aztec/entrypoints/interfaces';
|
|
36
31
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
37
32
|
import { createLogger } from '@aztec/foundation/log';
|
|
38
33
|
import type { FieldsOf } from '@aztec/foundation/types';
|
|
39
|
-
import {
|
|
34
|
+
import { displayDebugLogs } from '@aztec/pxe/client/lazy';
|
|
40
35
|
import type { PXE, PackedPrivateEvent } from '@aztec/pxe/server';
|
|
41
36
|
import {
|
|
42
37
|
type ContractArtifact,
|
|
@@ -52,7 +47,7 @@ import {
|
|
|
52
47
|
getContractClassFromArtifact,
|
|
53
48
|
} from '@aztec/stdlib/contract';
|
|
54
49
|
import { SimulationError } from '@aztec/stdlib/errors';
|
|
55
|
-
import { Gas, GasSettings } from '@aztec/stdlib/gas';
|
|
50
|
+
import { Gas, GasFees, GasSettings, ManaUsageEstimate } from '@aztec/stdlib/gas';
|
|
56
51
|
import {
|
|
57
52
|
computeSiloedPrivateInitializationNullifier,
|
|
58
53
|
computeSiloedPublicInitializationNullifier,
|
|
@@ -60,12 +55,12 @@ import {
|
|
|
60
55
|
import type { AztecNode } from '@aztec/stdlib/interfaces/client';
|
|
61
56
|
import {
|
|
62
57
|
BlockHeader,
|
|
58
|
+
ExecutionPayload,
|
|
63
59
|
type TxExecutionRequest,
|
|
64
60
|
type TxProfileResult,
|
|
65
|
-
TxSimulationResult,
|
|
66
61
|
type UtilityExecutionResult,
|
|
62
|
+
mergeExecutionPayloads,
|
|
67
63
|
} from '@aztec/stdlib/tx';
|
|
68
|
-
import { ExecutionPayload, mergeExecutionPayloads } from '@aztec/stdlib/tx';
|
|
69
64
|
|
|
70
65
|
import { inspect } from 'util';
|
|
71
66
|
|
|
@@ -93,9 +88,25 @@ export type SimulateViaEntrypointOptions = Pick<
|
|
|
93
88
|
> & {
|
|
94
89
|
/** Fee options for the entrypoint */
|
|
95
90
|
feeOptions: FeeOptions;
|
|
96
|
-
/** Scopes to use for the simulation */
|
|
97
|
-
scopes: AccessScopes;
|
|
98
91
|
};
|
|
92
|
+
|
|
93
|
+
/** Options for `completeFeeOptions`. */
|
|
94
|
+
export type CompleteFeeOptionsConfig = {
|
|
95
|
+
/** The address where the transaction is being sent from. */
|
|
96
|
+
from: AztecAddress | NoFrom;
|
|
97
|
+
/** The address paying for fees (if any fee payment method is embedded in the execution payload). */
|
|
98
|
+
feePayer?: AztecAddress;
|
|
99
|
+
/** User-provided partial gas settings. */
|
|
100
|
+
gasSettings?: Partial<FieldsOf<GasSettings>>;
|
|
101
|
+
/** If true, returns gas settings with high gas limits for estimation. If false, uses fallback limits. */
|
|
102
|
+
forEstimation?: boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Assumed network congestion level for fee prediction. Controls how aggressively the wallet
|
|
105
|
+
* estimates future fees. Defaults to Limit (worst case) when not specified.
|
|
106
|
+
*/
|
|
107
|
+
congestionEstimate?: ManaUsageEstimate;
|
|
108
|
+
};
|
|
109
|
+
|
|
99
110
|
/**
|
|
100
111
|
* A base class for Wallet implementations
|
|
101
112
|
*/
|
|
@@ -214,18 +225,12 @@ export abstract class BaseWallet implements Wallet {
|
|
|
214
225
|
|
|
215
226
|
/**
|
|
216
227
|
* Completes partial user-provided fee options with wallet defaults.
|
|
217
|
-
* @param
|
|
218
|
-
* @param feePayer - The address paying for fees (if any fee payment method is embedded in the execution payload)
|
|
219
|
-
* @param gasSettings - User-provided partial gas settings
|
|
220
|
-
* @returns - Complete fee options that can be used to create a transaction execution request
|
|
228
|
+
* @param config - Fee completion config.
|
|
221
229
|
*/
|
|
222
|
-
protected async completeFeeOptions(
|
|
223
|
-
from
|
|
224
|
-
feePayer?: AztecAddress,
|
|
225
|
-
gasSettings?: Partial<FieldsOf<GasSettings>>,
|
|
226
|
-
): Promise<FeeOptions> {
|
|
230
|
+
protected async completeFeeOptions(config: CompleteFeeOptionsConfig): Promise<FeeOptions> {
|
|
231
|
+
const { from, feePayer, gasSettings, forEstimation, congestionEstimate } = config;
|
|
227
232
|
const maxFeesPerGas =
|
|
228
|
-
gasSettings?.maxFeesPerGas ?? (await this.
|
|
233
|
+
gasSettings?.maxFeesPerGas ?? (await this.getMinFees(congestionEstimate)).mul(1 + this.minFeePadding);
|
|
229
234
|
let accountFeePaymentMethodOptions;
|
|
230
235
|
// If from is an address, we need to determine the appropriate fee payment method options for the
|
|
231
236
|
// account contract entrypoint to use
|
|
@@ -242,7 +247,17 @@ export abstract class BaseWallet implements Wallet {
|
|
|
242
247
|
: AccountFeePaymentMethodOptions.EXTERNAL;
|
|
243
248
|
}
|
|
244
249
|
}
|
|
245
|
-
const
|
|
250
|
+
const gasSettingsOverrides = {
|
|
251
|
+
gasLimits: gasSettings?.gasLimits ? Gas.from(gasSettings.gasLimits) : undefined,
|
|
252
|
+
teardownGasLimits: gasSettings?.teardownGasLimits ? Gas.from(gasSettings.teardownGasLimits) : undefined,
|
|
253
|
+
maxFeesPerGas,
|
|
254
|
+
maxPriorityFeesPerGas: gasSettings?.maxPriorityFeesPerGas ?? GasFees.empty(),
|
|
255
|
+
};
|
|
256
|
+
// When estimating gas (simulation), use high limits so the simulation doesn't run out of gas.
|
|
257
|
+
// When sending for real, use protocol max limits that the network will actually accept.
|
|
258
|
+
const fullGasSettings = forEstimation
|
|
259
|
+
? GasSettings.forEstimation(gasSettingsOverrides)
|
|
260
|
+
: GasSettings.fallback(gasSettingsOverrides);
|
|
246
261
|
this.log.debug(`Using L2 gas settings`, fullGasSettings);
|
|
247
262
|
return {
|
|
248
263
|
gasSettings: fullGasSettings,
|
|
@@ -252,34 +267,25 @@ export abstract class BaseWallet implements Wallet {
|
|
|
252
267
|
}
|
|
253
268
|
|
|
254
269
|
/**
|
|
255
|
-
*
|
|
256
|
-
*
|
|
257
|
-
* to
|
|
258
|
-
* @param from - The address where the transaction is being sent from
|
|
259
|
-
* @param feePayer - The address paying for fees (if any fee payment method is embedded in the execution payload)
|
|
260
|
-
* @param gasSettings - User-provided partial gas settings
|
|
270
|
+
* Returns the worst-case min fee across predicted future slots.
|
|
271
|
+
* Falls back to getCurrentMinFees if the node doesn't support getPredictedMinFees.
|
|
272
|
+
* @param estimate - The mana usage estimate to use for fee prediction. Defaults to Limit for conservative estimation.
|
|
261
273
|
*/
|
|
262
|
-
protected async
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
maxPriorityFeesPerGas,
|
|
278
|
-
);
|
|
279
|
-
return {
|
|
280
|
-
...defaultFeeOptions,
|
|
281
|
-
gasSettings: gasSettingsForEstimation,
|
|
282
|
-
};
|
|
274
|
+
protected async getMinFees(estimate: ManaUsageEstimate = ManaUsageEstimate.Limit): Promise<GasFees> {
|
|
275
|
+
try {
|
|
276
|
+
const predicted = await this.aztecNode.getPredictedMinFees(estimate);
|
|
277
|
+
if (predicted.length === 0) {
|
|
278
|
+
return this.aztecNode.getCurrentMinFees();
|
|
279
|
+
}
|
|
280
|
+
return predicted.reduce((worst, fees) => (fees.feePerL2Gas > worst.feePerL2Gas ? fees : worst));
|
|
281
|
+
} catch (err: any) {
|
|
282
|
+
// Fallback for old nodes that don't support getPredictedMinFees.
|
|
283
|
+
// Only fall back on method-not-found errors (JSON-RPC code -32601); rethrow others.
|
|
284
|
+
if (err?.cause?.code === -32601 || err?.message?.includes('Method not found')) {
|
|
285
|
+
return this.aztecNode.getCurrentMinFees();
|
|
286
|
+
}
|
|
287
|
+
throw err;
|
|
288
|
+
}
|
|
283
289
|
}
|
|
284
290
|
|
|
285
291
|
registerSender(address: AztecAddress, _alias: string = ''): Promise<AztecAddress> {
|
|
@@ -335,12 +341,28 @@ export abstract class BaseWallet implements Wallet {
|
|
|
335
341
|
opts.from,
|
|
336
342
|
opts.feeOptions,
|
|
337
343
|
);
|
|
338
|
-
|
|
344
|
+
const result = await this.pxe.simulateTx(txRequest, {
|
|
339
345
|
simulatePublic: true,
|
|
340
346
|
skipTxValidation: opts.skipTxValidation,
|
|
341
347
|
skipFeeEnforcement: opts.skipFeeEnforcement,
|
|
342
|
-
scopes: opts.
|
|
348
|
+
scopes: this.scopesFrom(opts.from, opts.additionalScopes),
|
|
343
349
|
});
|
|
350
|
+
const appCallOffset = await this.computeAppCallOffset(opts.from, opts.feeOptions);
|
|
351
|
+
return TxSimulationResultWithAppOffset.fromResultAndOffset(result, appCallOffset);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Computes the index where the app's calls begin in the flattened array of calls (0 = entrypoint/root, 1..N = fee
|
|
356
|
+
* calls, N+1 = app).
|
|
357
|
+
* @param from - The sender address, or NO_FROM for the default entrypoint.
|
|
358
|
+
* @param feeOptions - Fee options containing the wallet fee payment method.
|
|
359
|
+
*/
|
|
360
|
+
protected async computeAppCallOffset(from: AztecAddress | NoFrom, feeOptions: FeeOptions): Promise<number> {
|
|
361
|
+
if (from === NO_FROM) {
|
|
362
|
+
return 0;
|
|
363
|
+
}
|
|
364
|
+
const feeExecutionPayload = await feeOptions.walletFeePaymentMethod?.getExecutionPayload();
|
|
365
|
+
return (feeExecutionPayload?.calls.length ?? 0) + 1; // +1 for entrypoint
|
|
344
366
|
}
|
|
345
367
|
|
|
346
368
|
/**
|
|
@@ -351,10 +373,17 @@ export abstract class BaseWallet implements Wallet {
|
|
|
351
373
|
* @param opts - Simulation options (from address, fee settings, etc.).
|
|
352
374
|
* @returns The merged simulation result.
|
|
353
375
|
*/
|
|
354
|
-
async simulateTx(
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
376
|
+
async simulateTx(
|
|
377
|
+
executionPayload: ExecutionPayload,
|
|
378
|
+
opts: SimulateOptions,
|
|
379
|
+
): Promise<TxSimulationResultWithAppOffset> {
|
|
380
|
+
const feeOptions = await this.completeFeeOptions({
|
|
381
|
+
from: opts.from,
|
|
382
|
+
feePayer: executionPayload.feePayer,
|
|
383
|
+
gasSettings: opts.fee?.gasSettings,
|
|
384
|
+
forEstimation: true,
|
|
385
|
+
congestionEstimate: opts.fee?.congestionEstimate,
|
|
386
|
+
});
|
|
358
387
|
const { optimizableCalls, remainingCalls } = extractOptimizablePublicStaticCalls(executionPayload);
|
|
359
388
|
const remainingPayload = { ...executionPayload, calls: remainingCalls };
|
|
360
389
|
|
|
@@ -386,7 +415,7 @@ export abstract class BaseWallet implements Wallet {
|
|
|
386
415
|
? this.simulateViaEntrypoint(remainingPayload, {
|
|
387
416
|
from: opts.from,
|
|
388
417
|
feeOptions,
|
|
389
|
-
|
|
418
|
+
additionalScopes: opts.additionalScopes,
|
|
390
419
|
skipTxValidation: opts.skipTxValidation,
|
|
391
420
|
skipFeeEnforcement: opts.skipFeeEnforcement ?? true,
|
|
392
421
|
})
|
|
@@ -397,7 +426,12 @@ export abstract class BaseWallet implements Wallet {
|
|
|
397
426
|
}
|
|
398
427
|
|
|
399
428
|
async profileTx(executionPayload: ExecutionPayload, opts: ProfileOptions): Promise<TxProfileResult> {
|
|
400
|
-
const feeOptions = await this.completeFeeOptions(
|
|
429
|
+
const feeOptions = await this.completeFeeOptions({
|
|
430
|
+
from: opts.from,
|
|
431
|
+
feePayer: executionPayload.feePayer,
|
|
432
|
+
gasSettings: opts.fee?.gasSettings,
|
|
433
|
+
congestionEstimate: opts.fee?.congestionEstimate,
|
|
434
|
+
});
|
|
401
435
|
const txRequest = await this.createTxExecutionRequestFromPayloadAndFee(executionPayload, opts.from, feeOptions);
|
|
402
436
|
return this.pxe.profileTx(txRequest, {
|
|
403
437
|
profileMode: opts.profileMode,
|
|
@@ -410,7 +444,12 @@ export abstract class BaseWallet implements Wallet {
|
|
|
410
444
|
executionPayload: ExecutionPayload,
|
|
411
445
|
opts: SendOptions<W>,
|
|
412
446
|
): Promise<SendReturn<W>> {
|
|
413
|
-
const feeOptions = await this.completeFeeOptions(
|
|
447
|
+
const feeOptions = await this.completeFeeOptions({
|
|
448
|
+
from: opts.from,
|
|
449
|
+
feePayer: executionPayload.feePayer,
|
|
450
|
+
gasSettings: opts.fee?.gasSettings,
|
|
451
|
+
congestionEstimate: opts.fee?.congestionEstimate,
|
|
452
|
+
});
|
|
414
453
|
const txRequest = await this.createTxExecutionRequestFromPayloadAndFee(executionPayload, opts.from, feeOptions);
|
|
415
454
|
const provenTx = await this.pxe.proveTx(txRequest, this.scopesFrom(opts.from, opts.additionalScopes));
|
|
416
455
|
const offchainOutput = extractOffchainOutput(
|
package/src/base-wallet/index.ts
CHANGED
|
@@ -1,2 +1,7 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export {
|
|
2
|
+
BaseWallet,
|
|
3
|
+
type CompleteFeeOptionsConfig,
|
|
4
|
+
type FeeOptions,
|
|
5
|
+
type SimulateViaEntrypointOptions,
|
|
6
|
+
} from './base_wallet.js';
|
|
2
7
|
export { simulateViaNode, buildMergedSimulationResult, extractOptimizablePublicStaticCalls } from './utils.js';
|
package/src/base-wallet/utils.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { AztecNode } from '@aztec/aztec.js/node';
|
|
2
|
+
import { TxSimulationResultWithAppOffset } from '@aztec/aztec.js/wallet';
|
|
2
3
|
import { MAX_ENQUEUED_CALLS_PER_CALL } from '@aztec/constants';
|
|
3
4
|
import type { ChainInfo } from '@aztec/entrypoints/interfaces';
|
|
4
5
|
import { makeTuple } from '@aztec/foundation/array';
|
|
@@ -214,13 +215,13 @@ export async function simulateViaNode(
|
|
|
214
215
|
*/
|
|
215
216
|
export function buildMergedSimulationResult(
|
|
216
217
|
optimizedResults: TxSimulationResult[],
|
|
217
|
-
normalResult:
|
|
218
|
-
):
|
|
218
|
+
normalResult: TxSimulationResultWithAppOffset | null,
|
|
219
|
+
): TxSimulationResultWithAppOffset {
|
|
219
220
|
const optimizedReturnValues = optimizedResults.flatMap(r => r.publicOutput?.publicReturnValues ?? []);
|
|
220
221
|
const normalReturnValues = normalResult?.publicOutput?.publicReturnValues ?? [];
|
|
221
222
|
const allReturnValues = [...optimizedReturnValues, ...normalReturnValues];
|
|
222
223
|
|
|
223
|
-
const baseResult = normalResult ?? optimizedResults[0];
|
|
224
|
+
const baseResult: TxSimulationResult = normalResult ?? optimizedResults[0];
|
|
224
225
|
|
|
225
226
|
const mergedPublicOutput: PublicSimulationOutput | undefined = baseResult.publicOutput
|
|
226
227
|
? {
|
|
@@ -229,10 +230,11 @@ export function buildMergedSimulationResult(
|
|
|
229
230
|
}
|
|
230
231
|
: undefined;
|
|
231
232
|
|
|
232
|
-
|
|
233
|
+
const merged = new TxSimulationResult(
|
|
233
234
|
baseResult.privateExecutionResult,
|
|
234
235
|
baseResult.publicInputs,
|
|
235
236
|
mergedPublicOutput,
|
|
236
237
|
normalResult?.stats,
|
|
237
238
|
);
|
|
239
|
+
return TxSimulationResultWithAppOffset.fromResultAndOffset(merged, normalResult?.appCallOffset ?? 0);
|
|
238
240
|
}
|
package/src/crypto.ts
CHANGED
|
@@ -497,3 +497,107 @@ export function hashToEmoji(hash: string, count: number = DEFAULT_EMOJI_GRID_SIZ
|
|
|
497
497
|
}
|
|
498
498
|
return emojis.join('');
|
|
499
499
|
}
|
|
500
|
+
|
|
501
|
+
// ─── Passphrase-based encryption (PBKDF2 + AES-256-GCM) ───────────────────
|
|
502
|
+
|
|
503
|
+
/** Default PBKDF2 iteration count. High to compensate for short PINs (~1-2s on modern hardware). */
|
|
504
|
+
const DEFAULT_PBKDF2_ITERATIONS = 2_000_000;
|
|
505
|
+
const PBKDF2_SALT_BYTES = 16;
|
|
506
|
+
const PBKDF2_IV_BYTES = 12;
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* Derives an AES-256-GCM key from a passphrase using PBKDF2-SHA256.
|
|
510
|
+
*
|
|
511
|
+
* @param passphrase - The user-provided passphrase or PIN
|
|
512
|
+
* @param salt - Random salt bytes
|
|
513
|
+
* @param iterations - PBKDF2 iteration count (default: 2,000,000)
|
|
514
|
+
* @returns An AES-256-GCM CryptoKey
|
|
515
|
+
*/
|
|
516
|
+
export async function deriveKeyFromPassphrase(
|
|
517
|
+
passphrase: string,
|
|
518
|
+
salt: Uint8Array,
|
|
519
|
+
iterations: number = DEFAULT_PBKDF2_ITERATIONS,
|
|
520
|
+
): Promise<CryptoKey> {
|
|
521
|
+
const keyMaterial = await crypto.subtle.importKey('raw', new TextEncoder().encode(passphrase), 'PBKDF2', false, [
|
|
522
|
+
'deriveKey',
|
|
523
|
+
]);
|
|
524
|
+
return crypto.subtle.deriveKey(
|
|
525
|
+
{ name: 'PBKDF2', salt: salt as BufferSource, iterations, hash: 'SHA-256' },
|
|
526
|
+
keyMaterial,
|
|
527
|
+
{ name: 'AES-GCM', length: 256 },
|
|
528
|
+
false,
|
|
529
|
+
['encrypt', 'decrypt'],
|
|
530
|
+
);
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* Encrypts arbitrary bytes with a passphrase using PBKDF2 + AES-256-GCM.
|
|
535
|
+
*
|
|
536
|
+
* Output layout: `[salt (16)] [iv (12)] [ciphertext (...)]`
|
|
537
|
+
*
|
|
538
|
+
* @param plaintext - Data to encrypt
|
|
539
|
+
* @param passphrase - User passphrase or PIN
|
|
540
|
+
* @param iterations - PBKDF2 iteration count (default: 2,000,000)
|
|
541
|
+
* @returns A Uint8Array containing salt + iv + ciphertext
|
|
542
|
+
*/
|
|
543
|
+
export async function encryptWithPassphrase(
|
|
544
|
+
plaintext: Uint8Array,
|
|
545
|
+
passphrase: string,
|
|
546
|
+
iterations: number = DEFAULT_PBKDF2_ITERATIONS,
|
|
547
|
+
): Promise<Uint8Array> {
|
|
548
|
+
const salt = crypto.getRandomValues(new Uint8Array(PBKDF2_SALT_BYTES));
|
|
549
|
+
const iv = crypto.getRandomValues(new Uint8Array(PBKDF2_IV_BYTES));
|
|
550
|
+
const key = await deriveKeyFromPassphrase(passphrase, salt, iterations);
|
|
551
|
+
const ciphertext = new Uint8Array(
|
|
552
|
+
await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, plaintext as BufferSource),
|
|
553
|
+
);
|
|
554
|
+
const result = new Uint8Array(PBKDF2_SALT_BYTES + PBKDF2_IV_BYTES + ciphertext.length);
|
|
555
|
+
result.set(salt, 0);
|
|
556
|
+
result.set(iv, PBKDF2_SALT_BYTES);
|
|
557
|
+
result.set(ciphertext, PBKDF2_SALT_BYTES + PBKDF2_IV_BYTES);
|
|
558
|
+
return result;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* Decrypts data produced by {@link encryptWithPassphrase}.
|
|
563
|
+
*
|
|
564
|
+
* @param data - The encrypted blob (salt + iv + ciphertext)
|
|
565
|
+
* @param passphrase - The passphrase used during encryption
|
|
566
|
+
* @param iterations - PBKDF2 iteration count (must match encryption)
|
|
567
|
+
* @returns The decrypted plaintext bytes
|
|
568
|
+
* @throws On wrong passphrase (AES-GCM auth tag mismatch)
|
|
569
|
+
*/
|
|
570
|
+
export async function decryptWithPassphrase(
|
|
571
|
+
data: Uint8Array,
|
|
572
|
+
passphrase: string,
|
|
573
|
+
iterations: number = DEFAULT_PBKDF2_ITERATIONS,
|
|
574
|
+
): Promise<Uint8Array> {
|
|
575
|
+
const salt = data.slice(0, PBKDF2_SALT_BYTES);
|
|
576
|
+
const iv = data.slice(PBKDF2_SALT_BYTES, PBKDF2_SALT_BYTES + PBKDF2_IV_BYTES);
|
|
577
|
+
const ciphertext = data.slice(PBKDF2_SALT_BYTES + PBKDF2_IV_BYTES);
|
|
578
|
+
const key = await deriveKeyFromPassphrase(passphrase, salt, iterations);
|
|
579
|
+
return new Uint8Array(await crypto.subtle.decrypt({ name: 'AES-GCM', iv }, key, ciphertext as BufferSource));
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* Converts a Uint8Array to a base64 string.
|
|
584
|
+
*/
|
|
585
|
+
export function uint8ToBase64(bytes: Uint8Array): string {
|
|
586
|
+
let binary = '';
|
|
587
|
+
for (const b of bytes) {
|
|
588
|
+
binary += String.fromCharCode(b);
|
|
589
|
+
}
|
|
590
|
+
return btoa(binary);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* Converts a base64 string to a Uint8Array.
|
|
595
|
+
*/
|
|
596
|
+
export function base64ToUint8(b64: string): Uint8Array {
|
|
597
|
+
const binary = atob(b64);
|
|
598
|
+
const bytes = new Uint8Array(binary.length);
|
|
599
|
+
for (let i = 0; i < binary.length; i++) {
|
|
600
|
+
bytes[i] = binary.charCodeAt(i);
|
|
601
|
+
}
|
|
602
|
+
return bytes;
|
|
603
|
+
}
|
|
@@ -6,7 +6,7 @@ import { schemaHasMethod } from '@aztec/foundation/schemas';
|
|
|
6
6
|
import type { FunctionsOf } from '@aztec/foundation/types';
|
|
7
7
|
|
|
8
8
|
import { type EncryptedPayload, decrypt, encrypt } from '../../crypto.js';
|
|
9
|
-
import { type WalletMessage, WalletMessageType, type WalletResponse } from '../../types.js';
|
|
9
|
+
import { type DisconnectCallback, type WalletMessage, WalletMessageType, type WalletResponse } from '../../types.js';
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Internal type representing a wallet method call before encryption.
|
|
@@ -19,11 +19,6 @@ type WalletMethodCall = {
|
|
|
19
19
|
args: unknown[];
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
-
/**
|
|
23
|
-
* Callback type for wallet disconnect events.
|
|
24
|
-
*/
|
|
25
|
-
export type DisconnectCallback = () => void;
|
|
26
|
-
|
|
27
22
|
/**
|
|
28
23
|
* A wallet implementation that communicates with browser extension wallets
|
|
29
24
|
* using an encrypted MessageChannel.
|