-
Notifications
You must be signed in to change notification settings - Fork 6
/
wallet.ts
439 lines (411 loc) · 18.3 KB
/
wallet.ts
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
import {
Account,
AccountAuthenticator,
AnyRawTransaction,
Aptos,
AptosConfig,
Network,
SigningScheme
} from '@aptos-labs/ts-sdk'
import {
APTOS_CHAINS,
AccountInfo,
AptosConnectMethod,
AptosDisconnectMethod,
AptosGetAccountMethod,
AptosGetNetworkMethod,
AptosOnAccountChangeMethod,
AptosSignMessageInput,
AptosSignMessageMethod,
AptosSignMessageOutput,
AptosSignTransactionMethod,
AptosWallet,
IdentifierArray,
NetworkInfo,
UserResponse,
registerWallet,
AptosWalletAccount,
AptosOnNetworkChangeMethod,
AptosFeatures,
UserResponseStatus
} from '../'
/**
* This class is a template you can modify to implement an AIP-62 Wallet.
*
* Sections of the code which need to be revised will be marked with a "REVISION" comment.
* We recommend using the REVISION comments like a checklist and deleting them as you go.
* Ex. REVISION - Update this section.
*
* Function implementations are for DEMONSTRATION PURPOSES ONLY. Please ensure you rewrite all features
* to use your Wallet as the method of communicating on-chain.
*
* For a working implementation of this example, see the next-js example app here: https://github.com/aptos-labs/aptos-wallet-adapter/tree/main/apps/nextjs-example
* (And more specifically, see https://github.com/aptos-labs/aptos-wallet-adapter/blob/main/apps/nextjs-example/src/utils/standardWallet.ts)
*/
/**
* Interface of a **WalletAccount**, also referred to as an **Account**.
*
* An account is a _read-only data object_ that is provided from the Wallet to the app, authorizing the app to use it.
*
* The app can use an account to display and query information from a chain.
*
* The app can also act using an account by passing it to functions of the Wallet.
*
* Wallets may use or extend {@link "@wallet-standard/wallet".ReadonlyWalletAccount} which implements this interface.
*
*/
// REVISION - Replace the "MyWallet" in "MyWalletAccount" with the name of your wallet. Ex. "PetraAccount"
export class MyWalletAccount implements AptosWalletAccount {
/** Address of the account, corresponding with a public key. */
address: string;
/** Public key of the account, corresponding with a secret key to use. */
publicKey: Uint8Array;
/**
* Chains supported by the account.
*
* This must be a subset of ["aptos:devnet", "aptos:testnet", "aptos:localnet", "aptos:mainnet"].
*
* It is recommended to support at least ["aptos:devnet", "aptos:testnet", and "aptos:mainnet"].
*/
chains: IdentifierArray = APTOS_CHAINS
/**
* Function names of features that are supported for this Wallet's account object.
*/
features: IdentifierArray;
/** The signing scheme used for the private key of the address. Ex. SigningScheme.Ed25519 */
signingScheme: SigningScheme;
/** Optional user-friendly descriptive label or name for the account. This may be displayed by the app. */
label?: string;
/**
* Optional user-friendly icon for the account. This may be displayed by the app.
*/
icon?:
| `data:image/svg+xml;base64,${string}`
| `data:image/webp;base64,${string}`
| `data:image/png;base64,${string}`
| `data:image/gif;base64,${string}`
| undefined;
// REVISION - Update this constructor to use values your wallet supports.
constructor(account: Account) {
this.address = account.accountAddress.toString();
this.publicKey = account.publicKey.toUint8Array();
// REVISION - Choose which chains your wallet supports. This may only be subset of all Aptos networks.
this.chains = APTOS_CHAINS; // ["aptos:devnet", "aptos:testnet", "aptos:localnet", "aptos:mainnet"]
/**
* REVISION - Ensure this signing scheme matches the encoding used to generate your private key.
*/
this.signingScheme = account.signingScheme;
}
}
/**
* REVISION - This class needs to be extensively customized to match the details of your wallet.
*
* 1. MyWallet should be renamed to be the name of your wallet. Ex. For Petra, MyWallet should be named "PetraWallet". (Be sure to also update references to "MyWallet" in this file.)
* 2. Update the values of this class to match your Wallet's deatils.
* 3. Implement each of the features below. (Including adding implementations for any additional required features that you can find here in the "AptosFeatures" type: https://github.com/aptos-labs/wallet-standard/blob/main/src/features/index.ts)
*/
export class MyWallet implements AptosWallet {
// REVISION - Include the link to create an account using your wallet or your primary website. (Ex. https://chromewebstore.google.com/detail/petra-aptos-wallet/ejjladinnckdgjemekebdpeokbikhfci?hl=en)
readonly url: string = "https://aptos.dev";
// This should be updated whenever you release a new implementation of "MyWallet"
readonly version = "1.0.0";
// REVISION - Change the name to the name of your Wallet. (Ex. "Petra")
readonly name: string = "Aptos Burner";
/**
* REVISION - Set the icon to be a base64 encoding of your Wallet's logo.
*
* The icon data must be of the format:
* 1. "data:image/"
* 2. The icon's file extension, which must be one of:
* - "svg+xml"
* - "webp"
* - "png"
* - "gif"
* 3. ";base64,"
* 4. The base64 encoding of the image file.
*
* See the current value of icon for an example of this format.
*/
readonly icon =
"data:image/png;base64,iVBORw0KGgoAAAANSUhE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/P/yvHxlGxTYPh3tOn4gMUYN2g4FPc509DAdYFqvxZh1ArhwKsg6rSVzTHvywU4EeoqnyPTxKnAKuCVo4iD4s6ARwhTwGWoTrk8e3bIE4IH4cCVCDI1U6dL1/K73Eh4B727ctCASoQ6MBa9zJwJtA4FMA4FMA4FMA4FMA4FMA4FMA4FMA47Qtg4P/n1Uz7AgQ8zeoD7Qug5KQMq+joApgFWkNHEWhwEUYLFMA4OgRQdGCCNXQIUG28II2jZyKIWaAV9Aig7OgUK+gRAMH36ImaUNC1FoDt1swCjaJLAAQfT9mQxtC3GohugCOCxtC5HIyHLNkVNIJOATAv4Mnz9b6jd0MIhoWsB2pH944gPHmLkQGpDf1bwtAVUILa8GNPICRgd1AL/mwKRXfA0cHa8WtXMArDfp8bSdeIf9vCEfxHj8psQBF+GH/PB0A2wIzhrVsih4ciOztCVsfvAyKQAVAbYPr44EDk6Ehkd1fI8oRxQggKQ2QEXMgEe3ulELhvbQmZT3hHxFRn+1Tn/UAAZAWIUXUTHz4IKQn/jCBkB6Pn/ywDHw41DgUwDgRIhVgljSWKzoXYJM+dAFmWCrHKeewsOBViExd71AAjd10IsUYaDYdnsfty4Uz4U4g1zvClHAbm+e9CbJFlfdwKAVwWSJ0EfwixwrCIuYxPBOV5T1gLWCCtWj+4EqCoBbLsFyFhk2UPq9YPJqaCURW6W19IqPRdjCeG/dGsd+Xdbs/dToSERD8aDHrTP4zmvZsSBMXM4INo0afyTudY4vg39zIR4iNFXXfZtc9k4XJw0V9k2R1OFHkIhvVZdn1R8MHCDDDx+zqdxK0c9tz1szAjaKWc1XUTe+OV/iKWFmAcJ8NtJ8Kxe7kvkCGKEiHN45Zz3b/9yN3/uVzUGxXD+RX4F56985hsqA6SAAAAAElFTkSuQmCC";
/**
* REVISION - Set the subset of Aptos chains your wallet supports.
* APTOS_CHAINS = ["aptos:devnet", "aptos:testnet", "aptos:localnet", "aptos:mainnet"]
* It is recommended to support at least "aptos:mainnet", "aptos:testnet", and "aptos:devnet".
*/
chains = APTOS_CHAINS;
/**
* The set of accounts that your Wallet has shared information for. These do NOT include private keys.
* This list is normally expanded during `aptos:connect` and reduced during `aptos:disconnect`.
* NOTE: For demonstration purposes, the template initializes a default account in the constructor,
* but that should NOT be carried into your final implementation of this template.
*/
accounts: MyWalletAccount[] = [];
// Local MyWallet class variables,
/**
* REVISION - These two variables likely should NOT be in your finalized plugin template.
* They are used throughout this example's feature implementations in order to show how you could
* implement each function.
*
* signer - This stores the private keys for an account on-chain. (Example purposes only)
* aptos - This handles the network connection. (Your wallet may have a different way of handling the on-chain connection than this Aptos instance)
*
* Remember: These two variables SHOULD LIKELY BE DELETED after you replace your implementations of each feature with ones that use your Wallet.
*/
signer: Account;
aptos: Aptos;
/**
* REVISION - List all features your wallet supports below.
* You will need to implement how your wallet supports each.
*
* In order to be compatible with the AIP-62 Wallet standard, ensure you are at least supporting all
* currently required features by checking the list of features in the `AptosFeatures` type here:
* https://github.com/aptos-labs/wallet-standard/blob/main/src/features/index.ts
*
* To find the names of features to pass into `this.features` below you can either go into the feature implementations
* and look at the <AptosFeature>NameSpace variable, or you can import the `AptosFeatures` type and see the names there.
* Ex. See `AptosSignTransactionNamespace` in https://github.com/aptos-labs/wallet-standard/blob/main/src/features/aptosSignTransaction.ts
*
* For additional customization, you may implement optional features.
* For the most support though, you should extend the wallet-standard to support additional features as part of the standard.
*/
get features(): AptosFeatures {
return {
"aptos:connect": {
version: "1.0.0",
connect: this.connect,
},
"aptos:network": {
version: "1.0.0",
network: this.network,
},
"aptos:disconnect": {
version: "1.0.0",
disconnect: this.disconnect,
},
"aptos:signTransaction": {
version: "1.0.0",
signTransaction: this.signTransaction,
},
"aptos:signMessage": {
version: "1.0.0",
signMessage: this.signMessage,
},
"aptos:onAccountChange": {
version: "1.0.0",
onAccountChange: this.onAccountChange,
},
"aptos:onNetworkChange": {
version: "1.0.0",
onNetworkChange: this.onNetworkChange,
},
"aptos:account": {
version: "1.0.0",
account: this.account,
},
};
}
/**
* REVISION - This constructor should be updated to support your Wallet's implementation of the supported features.
*
* The template code's constructor currently initializes `signer` to act as the private key for an account on-chain, and uses
* `aptos` to handle the on-chain connection.
*
*/
constructor() {
// THIS LOGIC SHOULD BE REPLACED. IT IS FOR EXAMPLE PURPOSES ONLY.
// Create a random signer for our stub implementations.
this.signer = Account.generate();
// We will use DEVNET since we can fund our test account via a faucet there.
const aptosConfig = new AptosConfig({
network: Network.DEVNET,
});
// Use the instance Aptos connection to process requests.
this.aptos = new Aptos(aptosConfig);
// Update our Wallet object to know that we are connected to this new signer.
this.accounts = [new MyWalletAccount(this.signer)];
}
/**
* REVISION - Implement this function using your Wallet.
*
* Look up the account info for the currently connected wallet address on the chosen network.
*
* @returns Return account info.
*/
account: AptosGetAccountMethod = async (): Promise<AccountInfo> => {
const account = new AccountInfo({
address: this.signer.accountAddress,
publicKey: this.signer.publicKey,
});
return Promise.resolve(account);
};
/**
* REVISION - Implement this function using your Wallet.
*
* Connect an account using this Wallet.
* This must wait for the user to sign in to the Wallet provider and confirm they are ok sharing
* details with the dapp.
*
* For demonstration purposes, this template example assumes the user is using the account generated in `signer`
* and assumes the user approved letting the dapp use the account information.
*
* Your implmentation should include a way to track which account was just connected. This likely will involve
* setting the `this.accounts` variable.
*
* @returns Whether the user approved connecting their account, and account info.
* @throws Error when unable to connect to the Wallet provider.
*/
connect: AptosConnectMethod = async (): Promise<
UserResponse<AccountInfo>
> => {
// THIS LOGIC SHOULD BE REPLACED. IT IS FOR EXAMPLE PURPOSES ONLY.
try {
await this.aptos.fundAccount({
accountAddress: this.signer.accountAddress,
amount: 1_000_000,
});
const account = new AccountInfo({
address: this.signer.accountAddress,
publicKey: this.signer.publicKey,
});
return {
status: UserResponseStatus.APPROVED,
args: account,
};
} catch (e) {
return {
status: UserResponseStatus.REJECTED
}
}
};
/**
* REVISION - Implement this function using your Wallet.
*
* Return the name, chainId, and url of the network connection your wallet is using to connect to the Aptos chain.
*
* @returns Which network the connected Wallet is pointing to.
*/
network: AptosGetNetworkMethod = async (): Promise<NetworkInfo> => {
// THIS LOGIC SHOULD BE REPLACED. IT IS FOR EXAMPLE PURPOSES ONLY.
// You may use getLedgerInfo() to determine which ledger your Wallet is connected to.
const network = await this.aptos.getLedgerInfo();
return {
// REVISION - Ensure the name and url match the chain_id your wallet responds with.
name: Network.DEVNET,
// REVISION - For mainnet and testnet is not recommended to make the getLedgerInfo() network call as the chain_id is fixed for those networks.
chainId: network.chain_id,
url: "https://fullnode.devnet.aptoslabs.com/v1",
};
};
/**
* REVISION - Implement this function using your Wallet.
*
* Remove the permission of the Wallet class to access the account that was connected.
*
* @returns Resolves when done cleaning up.
*/
disconnect: AptosDisconnectMethod = async (): Promise<void> => {
// THIS LOGIC SHOULD BE REPLACED. IT IS FOR EXAMPLE PURPOSES ONLY.
return Promise.resolve();
};
/**
* REVISION - Implement this function using your Wallet.
*
* @param transaction - A transaction that the user should have the ability to sign if they choose to.
* @param asFeePayer - Optionally, another this signature is acting as a fee-payer for the transaction being signed.
* @returns The result of whether the user chose to sign the transaction or not.
*/
signTransaction: AptosSignTransactionMethod = async (
transaction: AnyRawTransaction,
asFeePayer?: boolean,
): Promise<UserResponse<AccountAuthenticator>> => {
// THIS LOGIC SHOULD BE REPLACED. IT IS FOR EXAMPLE PURPOSES ONLY.
if (asFeePayer) {
const senderAuthenticator = this.aptos.transaction.signAsFeePayer({
signer: this.signer,
transaction,
});
return Promise.resolve({
status: UserResponseStatus.APPROVED,
args: senderAuthenticator,
});
}
const senderAuthenticator = this.aptos.transaction.sign({
signer: this.signer,
transaction,
});
return Promise.resolve({
status: UserResponseStatus.APPROVED,
args: senderAuthenticator,
});
};
/**
* REVISION - Implement this function using your Wallet.
*
* @param input - A message to sign with the private key of the connected account.
* @returns A user response either with a signed message, or the user rejecting to sign.
*/
signMessage: AptosSignMessageMethod = async (
input: AptosSignMessageInput,
): Promise<UserResponse<AptosSignMessageOutput>> => {
// THIS LOGIC SHOULD BE REPLACED. IT IS FOR EXAMPLE PURPOSES ONLY.
// 'Aptos' + application + address + nonce + chainId + message
const messageToSign = `Aptos
demoAdapter
${this.signer.accountAddress.toString()}
${input.nonce}
${input.chainId ?? (await this.network()).chainId}
${input.message}`;
const encodedMessageToSign = new TextEncoder().encode(messageToSign);
const signature = this.signer.sign(encodedMessageToSign);
return Promise.resolve({
status: UserResponseStatus.APPROVED,
args: {
address: this.signer.accountAddress.toString(),
fullMessage: messageToSign,
message: input.message,
nonce: input.nonce,
prefix: "APTOS",
signature: signature,
},
});
};
/**
* REVISION - Implement this function using your Wallet.
*
* An event which will be triggered anytime an Account changes.
*
* @returns when the logic is resolved.
*/
onAccountChange: AptosOnAccountChangeMethod = async (): Promise<void> => {
// THIS LOGIC SHOULD BE REPLACED. IT IS FOR EXAMPLE PURPOSES ONLY.
return Promise.resolve();
};
/**
* REVISION - Implement this function using your Wallet.
*
* When users indicate a Network change should occur, update your Wallet accordingly.
*
* @returns when the logic is resolved.
*/
onNetworkChange: AptosOnNetworkChangeMethod = async (): Promise<void> => {
// THIS LOGIC SHOULD BE REPLACED. IT IS FOR EXAMPLE PURPOSES ONLY.
return Promise.resolve();
};
}
/**
* REVISION - This section is ONLY for Browser Extension Wallets.
*
* Ensure that you import and call registerWallet on pageload in your Wallet's logic.
* This will enable any dapps that are using the AIP-62 Wallet Adapter package to detect
* your wallet and connect to it.
*
* The below registration is for example purposes only, and likely should not occur in the
* same file as your "MyWallet" implementation in practice since registerWallet must be called
* when the page is loaded.
*/
(function () {
if (typeof window === "undefined") return;
const myWallet = new MyWallet();
registerWallet(myWallet);
})();