forked from coinbase/waas-sdk-react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
484 lines (442 loc) · 16.3 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// Copyright (c) 2018-2023 Coinbase, Inc. <https://www.coinbase.com/>
// Licensed under the Apache License, version 2.0
import { NativeModules, Platform } from 'react-native';
const LINKING_ERROR =
`The package 'react-native-waas-sdk' doesn't seem to be linked. Make sure: \n\n` +
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
'- You rebuilt the app after installing the package\n' +
'- You are not using Expo Go\n';
/**
* The native hook into the WaaS MPC SDK.
*/
const MPCSdk = NativeModules.MPCSdk
? NativeModules.MPCSdk
: new Proxy(
{},
{
get() {
throw new Error(LINKING_ERROR);
},
}
);
/**
* Initializes the MPC SDK. This function must be invoked before
* any MPC SDK methods are called.
* @returns A promise with the string "success" on successful initialization; a rejection
* otherwise.
*/
export function initMPCSdk(isSimulator?: boolean): Promise<string> {
return MPCSdk.initialize(isSimulator);
}
/**
Bootstraps the Device with the given passcode. The passcode is used to generate a private/public key pair
that encodes the back-up material for WaaS keys created on this Device. This function should be called exactly once per
Device per application, and should be called before the Device is registered with GetRegistrationData.
It is the responsibility of the application to track whether BootstrapDevice has been called for the Device.
* @param passcode: Passcode to protect all key materials in the secure enclave.
* @returns A promise with the string "bootstrap complete" on successful initialization or a rejection otherwise.
*/
export function bootstrapDevice(passcode: string): Promise<string> {
return MPCSdk.bootstrapDevice(passcode);
}
/**
* Retrieves the data required to call RegisterDeviceAPI on MPCKeyService.
* @returns A promise with the RegistrationData on success; a rejection otherwise.
*/
export function getRegistrationData(): Promise<String> {
return MPCSdk.getRegistrationData();
}
/**
* ComputeMPCOperation computes an MPC operation, given mpcData from the response of ListMPCOperations API on MPCKeyService.
* @param mpcData The mpcData from ListMPCOperationsResponse on MPCKeyService.
* @returns A promise with the string "success" on successful MPC computation; a rejection otherwise.
*/
export function computeMPCOperation(mpcData: string): Promise<String> {
return MPCSdk.computeMPCOperation(mpcData);
}
/**
* The native hook into the WaaS PoolService.
*/
const PoolService = NativeModules.PoolService
? NativeModules.PoolService
: new Proxy(
{},
{
get() {
throw new Error(LINKING_ERROR);
},
}
);
/**
* The Pool resource.
*/
export type Pool = {
// The resource name of the Pool.
// Format: pools/{pool_id}
name: string;
// A user-readable name for the Pool set by the user.
// Example: 'Acme Co. Retail Trading'
displayName: string;
};
/**
* Initializes the PoolService with Cloud API Key. This function must be invoked before
* any PoolService functions are called.
* @param apiKeyName The API key name.
* @param privateKey The private key.
* @param url The URL of the PoolService. Optional.
* @returns A promise with the string "success" on successful initialization; a rejection
* otherwise.
*/
export function initPoolService(
apiKeyName: string,
privateKey: string
): Promise<string> {
return PoolService.initialize(apiKeyName, privateKey);
}
/**
* Creates a Pool. Call this method before creating any resources scoped to a Pool.
* @param displayName A user-readable name for the Pool.
* @param poolID The ID to use for the Pool, which will become the final component of
* the resource name. If not provided, the server will assign a Pool ID automatically.
* @returns A promise with the Pool on success; a rejection otherwise.
*/
export function createPool(
displayName: string,
poolID?: string
): Promise<Pool> {
let poolIDString = poolID;
if (poolIDString === undefined) {
poolIDString = '';
}
return PoolService.createPool(displayName, poolIDString);
}
/**
* The native hook into the WaaS MPCKeyService.
*/
const MPCKeyService = NativeModules.MPCKeyService
? NativeModules.MPCKeyService
: new Proxy(
{},
{
get() {
throw new Error(LINKING_ERROR);
},
}
);
/**
* Initializes the MPCKeyService.
* This function must be invoked before any MPCKeyService functions are called.
* @returns A promise with the string "success" on successful initialization; a rejection
* otherwise.
*/
export function initMPCKeyService(
apiKeyName: string,
privateKey: string
): Promise<string> {
return MPCKeyService.initialize(apiKeyName, privateKey);
}
/**
* An object representing a pending CreateDeviceGroup operation.
* Also referred to as a "pending DeviceGroup"
*/
export type CreateDeviceGroupOperation = {
// The resource name of the DeviceGroup.
// Format: pools/{pool_id}/deviceGroups/{device_group_id}
DeviceGroup: string;
// The resource name of the Operation creating this DeviceGroup.
// The format: operations/{operation_id}
Operation: string;
// The resource name of the MPCOperation.
// Format: pools/{pool_id}/deviceGroups/{device_group_id}/mpcOperations/{mpc_operation_id}
MPCOperation: string;
// The MPCData associated with this operation. To process this operation, ComputeMPCOperation API has to be invoked with this data.
// Format: base64 encoded string.
MPCData: string;
};
/**
* The Device resource.
*/
export type Device = {
// The resource name of this Device.
// Format: devices/{device_id}
Name: string;
};
/**
* The Signature resource.
*/
export type Signature = {
// The resource name of the Signature.
// Format: pools/{pool_id}/deviceGroups/{device_group_id}/mpcKeys/{mpc_key_id}/signatures/{signature_id}
Name: string;
// The hex-encoded payload to be signed.
// In the case of transactions, this corresponds to the hash of the unsigned
// transaction.
Payload: string;
// The hex-encoded signed payload.
// In the case of transactions, this corresponds to the 65-byte V, R, S value.
// Note that this signed payload must be combined with a transaction object and then
// marshaled into RLP format before it can be broadcast on-chain.
SignedPayload: string;
};
/**
* An EIP-1559 transaction.
*/
export type Transaction = {
// The chain ID of the transaction as a "0x"-prefixed hex string.
ChainID: string;
// The nonce of the transaction.
Nonce: number;
// The EIP-1559 maximum priority fee per gas as a "0x"-prefixed hex string.
MaxPriorityFeePerGas: string;
// The EIP-1559 maximum fee per gas as a "0x"-prefixed hex string.
MaxFeePerGas: string;
// The maximum amount of gas to use on the transaction.
Gas: number;
// The checksummed address to which the transaction is addressed, as a "0x"-prefixed hex string.
// Note: This is NOT a WaaS Address resource of the form
// `networks/{networkID}/addresses/{addressID}.
To: string;
// The native value of the transaction as a "0x"-prefixed hex string.
Value: string;
// The hex-encoded data for the transaction.
Data: string;
};
/**
* A signed EIP-1559 transaction.
*/
export type SignedTransaction = {
// The unsigned Transaction.
Transaction: Transaction;
// The signature of the signed transaction.
// The Payload is the hash of the unsigned transaction, and the
// SignedPayload is the 65-byte V, R, S value.
// Both are hex strings.
Signature: Signature;
// RawTransaction is the RLP-encoded signed transaction.
// It is a hex string that can be broadcast on-chain.
RawTransaction: string;
// TransactionHash is the hash of the signed transaction.
// It is a hex string.
TransactionHash: string;
};
/**
* An object representing a pending CreateSignature MPC operation.
* Another name for this is a "pending Signature".
*/
export type CreateSignatureOperation = {
// The resource name of the DeviceGroup.
// Format: pools/{pool_id}/deviceGroups/{device_group_id}
DeviceGroup: string;
// The resource name of the Operation creating this DeviceGroup.
// The format: operations/{operation_id}
Operation: string;
// The resource name of the MPCOperation.
// Format: pools/{pool_id}/deviceGroups/{device_group_id}/mpcOperations/{mpc_operation_id}
MPCOperation: string;
// The MPCData associated with this operation. To process this operation, ComputeMPCOperation API has to be invoked with this data.
// Format: base64 encoded string.
MPCData: string;
// The hex-encoded payload to be signed.
Payload: string;
};
/**
* Registers the current Device.
* @returns A promise with the registered Device on success; a rejection otherwise.
*/
export function registerDevice(): Promise<Device> {
return MPCKeyService.registerDevice();
}
/**
* Polls for pending DeviceGroup (i.e. CreateDeviceGroup), and returns the first set that materializes.
* Only one DeviceGroup can be polled at a time; thus, this function must return (by calling either
* stopPollingForPendingDeviceGroup or computeMPCOperation) before another call is made to this function.
* @param deviceGroup The resource name of the DeviceGroup for which to poll the pending
* CreateDeviceGroupOperation.
* @param pollInterval The interval at which to poll for the pending operation in milliseconds.
* If not provided, a reasonable default will be used.
* @returns A promise with a list of the pending CreateDeviceGroupOperations on success; a rejection otherwise.
*/
export function pollForPendingDeviceGroup(
deviceGroup: string,
pollInterval?: number
): Promise<Array<CreateDeviceGroupOperation>> {
const pollIntervalToUse = pollInterval === undefined ? 200 : pollInterval;
return MPCKeyService.pollForPendingDeviceGroup(
deviceGroup,
pollIntervalToUse
);
}
/**
* Stops polling for pending DeviceGroup. This function should be called, e.g., before your app exits,
* screen changes, etc. This function is a no-op if the SDK is not currently polling for a pending DeviceGroup.
* @returns A promise with string "stoped polling for pending DeviceGroup" if polling is stopped successfully;
* a promise with the empty string otherwise.
*/
export function stopPollingForPendingDeviceGroup(): Promise<string> {
return MPCKeyService.stopPollingForPendingDeviceGroup();
}
/**
* Initiates an operation to create a Signature resource from the given Transaction using
* the given parent Key.
* @param parent The resource name of the parent Key.
* Format: pools/{pool_id}/deviceGroups/{device_group_id}/mpcKeys/{mpc_key_id}
* @param tx The transaction to sign.
* @returns A promise with the string "success" on successful initiation; a rejection
* otherwise.
*/
export function createSignatureFromTx(
parent: string,
tx: Transaction
): Promise<string> {
return MPCKeyService.createSignatureFromTx(parent, tx);
}
/**
* Polls for pending Signatures (i.e. CreateSignatureOperations), and returns the first set that materializes.
* Only one DeviceGroup can be polled at a time; thus, this function must return (by calling either
* stopPollingForPendingSignatures or processPendingSignature) before another call is made to this function.
* @param deviceGroup The resource name of the deviceGroup for which to poll the pending
* CreateSignatureOperation.
* @param pollInterval The interval at which to poll for the pending operation in milliseconds.
* If not provided, a reasonable default will be used.
* @returns A promise with a list of the pending Signatures on success; a rejection otherwise.
*/
export function pollForPendingSignatures(
deviceGroup: string,
pollInterval?: number
): Promise<Array<CreateSignatureOperation>> {
const pollIntervalToUse = pollInterval === undefined ? 200 : pollInterval;
return MPCKeyService.pollForPendingSignatures(deviceGroup, pollIntervalToUse);
}
/**
* Stops polling for pending Signatures. This function should be called, e.g., before your app exits,
* screen changes, etc. This function is a no-op if the SDK is not currently polling for a pending Signature.
* @returns A promise with string "stopped polling for pending Signatures" if polling is stopped successfully;
* a promise with the empty string otherwise.
*/
export function stopPollingForPendingSignatures(): Promise<string> {
return MPCKeyService.stopPollingForPendingSignatures();
}
/**
* Waits for a pending Signature.
* @param wallet The name of operation that created the Signature.
* @returns A promise with the Signature on success; a rejection otherwise.
*/
export function waitPendingSignature(operation: string): Promise<Signature> {
return MPCKeyService.waitPendingSignature(operation);
}
/**
* Obtains the signed transaction object based on the given inputs.
* @param unsignedTx The unsigned Transaction object.
* @param signature The Signature object obtained from the CreateSignature flow.
*/
export function getSignedTransaction(
unsignedTx: Transaction,
signature: Signature
): Promise<SignedTransaction> {
return MPCKeyService.getSignedTransaction(unsignedTx, signature);
}
/**
* The native hook into the WaaS MPCWalletService.
*/
const MPCWalletService = NativeModules.MPCWalletService
? NativeModules.MPCWalletService
: new Proxy(
{},
{
get() {
throw new Error(LINKING_ERROR);
},
}
);
/**
* The response for CreateMPCWallet.
*/
export type CreateMPCWalletResponse = {
// The resource name of the DeviceGroup associated with this MPCWallet.
// Format: pools/{pool_id}/deviceGroups/{device_group_id}
DeviceGroup: string;
// The resource name of the WaaS operation that creates this MPCWallet.
// Format: operations/{operation_id}
Operation: string;
};
/**
* The Address resource.
*/
export type Address = {
// The resource name of the Address.
Name: string;
// The address value - for example, a 0x-prefixed checksummed hexadecimal string.
Address: string;
// The resource names of the MPCKeys that back this Address.
// For EVM networks, there will be only one MPCKey.
MPCKeys: Array<string>;
// The resource name of the MPCWallet to which this Address belongs.
MPCWallet: string;
};
/**
* The MPCWallet resource.
*/
export type MPCWallet = {
// The resource name of the MPCWallet.
// Format: pools/{pool_id}/mpcWallets/{mpc_wallet_id}
Name: string;
// The resource name of the MPCKeyService DeviceGroup associated with this MPCWallet.
// The DeviceGroup will perform the underlying MPC operations.
// Format: pools/{pool_id}/deviceGroups/{device_group_id}
DeviceGroup: string;
};
/**
* Initializes the MPCWalletService with Cloud API Key. This function must be invoked before
* any MPCWalletService functions are called.
* @param apiKeyName The API key name.
* @param privateKey The private key.
* @param url The URL of the WalletService. Optional.
* @returns A promise with the string "success" on successful initialization; a rejection
* otherwise.
*/
export function initMPCWalletService(
apiKeyName: string,
privateKey: string
): Promise<string> {
return MPCWalletService.initialize(apiKeyName, privateKey);
}
/**
* Creates an MPCWallet.
* @param parent The resource name of the parent Pool.
* @param device The resource name of the Device.
* @returns A promise with the response on success; a rejection otherwise.
*/
export function createMPCWallet(
parent: string,
device: string
): Promise<CreateMPCWalletResponse> {
return MPCWalletService.createMPCWallet(parent, device);
}
/**
* Waits for a pending MPCWallet.
* @param wallet The name of operation that created the MPCWallet.
* @returns A promise with the MPCWallet on success; a rejection otherwise.
*/
export function waitPendingMPCWallet(operation: string): Promise<MPCWallet> {
return MPCWalletService.waitPendingMPCWallet(operation);
}
/**
* Generates an Address.
* @param wallet The resource name of the MPCWallet to create the Address in.
* @param network The resource name of Network to create the Address for.
* @returns A promise with the Address on success; a rejection otherwise.
*/
export function generateAddress(
wallet: string,
network: string
): Promise<Address> {
return MPCWalletService.generateAddress(wallet, network);
}
/**
* Gets an Address.
* @param name The resource name of the Address.
* @returns A promise with the Address on success; a rejection otherwise.
*/
export function getAddress(name: string): Promise<Address> {
return MPCWalletService.getAddress(name);
}