-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathproofChallenge.ts
More file actions
75 lines (62 loc) · 2.74 KB
/
proofChallenge.ts
File metadata and controls
75 lines (62 loc) · 2.74 KB
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
/* eslint-disable no-console */
/* eslint-disable max-len */
import { Account, Aptos, AptosConfig, Network, NetworkToNetworkName, MoveVector, U8 } from "@aptos-labs/ts-sdk";
/**
* This example demonstrate the end-to-end flow of creating, signing and submitting
* a proog challenge to the Aptos chain
*/
// Setup the client
const APTOS_NETWORK: Network = NetworkToNetworkName[process.env.APTOS_NETWORK ?? Network.LOCAL];
const config = new AptosConfig({ network: APTOS_NETWORK });
const aptos = new Aptos(config);
async function main() {
// Create accounts
const fromAccount = Account.generate();
const newAccount = Account.generate();
// Fund and create the accounts on chain
await aptos.fundAccount({ accountAddress: fromAccount.accountAddress, amount: 1_000_000_000 });
await aptos.fundAccount({ accountAddress: newAccount.accountAddress, amount: 1_000_000_000 });
const accountInfo = await aptos.getAccountInfo({
accountAddress: fromAccount.accountAddress,
});
// Create a rotation proof challenge.
const challenge = await aptos.createProofChallenge({
struct: "0x1::account::RotationProofChallenge",
data: [
BigInt(accountInfo.sequence_number),
fromAccount.accountAddress,
accountInfo.authentication_key,
newAccount.publicKey.toUint8Array(),
],
});
// Display the challenge in a human readable format. This step is for
// any service who needs/wants to show the challenge to the account before
// they sign it.
const deserializedChallenge = await aptos.getProofChallenge({
struct: "0x1::account::RotationProofChallenge",
data: challenge.bcsToBytes(),
});
console.log("rotation proof challenge to sign on", deserializedChallenge);
// 1st account signs the challenge
const proofSignedByCurrentPrivateKey = aptos.signProofChallenge({ challenge, signer: fromAccount });
// 2nd account signs the challenge
const proofSignedByNewPrivateKey = aptos.signProofChallenge({ challenge, signer: newAccount });
// Submit challenge to chain
const transaction = await aptos.transaction.build.simple({
sender: fromAccount.accountAddress,
data: {
function: "0x1::account::rotate_authentication_key",
functionArguments: [
new U8(fromAccount.signingScheme), // from scheme
MoveVector.U8(fromAccount.publicKey.toUint8Array()),
new U8(newAccount.signingScheme), // to scheme
MoveVector.U8(newAccount.publicKey.toUint8Array()),
MoveVector.U8(proofSignedByCurrentPrivateKey.toUint8Array()),
MoveVector.U8(proofSignedByNewPrivateKey.toUint8Array()),
],
},
});
const response = await aptos.signAndSubmitTransaction({ signer: fromAccount, transaction });
await aptos.waitForTransaction({ transactionHash: response.hash });
}
main();