@aztec/wallet-sdk 0.0.1-commit.d1f2d6c → 0.0.1-commit.d20b825a7
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 +93 -35
- package/dest/base-wallet/base_wallet.d.ts.map +1 -1
- package/dest/base-wallet/base_wallet.js +253 -69
- package/dest/base-wallet/index.d.ts +3 -2
- package/dest/base-wallet/index.d.ts.map +1 -1
- package/dest/base-wallet/index.js +1 -0
- package/dest/base-wallet/utils.d.ts +50 -0
- package/dest/base-wallet/utils.d.ts.map +1 -0
- package/dest/base-wallet/utils.js +133 -0
- 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 +4 -6
- package/dest/extension/provider/extension_wallet.d.ts.map +1 -1
- package/dest/extension/provider/extension_wallet.js +9 -2
- 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 +6 -5
- 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 +48 -18
- package/dest/types.d.ts +14 -2
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +4 -0
- package/package.json +19 -9
- package/src/base-wallet/base_wallet.ts +347 -122
- package/src/base-wallet/index.ts +7 -1
- package/src/base-wallet/utils.ts +240 -0
- package/src/crypto.ts +104 -0
- package/src/extension/provider/extension_wallet.ts +13 -10
- 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 +5 -4
- package/src/manager/wallet_manager.ts +55 -23
- package/src/types.ts +13 -0
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import type { AztecNode } from '@aztec/aztec.js/node';
|
|
2
|
+
import { TxSimulationResultWithAppOffset } from '@aztec/aztec.js/wallet';
|
|
3
|
+
import { MAX_ENQUEUED_CALLS_PER_CALL } from '@aztec/constants';
|
|
4
|
+
import type { ChainInfo } from '@aztec/entrypoints/interfaces';
|
|
5
|
+
import { makeTuple } from '@aztec/foundation/array';
|
|
6
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
7
|
+
import type { Tuple } from '@aztec/foundation/serialize';
|
|
8
|
+
import type { ContractNameResolver } from '@aztec/pxe/client/lazy';
|
|
9
|
+
import { displayDebugLogs } from '@aztec/pxe/client/lazy';
|
|
10
|
+
import { generateSimulatedProvingResult } from '@aztec/pxe/simulator';
|
|
11
|
+
import { type FunctionCall, FunctionSelector } from '@aztec/stdlib/abi';
|
|
12
|
+
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
13
|
+
import type { GasSettings } from '@aztec/stdlib/gas';
|
|
14
|
+
import {
|
|
15
|
+
ClaimedLengthArray,
|
|
16
|
+
CountedPublicCallRequest,
|
|
17
|
+
PrivateCircuitPublicInputs,
|
|
18
|
+
PublicCallRequest,
|
|
19
|
+
} from '@aztec/stdlib/kernel';
|
|
20
|
+
import { ChonkProof } from '@aztec/stdlib/proofs';
|
|
21
|
+
import {
|
|
22
|
+
type BlockHeader,
|
|
23
|
+
type ExecutionPayload,
|
|
24
|
+
HashedValues,
|
|
25
|
+
PrivateCallExecutionResult,
|
|
26
|
+
PrivateExecutionResult,
|
|
27
|
+
PublicSimulationOutput,
|
|
28
|
+
Tx,
|
|
29
|
+
TxContext,
|
|
30
|
+
TxSimulationResult,
|
|
31
|
+
} from '@aztec/stdlib/tx';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Splits an execution payload into a leading prefix of public static calls
|
|
35
|
+
* (eligible for direct node simulation) and the remaining calls.
|
|
36
|
+
*
|
|
37
|
+
* Only a leading run of public static calls is eligible for optimization.
|
|
38
|
+
* Any non-public-static call may enqueue public state mutations
|
|
39
|
+
* (e.g. private calls can enqueue public calls), so all calls that follow
|
|
40
|
+
* must go through the normal simulation path to see the correct state.
|
|
41
|
+
*
|
|
42
|
+
*/
|
|
43
|
+
export function extractOptimizablePublicStaticCalls(payload: ExecutionPayload): {
|
|
44
|
+
/** Leading public static calls eligible for direct node simulation. */
|
|
45
|
+
optimizableCalls: FunctionCall[];
|
|
46
|
+
/** All remaining calls. */
|
|
47
|
+
remainingCalls: FunctionCall[];
|
|
48
|
+
} {
|
|
49
|
+
const splitIndex = payload.calls.findIndex(call => !call.isPublicStatic());
|
|
50
|
+
const boundary = splitIndex === -1 ? payload.calls.length : splitIndex;
|
|
51
|
+
return {
|
|
52
|
+
optimizableCalls: payload.calls.slice(0, boundary),
|
|
53
|
+
remainingCalls: payload.calls.slice(boundary),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Simulates a batch of public static calls by bypassing account entrypoint and private execution,
|
|
59
|
+
* directly constructing a minimal Tx and calling node.simulatePublicCalls.
|
|
60
|
+
*
|
|
61
|
+
* @param node - The Aztec node to simulate on.
|
|
62
|
+
* @param publicStaticCalls - Array of public static function calls (max MAX_ENQUEUED_CALLS_PER_CALL).
|
|
63
|
+
* @param from - The account address making the calls.
|
|
64
|
+
* @param chainInfo - Chain information (chainId and version).
|
|
65
|
+
* @param gasSettings - Gas settings for the transaction.
|
|
66
|
+
* @param blockHeader - Block header to use as anchor.
|
|
67
|
+
* @param skipFeeEnforcement - Whether to skip fee enforcement during simulation.
|
|
68
|
+
* @returns TxSimulationResult with public return values.
|
|
69
|
+
*/
|
|
70
|
+
async function simulateBatchViaNode(
|
|
71
|
+
node: AztecNode,
|
|
72
|
+
publicStaticCalls: FunctionCall[],
|
|
73
|
+
from: AztecAddress,
|
|
74
|
+
chainInfo: ChainInfo,
|
|
75
|
+
gasSettings: GasSettings,
|
|
76
|
+
blockHeader: BlockHeader,
|
|
77
|
+
skipFeeEnforcement: boolean,
|
|
78
|
+
getContractName: ContractNameResolver,
|
|
79
|
+
): Promise<TxSimulationResult> {
|
|
80
|
+
const txContext = new TxContext(chainInfo.chainId, chainInfo.version, gasSettings);
|
|
81
|
+
|
|
82
|
+
const publicFunctionCalldata: HashedValues[] = [];
|
|
83
|
+
for (const call of publicStaticCalls) {
|
|
84
|
+
const calldata = await HashedValues.fromCalldata([call.selector.toField(), ...call.args]);
|
|
85
|
+
publicFunctionCalldata.push(calldata);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const publicCallRequests = makeTuple(MAX_ENQUEUED_CALLS_PER_CALL, i => {
|
|
89
|
+
const call = publicStaticCalls[i];
|
|
90
|
+
if (!call) {
|
|
91
|
+
return CountedPublicCallRequest.empty();
|
|
92
|
+
}
|
|
93
|
+
const publicCallRequest = new PublicCallRequest(from, call.to, call.isStatic, publicFunctionCalldata[i]!.hash);
|
|
94
|
+
// Counter starts at 1 (minRevertibleSideEffectCounter) so all calls are revertible
|
|
95
|
+
return new CountedPublicCallRequest(publicCallRequest, i + 1);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
const publicCallRequestsArray: ClaimedLengthArray<CountedPublicCallRequest, typeof MAX_ENQUEUED_CALLS_PER_CALL> =
|
|
99
|
+
new ClaimedLengthArray(
|
|
100
|
+
publicCallRequests as Tuple<CountedPublicCallRequest, typeof MAX_ENQUEUED_CALLS_PER_CALL>,
|
|
101
|
+
publicStaticCalls.length,
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
const publicInputs = PrivateCircuitPublicInputs.from({
|
|
105
|
+
...PrivateCircuitPublicInputs.empty(),
|
|
106
|
+
anchorBlockHeader: blockHeader,
|
|
107
|
+
txContext: txContext,
|
|
108
|
+
publicCallRequests: publicCallRequestsArray,
|
|
109
|
+
startSideEffectCounter: new Fr(0),
|
|
110
|
+
endSideEffectCounter: new Fr(publicStaticCalls.length + 1),
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// Minimal entrypoint structure — no real private execution, just public call requests
|
|
114
|
+
const emptyEntrypoint = new PrivateCallExecutionResult(
|
|
115
|
+
Buffer.alloc(0),
|
|
116
|
+
Buffer.alloc(0),
|
|
117
|
+
new Map(),
|
|
118
|
+
publicInputs,
|
|
119
|
+
[],
|
|
120
|
+
new Map(),
|
|
121
|
+
[],
|
|
122
|
+
[],
|
|
123
|
+
[],
|
|
124
|
+
[],
|
|
125
|
+
[],
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
const privateResult = new PrivateExecutionResult(emptyEntrypoint, Fr.random(), publicFunctionCalldata);
|
|
129
|
+
|
|
130
|
+
const provingResult = await generateSimulatedProvingResult(
|
|
131
|
+
privateResult,
|
|
132
|
+
(_contractAddress: AztecAddress, _functionSelector: FunctionSelector) => Promise.resolve(''),
|
|
133
|
+
node,
|
|
134
|
+
1, // minRevertibleSideEffectCounter
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
provingResult.publicInputs.feePayer = from;
|
|
138
|
+
|
|
139
|
+
const tx = await Tx.create({
|
|
140
|
+
data: provingResult.publicInputs,
|
|
141
|
+
chonkProof: ChonkProof.empty(),
|
|
142
|
+
contractClassLogFields: [],
|
|
143
|
+
publicFunctionCalldata: publicFunctionCalldata,
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
const publicOutput = await node.simulatePublicCalls(tx, skipFeeEnforcement);
|
|
147
|
+
|
|
148
|
+
if (publicOutput.revertReason) {
|
|
149
|
+
throw publicOutput.revertReason;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Display debug logs from the public simulation.
|
|
153
|
+
await displayDebugLogs(publicOutput.debugLogs, getContractName);
|
|
154
|
+
|
|
155
|
+
return new TxSimulationResult(privateResult, provingResult.publicInputs, publicOutput, undefined);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Simulates public static calls by splitting them into batches of MAX_ENQUEUED_CALLS_PER_CALL
|
|
160
|
+
* and sending each batch directly to the node.
|
|
161
|
+
*
|
|
162
|
+
* @param node - The Aztec node to simulate on.
|
|
163
|
+
* @param publicStaticCalls - Array of public static function calls to optimize.
|
|
164
|
+
* @param from - The account address making the calls.
|
|
165
|
+
* @param chainInfo - Chain information (chainId and version).
|
|
166
|
+
* @param gasSettings - Gas settings for the transaction.
|
|
167
|
+
* @param blockHeader - Block header to use as anchor.
|
|
168
|
+
* @param skipFeeEnforcement - Whether to skip fee enforcement during simulation.
|
|
169
|
+
* @returns Array of TxSimulationResult, one per batch.
|
|
170
|
+
*/
|
|
171
|
+
export async function simulateViaNode(
|
|
172
|
+
node: AztecNode,
|
|
173
|
+
publicStaticCalls: FunctionCall[],
|
|
174
|
+
from: AztecAddress,
|
|
175
|
+
chainInfo: ChainInfo,
|
|
176
|
+
gasSettings: GasSettings,
|
|
177
|
+
blockHeader: BlockHeader,
|
|
178
|
+
skipFeeEnforcement: boolean = true,
|
|
179
|
+
getContractName: ContractNameResolver,
|
|
180
|
+
): Promise<TxSimulationResult[]> {
|
|
181
|
+
const batches: FunctionCall[][] = [];
|
|
182
|
+
|
|
183
|
+
for (let i = 0; i < publicStaticCalls.length; i += MAX_ENQUEUED_CALLS_PER_CALL) {
|
|
184
|
+
batches.push(publicStaticCalls.slice(i, i + MAX_ENQUEUED_CALLS_PER_CALL));
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const results: TxSimulationResult[] = [];
|
|
188
|
+
|
|
189
|
+
for (const batch of batches) {
|
|
190
|
+
const result = await simulateBatchViaNode(
|
|
191
|
+
node,
|
|
192
|
+
batch,
|
|
193
|
+
from,
|
|
194
|
+
chainInfo,
|
|
195
|
+
gasSettings,
|
|
196
|
+
blockHeader,
|
|
197
|
+
skipFeeEnforcement,
|
|
198
|
+
getContractName,
|
|
199
|
+
);
|
|
200
|
+
results.push(result);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return results;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Merges simulation results from the optimized (public static) and normal paths.
|
|
208
|
+
* Since optimized calls are always a leading prefix, return values are simply
|
|
209
|
+
* concatenated: optimized first, then normal.
|
|
210
|
+
* Stats are taken from the normal result only (the optimized path doesn't produce them).
|
|
211
|
+
*
|
|
212
|
+
* @param optimizedResults - Results from optimized public static call batches.
|
|
213
|
+
* @param normalResult - Result from normal simulation (null if all calls were optimized).
|
|
214
|
+
* @returns A single TxSimulationResult with return values in original call order.
|
|
215
|
+
*/
|
|
216
|
+
export function buildMergedSimulationResult(
|
|
217
|
+
optimizedResults: TxSimulationResult[],
|
|
218
|
+
normalResult: TxSimulationResultWithAppOffset | null,
|
|
219
|
+
): TxSimulationResultWithAppOffset {
|
|
220
|
+
const optimizedReturnValues = optimizedResults.flatMap(r => r.publicOutput?.publicReturnValues ?? []);
|
|
221
|
+
const normalReturnValues = normalResult?.publicOutput?.publicReturnValues ?? [];
|
|
222
|
+
const allReturnValues = [...optimizedReturnValues, ...normalReturnValues];
|
|
223
|
+
|
|
224
|
+
const baseResult: TxSimulationResult = normalResult ?? optimizedResults[0];
|
|
225
|
+
|
|
226
|
+
const mergedPublicOutput: PublicSimulationOutput | undefined = baseResult.publicOutput
|
|
227
|
+
? {
|
|
228
|
+
...baseResult.publicOutput,
|
|
229
|
+
publicReturnValues: allReturnValues,
|
|
230
|
+
}
|
|
231
|
+
: undefined;
|
|
232
|
+
|
|
233
|
+
const merged = new TxSimulationResult(
|
|
234
|
+
baseResult.privateExecutionResult,
|
|
235
|
+
baseResult.publicInputs,
|
|
236
|
+
mergedPublicOutput,
|
|
237
|
+
normalResult?.stats,
|
|
238
|
+
);
|
|
239
|
+
return TxSimulationResultWithAppOffset.fromResultAndOffset(merged, normalResult?.appCallOffset ?? 0);
|
|
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.
|
|
@@ -109,7 +104,7 @@ export class ExtensionWallet {
|
|
|
109
104
|
sharedKey: CryptoKey,
|
|
110
105
|
chainInfo: ChainInfo,
|
|
111
106
|
appId: string,
|
|
112
|
-
):
|
|
107
|
+
): ExtensionWallet {
|
|
113
108
|
const wallet = new ExtensionWallet(chainInfo, appId, extensionId, port, sharedKey);
|
|
114
109
|
|
|
115
110
|
// Set up message handler for encrypted responses and unencrypted control messages
|
|
@@ -127,8 +122,10 @@ export class ExtensionWallet {
|
|
|
127
122
|
wallet.port.start();
|
|
128
123
|
|
|
129
124
|
return new Proxy(wallet, {
|
|
130
|
-
get: (target, prop) => {
|
|
131
|
-
if (
|
|
125
|
+
get: (target, prop, receiver) => {
|
|
126
|
+
if (prop === 'asWallet') {
|
|
127
|
+
return () => receiver as unknown as Wallet;
|
|
128
|
+
} else if (schemaHasMethod(WalletSchema, prop.toString())) {
|
|
132
129
|
return async (...args: unknown[]) => {
|
|
133
130
|
const result = await target.postMessage({
|
|
134
131
|
type: prop.toString() as keyof FunctionsOf<Wallet>,
|
|
@@ -140,7 +137,13 @@ export class ExtensionWallet {
|
|
|
140
137
|
return target[prop as keyof ExtensionWallet];
|
|
141
138
|
}
|
|
142
139
|
},
|
|
143
|
-
})
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
asWallet(): Wallet {
|
|
144
|
+
// Overridden by the proxy in create() to return the proxy itself.
|
|
145
|
+
// This body is never reached when accessed through create().
|
|
146
|
+
return this as unknown as Wallet;
|
|
144
147
|
}
|
|
145
148
|
|
|
146
149
|
/**
|