-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathkeyless.ts
112 lines (89 loc) · 3.98 KB
/
keyless.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
/* eslint-disable max-len */
/* eslint-disable no-console */
/**
* This example shows how to use the Keyless accounts on Aptos
*/
import { Account, AccountAddress, Aptos, AptosConfig, EphemeralKeyPair, Network } from "@aptos-labs/ts-sdk";
import * as readlineSync from "readline-sync";
const ALICE_INITIAL_BALANCE = 100_000_000;
const BOB_INITIAL_BALANCE = 100;
const TRANSFER_AMOUNT = 10_000;
/**
* Prints the balance of an account
* @param aptos
* @param name
* @param address
* @returns {Promise<*>}
*
*/
const balance = async (aptos: Aptos, name: string, address: AccountAddress): Promise<any> => {
const amount = await aptos.getAccountAPTAmount({
accountAddress: address,
});
console.log(`${name}'s balance is: ${amount}`);
return amount;
};
const example = async () => {
// Set up the client
const network = Network.DEVNET;
const config = new AptosConfig({ network });
const aptos = new Aptos(config);
// Generate the ephemeral (temporary) key pair that will be used to sign transactions.
const aliceEphem = EphemeralKeyPair.generate();
console.log("\n=== Keyless Account Example ===\n");
const link = `https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount?redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground&prompt=consent&response_type=code&client_id=407408718192.apps.googleusercontent.com&scope=openid&access_type=offline&service=lso&o2v=2&theme=glif&flowName=GeneralOAuthFlow&nonce=${aliceEphem.nonce}`;
console.log(`${link}\n`);
console.log("1. Open the link above");
console.log("2. Log in with your Google account");
console.log("3. Click 'Exchange authorization code for tokens");
console.log("4. Copy the 'id_token' - (toggling 'Wrap lines' option at the bottom makes this easier)\n");
function inputJwt(): string {
return readlineSync.question("Paste the JWT (id_token) token here and press enter: ", {
hideEchoBack: false,
});
}
const jwt = inputJwt();
// Derive the Keyless Account from the JWT and ephemeral key pair.
const alice = await aptos.deriveKeylessAccount({
jwt,
ephemeralKeyPair: aliceEphem,
});
console.log("=== Addresses ===\n");
console.log(`Alice's keyless account address is: ${alice.accountAddress}`);
console.log(`Alice's nonce is: ${aliceEphem.nonce}`);
console.log(`Alice's ephemeral public key is: ${aliceEphem.getPublicKey().toString()}`);
const bob = Account.generate();
console.log(`Bob's address is: ${bob.accountAddress}`);
// Fund the accounts
console.log("\n=== Funding accounts ===\n");
await aptos.fundAccount({
accountAddress: alice.accountAddress,
amount: ALICE_INITIAL_BALANCE,
});
await aptos.fundAccount({
accountAddress: bob.accountAddress,
amount: BOB_INITIAL_BALANCE,
options: { waitForIndexer: false },
});
// // Show the balances
console.log("\n=== Balances ===\n");
const aliceBalance = await balance(aptos, "Alice", alice.accountAddress);
const bobBalance = await balance(aptos, "Bob", bob.accountAddress);
// Transfer between users
const transaction = await aptos.transferCoinTransaction({
sender: alice.accountAddress,
recipient: bob.accountAddress,
amount: TRANSFER_AMOUNT,
});
const committedTxn = await aptos.signAndSubmitTransaction({ signer: alice, transaction });
await aptos.waitForTransaction({ transactionHash: committedTxn.hash });
console.log(`\nCommitted transaction:\nhttps://explorer.aptoslabs.com/txn/${committedTxn.hash}?network=${network}`);
console.log("\n=== Balances after transfer ===\n");
const newAliceBalance = await balance(aptos, "Alice", alice.accountAddress);
const newBobBalance = await balance(aptos, "Bob", bob.accountAddress);
// Bob should have the transfer amount
if (TRANSFER_AMOUNT !== newBobBalance - bobBalance) throw new Error("Bob's balance after transfer is incorrect");
// Alice should have the remainder minus gas
if (TRANSFER_AMOUNT >= aliceBalance - newAliceBalance) throw new Error("Alice's balance after transfer is incorrect");
};
example();