-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.ts
More file actions
128 lines (99 loc) · 4.86 KB
/
Main.ts
File metadata and controls
128 lines (99 loc) · 4.86 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
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
import KeyTrackerB, { uncompressLamport } from "./KeyTrackerB";
import { ethers } from "ethers";
import "dotenv/config"
import LambBedrockAuthentication from "./LambBedrockAuthentication";
const getCurrentState = async (secret: string, rpcEndpoint: string): Promise<any> => {
const provider = new ethers.providers.JsonRpcProvider(rpcEndpoint);
const caller: ethers.Wallet = (new ethers.Wallet(secret)).connect(provider);
return {
balance: await caller.getBalance(),
}
}
async function main() {
const initialState = await getCurrentState(process.env.ECDSA_SECRET!, process.env.RPC_ENDPOINT!);
const lba = new LambBedrockAuthentication(
process.env.ECDSA_SECRET!,
process.env.RPC_ENDPOINT!,
process.env.WALLET_FACTORY!,
1,
);
// STEP 1: Create Account
console.log(`STEP 1: Create Account`)
const [address, kt, txHash] = await lba.createAccount();
const postCreationState = await getCurrentState(process.env.ECDSA_SECRET!, process.env.RPC_ENDPOINT!);
const pad = 40;
console.log(`${'Address '.padEnd(pad, '.')} ${address}`);
console.log(`${'Gas'.padEnd(pad, '.')} ${ethers.utils.formatEther(initialState.balance.sub(postCreationState.balance))}`)
console.log(`${'Tx Hash '.padEnd(pad, '.')} ${txHash}`);
console.log(`\n`);
// STEP 2: Sign Message
console.log(`STEP 2: Sign Message`)
const salt = Buffer.from((ethers.BigNumber.from(ethers.utils.randomBytes(32)).toHexString()).slice(2), 'hex').toString('base64'); // salt improves privacy but does not guarantee it.
const message = `My email address is contact@pauli.group [salt: ${salt}]`; // this message could be anything including an public encryption key
{
// PROOF verify message reports false before signing
const [isValid] = await lba.verifyMessage(address, message);
if (isValid === true)
throw new Error('Message should not be valid yet');
}
const presignState = await getCurrentState(process.env.ECDSA_SECRET!, process.env.RPC_ENDPOINT!);
const [
messageHash,
localKeyCount,
contractKeyCount,
keyCountsMatch,
txHash2,
] = await lba.signMessageAndPostSignature(message, address, kt);
const postSignState = await getCurrentState(process.env.ECDSA_SECRET!, process.env.RPC_ENDPOINT!);
console.log(`${'Message '.padEnd(pad, '.')} ${messageHash}`);
console.log(`${'Local Key Count '.padEnd(pad, '.')} ${localKeyCount}`);
console.log(`${'Contract Key Count '.padEnd(pad, '.')} ${contractKeyCount}`);
console.log(`${'Key Counts Match '.padEnd(pad, '.')} ${keyCountsMatch ? 'Yes' : 'No'}`);
console.log(`${'Gas Paid'.padEnd(pad, '.')} ${ethers.utils.formatEther(presignState.balance.sub(postSignState.balance))}`)
console.log(`${'Tx Hash '.padEnd(pad, '.')} ${txHash2}`);
console.log(`\n`);
// STEP 3: Verify Message
console.log(`STEP 3: Verify Message`)
const [isValid] = await lba.verifyMessage(address, message);
console.log(`${'Message '.padEnd(pad, '.')} "${message}"`);
console.log(`${'Is valid '.padEnd(pad, '.')} ${isValid ? 'Yes' : 'No'}`);
console.log(`\n`);
// STEP 4: Add Keys
console.log(`STEP 4: Add Keys`)
const [
fee,
localKeyCount2,
contractKeyCount2,
keyCountsMatch2,
txHash3,
] = await lba.addKeys(address, kt);
console.log(`${'Fee '.padEnd(pad, '.')} ${ethers.utils.formatEther(fee)}`);
console.log(`${'Local Key Count '.padEnd(pad, '.')} ${localKeyCount2}`);
console.log(`${'Contract Key Count '.padEnd(pad, '.')} ${contractKeyCount2}`);
console.log(`${'Key Counts Match '.padEnd(pad, '.')} ${keyCountsMatch2 ? 'Yes' : 'No'}`);
console.log(`${'Tx Hash '.padEnd(pad, '.')} ${txHash3}`);
console.log(`\n`);
// STEP 5: Remove Keys
console.log(`STEP 5: Remove Keys`)
const allPKHs = (kt as KeyTrackerB).keys.map(uncompressLamport).map(key => key.pkh);
// select 5 at random
const positions = [20, 31, 45, 52, 66,]
const toRemove = positions.map(i => allPKHs[i])
const [
localKeyCount3,
contractKeyCount3,
keyCountsMatch3,
txHash4,
] = await lba.removeKeys(address, kt as KeyTrackerB, toRemove);
console.log(`${'Local Key Count '.padEnd(pad, '.')} ${localKeyCount3}`);
console.log(`${'Contract Key Count '.padEnd(pad, '.')} ${contractKeyCount3}`);
console.log(`${'Key Counts Match '.padEnd(pad, '.')} ${keyCountsMatch3 ? 'Yes' : 'No'}`);
console.log(`${'Tx Hash '.padEnd(pad, '.')} ${txHash4}`);
console.log(`\n`);
// SUMMERY
const finalState = await getCurrentState(process.env.ECDSA_SECRET!, process.env.RPC_ENDPOINT!);
const valueDelta = initialState.balance.sub(finalState.balance);
console.log(`SUMMERY`);
console.log(`${ 'Total operational cost'.padEnd(pad, '.')} ${ethers.utils.formatEther(valueDelta)}`);
}
main();