@aztec/wallet-sdk 4.1.2 → 4.2.0-aztecnr-rc.2
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 +23 -17
- package/dest/base-wallet/base_wallet.d.ts.map +1 -1
- package/dest/base-wallet/base_wallet.js +70 -45
- package/dest/base-wallet/index.d.ts +2 -2
- package/dest/base-wallet/index.d.ts.map +1 -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 +122 -78
- package/src/base-wallet/index.ts +1 -1
- 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,eyJ2ZXJzaW9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"}
|
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": "4.
|
|
4
|
+
"version": "4.2.0-aztecnr-rc.2",
|
|
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": "4.
|
|
75
|
-
"@aztec/constants": "4.
|
|
76
|
-
"@aztec/entrypoints": "4.
|
|
77
|
-
"@aztec/foundation": "4.
|
|
78
|
-
"@aztec/pxe": "4.
|
|
79
|
-
"@aztec/stdlib": "4.
|
|
78
|
+
"@aztec/aztec.js": "4.2.0-aztecnr-rc.2",
|
|
79
|
+
"@aztec/constants": "4.2.0-aztecnr-rc.2",
|
|
80
|
+
"@aztec/entrypoints": "4.2.0-aztecnr-rc.2",
|
|
81
|
+
"@aztec/foundation": "4.2.0-aztecnr-rc.2",
|
|
82
|
+
"@aztec/pxe": "4.2.0-aztecnr-rc.2",
|
|
83
|
+
"@aztec/stdlib": "4.2.0-aztecnr-rc.2"
|
|
80
84
|
},
|
|
81
85
|
"devDependencies": {
|
|
82
|
-
"@aztec/noir-contracts.js": "4.
|
|
86
|
+
"@aztec/noir-contracts.js": "4.2.0-aztecnr-rc.2",
|
|
83
87
|
"@jest/globals": "^30.0.0",
|
|
84
88
|
"@types/jest": "^30.0.0",
|
|
85
89
|
"@types/node": "^22.15.17",
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { Account } from '@aztec/aztec.js/account';
|
|
1
|
+
import type { Account, NoFrom } from '@aztec/aztec.js/account';
|
|
2
|
+
import { NO_FROM } from '@aztec/aztec.js/account';
|
|
2
3
|
import type { CallIntent, IntentInnerHash } from '@aztec/aztec.js/authorization';
|
|
3
4
|
import {
|
|
4
5
|
type InteractionWaitOptions,
|
|
@@ -8,19 +9,20 @@ import {
|
|
|
8
9
|
} from '@aztec/aztec.js/contracts';
|
|
9
10
|
import type { FeePaymentMethod } from '@aztec/aztec.js/fee';
|
|
10
11
|
import { waitForTx } from '@aztec/aztec.js/node';
|
|
11
|
-
import
|
|
12
|
-
Aliased,
|
|
13
|
-
AppCapabilities,
|
|
14
|
-
BatchResults,
|
|
15
|
-
BatchedMethod,
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
12
|
+
import {
|
|
13
|
+
type Aliased,
|
|
14
|
+
type AppCapabilities,
|
|
15
|
+
type BatchResults,
|
|
16
|
+
type BatchedMethod,
|
|
17
|
+
ContractInitializationStatus,
|
|
18
|
+
type ExecuteUtilityOptions,
|
|
19
|
+
type PrivateEvent,
|
|
20
|
+
type PrivateEventFilter,
|
|
21
|
+
type ProfileOptions,
|
|
22
|
+
type SendOptions,
|
|
23
|
+
type SimulateOptions,
|
|
24
|
+
type Wallet,
|
|
25
|
+
type WalletCapabilities,
|
|
24
26
|
} from '@aztec/aztec.js/wallet';
|
|
25
27
|
import {
|
|
26
28
|
GAS_ESTIMATION_DA_GAS_LIMIT,
|
|
@@ -29,6 +31,7 @@ import {
|
|
|
29
31
|
GAS_ESTIMATION_TEARDOWN_L2_GAS_LIMIT,
|
|
30
32
|
} from '@aztec/constants';
|
|
31
33
|
import { AccountFeePaymentMethodOptions, type DefaultAccountEntrypointOptions } from '@aztec/entrypoints/account';
|
|
34
|
+
import { DefaultEntrypoint } from '@aztec/entrypoints/default';
|
|
32
35
|
import type { ChainInfo } from '@aztec/entrypoints/interfaces';
|
|
33
36
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
34
37
|
import { createLogger } from '@aztec/foundation/log';
|
|
@@ -50,7 +53,10 @@ import {
|
|
|
50
53
|
} from '@aztec/stdlib/contract';
|
|
51
54
|
import { SimulationError } from '@aztec/stdlib/errors';
|
|
52
55
|
import { Gas, GasSettings } from '@aztec/stdlib/gas';
|
|
53
|
-
import {
|
|
56
|
+
import {
|
|
57
|
+
computeSiloedPrivateInitializationNullifier,
|
|
58
|
+
computeSiloedPublicInitializationNullifier,
|
|
59
|
+
} from '@aztec/stdlib/hash';
|
|
54
60
|
import type { AztecNode } from '@aztec/stdlib/interfaces/client';
|
|
55
61
|
import {
|
|
56
62
|
BlockHeader,
|
|
@@ -75,11 +81,21 @@ export type FeeOptions = {
|
|
|
75
81
|
*/
|
|
76
82
|
walletFeePaymentMethod?: FeePaymentMethod;
|
|
77
83
|
/** Configuration options for the account to properly handle the selected fee payment method */
|
|
78
|
-
accountFeePaymentMethodOptions
|
|
84
|
+
accountFeePaymentMethodOptions?: AccountFeePaymentMethodOptions;
|
|
79
85
|
/** The gas settings to use for the transaction */
|
|
80
86
|
gasSettings: GasSettings;
|
|
81
87
|
};
|
|
82
88
|
|
|
89
|
+
/** Options for `simulateViaEntrypoint`. */
|
|
90
|
+
export type SimulateViaEntrypointOptions = Pick<
|
|
91
|
+
SimulateOptions,
|
|
92
|
+
'from' | 'additionalScopes' | 'skipTxValidation' | 'skipFeeEnforcement'
|
|
93
|
+
> & {
|
|
94
|
+
/** Fee options for the entrypoint */
|
|
95
|
+
feeOptions: FeeOptions;
|
|
96
|
+
/** Scopes to use for the simulation */
|
|
97
|
+
scopes: AccessScopes;
|
|
98
|
+
};
|
|
83
99
|
/**
|
|
84
100
|
* A base class for Wallet implementations
|
|
85
101
|
*/
|
|
@@ -94,14 +110,8 @@ export abstract class BaseWallet implements Wallet {
|
|
|
94
110
|
protected log = createLogger('wallet-sdk:base_wallet'),
|
|
95
111
|
) {}
|
|
96
112
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
protected scopesFor(from: AztecAddress): AztecAddress[] {
|
|
100
|
-
return from.isZero() ? [] : [from];
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
protected scopesFrom(from: AztecAddress, additionalScopes: AztecAddress[] = []): AztecAddress[] {
|
|
104
|
-
const allScopes = from.isZero() ? additionalScopes : [from, ...additionalScopes];
|
|
113
|
+
protected scopesFrom(from: AztecAddress | NoFrom, additionalScopes: AztecAddress[] = []): AztecAddress[] {
|
|
114
|
+
const allScopes = from === NO_FROM ? additionalScopes : [from, ...additionalScopes];
|
|
105
115
|
const scopeSet = new Set(allScopes.map(address => address.toString()));
|
|
106
116
|
return [...scopeSet].map(AztecAddress.fromString);
|
|
107
117
|
}
|
|
@@ -129,26 +139,33 @@ export abstract class BaseWallet implements Wallet {
|
|
|
129
139
|
|
|
130
140
|
protected async createTxExecutionRequestFromPayloadAndFee(
|
|
131
141
|
executionPayload: ExecutionPayload,
|
|
132
|
-
from: AztecAddress,
|
|
142
|
+
from: AztecAddress | NoFrom,
|
|
133
143
|
feeOptions: FeeOptions,
|
|
134
144
|
): Promise<TxExecutionRequest> {
|
|
135
145
|
const feeExecutionPayload = await feeOptions.walletFeePaymentMethod?.getExecutionPayload();
|
|
136
|
-
const executionOptions: DefaultAccountEntrypointOptions = {
|
|
137
|
-
txNonce: Fr.random(),
|
|
138
|
-
cancellable: this.cancellableTransactions,
|
|
139
|
-
feePaymentMethodOptions: feeOptions.accountFeePaymentMethodOptions,
|
|
140
|
-
};
|
|
141
146
|
const finalExecutionPayload = feeExecutionPayload
|
|
142
147
|
? mergeExecutionPayloads([feeExecutionPayload, executionPayload])
|
|
143
148
|
: executionPayload;
|
|
144
|
-
const fromAccount = await this.getAccountFromAddress(from);
|
|
145
149
|
const chainInfo = await this.getChainInfo();
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
chainInfo
|
|
150
|
-
|
|
151
|
-
|
|
150
|
+
|
|
151
|
+
if (from === NO_FROM) {
|
|
152
|
+
const entrypoint = new DefaultEntrypoint();
|
|
153
|
+
return entrypoint.createTxExecutionRequest(finalExecutionPayload, feeOptions.gasSettings, chainInfo);
|
|
154
|
+
} else {
|
|
155
|
+
const fromAccount = await this.getAccountFromAddress(from);
|
|
156
|
+
const executionOptions: DefaultAccountEntrypointOptions = {
|
|
157
|
+
txNonce: Fr.random(),
|
|
158
|
+
cancellable: this.cancellableTransactions,
|
|
159
|
+
// If from is an address, feeOptions include the way the account contract should handle the fee payment
|
|
160
|
+
feePaymentMethodOptions: feeOptions.accountFeePaymentMethodOptions!,
|
|
161
|
+
};
|
|
162
|
+
return fromAccount.createTxExecutionRequest(
|
|
163
|
+
finalExecutionPayload,
|
|
164
|
+
feeOptions.gasSettings,
|
|
165
|
+
chainInfo,
|
|
166
|
+
executionOptions,
|
|
167
|
+
);
|
|
168
|
+
}
|
|
152
169
|
}
|
|
153
170
|
|
|
154
171
|
public async createAuthWit(
|
|
@@ -203,23 +220,27 @@ export abstract class BaseWallet implements Wallet {
|
|
|
203
220
|
* @returns - Complete fee options that can be used to create a transaction execution request
|
|
204
221
|
*/
|
|
205
222
|
protected async completeFeeOptions(
|
|
206
|
-
from: AztecAddress,
|
|
223
|
+
from: AztecAddress | NoFrom,
|
|
207
224
|
feePayer?: AztecAddress,
|
|
208
225
|
gasSettings?: Partial<FieldsOf<GasSettings>>,
|
|
209
226
|
): Promise<FeeOptions> {
|
|
210
227
|
const maxFeesPerGas =
|
|
211
228
|
gasSettings?.maxFeesPerGas ?? (await this.aztecNode.getCurrentMinFees()).mul(1 + this.minFeePadding);
|
|
212
229
|
let accountFeePaymentMethodOptions;
|
|
213
|
-
//
|
|
214
|
-
//
|
|
215
|
-
if (
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
230
|
+
// If from is an address, we need to determine the appropriate fee payment method options for the
|
|
231
|
+
// account contract entrypoint to use
|
|
232
|
+
if (from !== NO_FROM) {
|
|
233
|
+
if (!feePayer) {
|
|
234
|
+
// The transaction does not include a fee payment method, so we set the flag
|
|
235
|
+
// for the account to use its fee juice balance
|
|
236
|
+
accountFeePaymentMethodOptions = AccountFeePaymentMethodOptions.PREEXISTING_FEE_JUICE;
|
|
237
|
+
} else {
|
|
238
|
+
// The transaction includes fee payment method, so we check if we are the fee payer for it
|
|
239
|
+
// (this can only happen if the embedded payment method is FeeJuiceWithClaim)
|
|
240
|
+
accountFeePaymentMethodOptions = from.equals(feePayer)
|
|
241
|
+
? AccountFeePaymentMethodOptions.FEE_JUICE_WITH_CLAIM
|
|
242
|
+
: AccountFeePaymentMethodOptions.EXTERNAL;
|
|
243
|
+
}
|
|
223
244
|
}
|
|
224
245
|
const fullGasSettings: GasSettings = GasSettings.default({ ...gasSettings, maxFeesPerGas });
|
|
225
246
|
this.log.debug(`Using L2 gas settings`, fullGasSettings);
|
|
@@ -239,7 +260,7 @@ export abstract class BaseWallet implements Wallet {
|
|
|
239
260
|
* @param gasSettings - User-provided partial gas settings
|
|
240
261
|
*/
|
|
241
262
|
protected async completeFeeOptionsForEstimation(
|
|
242
|
-
from: AztecAddress,
|
|
263
|
+
from: AztecAddress | NoFrom,
|
|
243
264
|
feePayer?: AztecAddress,
|
|
244
265
|
gasSettings?: Partial<FieldsOf<GasSettings>>,
|
|
245
266
|
) {
|
|
@@ -306,22 +327,20 @@ export abstract class BaseWallet implements Wallet {
|
|
|
306
327
|
/**
|
|
307
328
|
* Simulates calls through the standard PXE path (account entrypoint).
|
|
308
329
|
* @param executionPayload - The execution payload to simulate.
|
|
309
|
-
* @param
|
|
310
|
-
* @param feeOptions - Fee options for the transaction.
|
|
311
|
-
* @param skipTxValidation - Whether to skip tx validation.
|
|
312
|
-
* @param skipFeeEnforcement - Whether to skip fee enforcement.
|
|
313
|
-
* @param scopes - The scopes to use for the simulation.
|
|
330
|
+
* @param opts - Simulation options.
|
|
314
331
|
*/
|
|
315
|
-
protected async simulateViaEntrypoint(
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
332
|
+
protected async simulateViaEntrypoint(executionPayload: ExecutionPayload, opts: SimulateViaEntrypointOptions) {
|
|
333
|
+
const txRequest = await this.createTxExecutionRequestFromPayloadAndFee(
|
|
334
|
+
executionPayload,
|
|
335
|
+
opts.from,
|
|
336
|
+
opts.feeOptions,
|
|
337
|
+
);
|
|
338
|
+
return this.pxe.simulateTx(txRequest, {
|
|
339
|
+
simulatePublic: true,
|
|
340
|
+
skipTxValidation: opts.skipTxValidation,
|
|
341
|
+
skipFeeEnforcement: opts.skipFeeEnforcement,
|
|
342
|
+
scopes: opts.scopes,
|
|
343
|
+
});
|
|
325
344
|
}
|
|
326
345
|
|
|
327
346
|
/**
|
|
@@ -349,12 +368,13 @@ export abstract class BaseWallet implements Wallet {
|
|
|
349
368
|
blockHeader = (await this.aztecNode.getBlockHeader())!;
|
|
350
369
|
}
|
|
351
370
|
|
|
371
|
+
const simulationOrigin = opts.from === NO_FROM ? AztecAddress.ZERO : opts.from;
|
|
352
372
|
const [optimizedResults, normalResult] = await Promise.all([
|
|
353
373
|
optimizableCalls.length > 0
|
|
354
374
|
? simulateViaNode(
|
|
355
375
|
this.aztecNode,
|
|
356
376
|
optimizableCalls,
|
|
357
|
-
|
|
377
|
+
simulationOrigin,
|
|
358
378
|
chainInfo,
|
|
359
379
|
feeOptions.gasSettings,
|
|
360
380
|
blockHeader,
|
|
@@ -363,14 +383,13 @@ export abstract class BaseWallet implements Wallet {
|
|
|
363
383
|
)
|
|
364
384
|
: Promise.resolve([]),
|
|
365
385
|
remainingCalls.length > 0
|
|
366
|
-
? this.simulateViaEntrypoint(
|
|
367
|
-
|
|
368
|
-
opts.from,
|
|
386
|
+
? this.simulateViaEntrypoint(remainingPayload, {
|
|
387
|
+
from: opts.from,
|
|
369
388
|
feeOptions,
|
|
370
|
-
this.scopesFrom(opts.from, opts.additionalScopes),
|
|
371
|
-
opts.skipTxValidation,
|
|
372
|
-
opts.skipFeeEnforcement ?? true,
|
|
373
|
-
)
|
|
389
|
+
scopes: this.scopesFrom(opts.from, opts.additionalScopes),
|
|
390
|
+
skipTxValidation: opts.skipTxValidation,
|
|
391
|
+
skipFeeEnforcement: opts.skipFeeEnforcement ?? true,
|
|
392
|
+
})
|
|
374
393
|
: Promise.resolve(null),
|
|
375
394
|
]);
|
|
376
395
|
|
|
@@ -394,7 +413,10 @@ export abstract class BaseWallet implements Wallet {
|
|
|
394
413
|
const feeOptions = await this.completeFeeOptions(opts.from, executionPayload.feePayer, opts.fee?.gasSettings);
|
|
395
414
|
const txRequest = await this.createTxExecutionRequestFromPayloadAndFee(executionPayload, opts.from, feeOptions);
|
|
396
415
|
const provenTx = await this.pxe.proveTx(txRequest, this.scopesFrom(opts.from, opts.additionalScopes));
|
|
397
|
-
const offchainOutput = extractOffchainOutput(
|
|
416
|
+
const offchainOutput = extractOffchainOutput(
|
|
417
|
+
provenTx.getOffchainEffects(),
|
|
418
|
+
provenTx.publicInputs.constants.anchorBlockHeader.globalVariables.timestamp,
|
|
419
|
+
);
|
|
398
420
|
const tx = await provenTx.toTx();
|
|
399
421
|
const txHash = tx.getTxHash();
|
|
400
422
|
if (await this.aztecNode.getTxEffect(txHash)) {
|
|
@@ -451,7 +473,7 @@ export abstract class BaseWallet implements Wallet {
|
|
|
451
473
|
}
|
|
452
474
|
|
|
453
475
|
executeUtility(call: FunctionCall, opts: ExecuteUtilityOptions): Promise<UtilityExecutionResult> {
|
|
454
|
-
return this.pxe.executeUtility(call, { authwits: opts.authWitnesses, scopes:
|
|
476
|
+
return this.pxe.executeUtility(call, { authwits: opts.authWitnesses, scopes: opts.scopes });
|
|
455
477
|
}
|
|
456
478
|
|
|
457
479
|
async getPrivateEvents<T>(
|
|
@@ -474,17 +496,39 @@ export abstract class BaseWallet implements Wallet {
|
|
|
474
496
|
return decodedEvents;
|
|
475
497
|
}
|
|
476
498
|
|
|
499
|
+
/**
|
|
500
|
+
* Returns metadata about a contract, including whether it has been initialized, published, and updated.
|
|
501
|
+
* @param address - The contract address to query.
|
|
502
|
+
*/
|
|
477
503
|
async getContractMetadata(address: AztecAddress) {
|
|
478
504
|
const instance = await this.pxe.getContractInstance(address);
|
|
479
|
-
const
|
|
480
|
-
|
|
481
|
-
|
|
505
|
+
const publiclyRegisteredContractPromise = this.aztecNode.getContract(address);
|
|
506
|
+
|
|
507
|
+
let initializationStatus: ContractInitializationStatus;
|
|
508
|
+
if (instance) {
|
|
509
|
+
// We have the instance, so we can compute the private initialization nullifier (which includes init_hash and is
|
|
510
|
+
// emitted by both private and public initializers) and get a definitive INITIALIZED/UNINITIALIZED answer.
|
|
511
|
+
const initNullifier = await computeSiloedPrivateInitializationNullifier(address, instance.initializationHash);
|
|
512
|
+
const witness = await this.aztecNode.getNullifierMembershipWitness('latest', initNullifier);
|
|
513
|
+
initializationStatus = witness
|
|
514
|
+
? ContractInitializationStatus.INITIALIZED
|
|
515
|
+
: ContractInitializationStatus.UNINITIALIZED;
|
|
516
|
+
} else {
|
|
517
|
+
// Without the instance we lack the init_hash needed for the private nullifier. We fall back to checking the
|
|
518
|
+
// public initialization nullifier (computed from address alone). Not all contracts emit it (only those with
|
|
519
|
+
// public functions that require initialization checks), so its absence doesn't mean the contract is
|
|
520
|
+
// uninitialized.
|
|
521
|
+
const publicNullifier = await computeSiloedPublicInitializationNullifier(address);
|
|
522
|
+
const witness = await this.aztecNode.getNullifierMembershipWitness('latest', publicNullifier);
|
|
523
|
+
initializationStatus = witness ? ContractInitializationStatus.INITIALIZED : ContractInitializationStatus.UNKNOWN;
|
|
524
|
+
}
|
|
525
|
+
const publiclyRegisteredContract = await publiclyRegisteredContractPromise;
|
|
482
526
|
const isContractUpdated =
|
|
483
527
|
publiclyRegisteredContract &&
|
|
484
528
|
!publiclyRegisteredContract.currentContractClassId.equals(publiclyRegisteredContract.originalContractClassId);
|
|
485
529
|
return {
|
|
486
530
|
instance: instance ?? undefined,
|
|
487
|
-
|
|
531
|
+
initializationStatus,
|
|
488
532
|
isContractPublished: !!publiclyRegisteredContract,
|
|
489
533
|
isContractUpdated: !!isContractUpdated,
|
|
490
534
|
updatedContractClassId: isContractUpdated ? publiclyRegisteredContract.currentContractClassId : undefined,
|
package/src/base-wallet/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { BaseWallet, type FeeOptions } from './base_wallet.js';
|
|
1
|
+
export { BaseWallet, type FeeOptions, type SimulateViaEntrypointOptions } from './base_wallet.js';
|
|
2
2
|
export { simulateViaNode, buildMergedSimulationResult, extractOptimizablePublicStaticCalls } from './utils.js';
|
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
|
+
}
|