-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt-interactive.ts
92 lines (71 loc) · 2.6 KB
/
encrypt-interactive.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
#!/usr/bin/env ts-node
import * as readline from 'readline';
import * as util from 'util';
import { all } from "iso-3166-1";
import { Address, encrypt, encryptAddress } from "./enc";
import { exit } from 'process';
import { abi as EPSAbi } from "./EthereumPostalService.json";
import { Command } from "commander";
import { ethers } from 'ethers';
let DEFAULT_ADDR = "0x2156fcCff55637317D211B62318007309378fB95";
let program = new Command();
program.requiredOption("-r, --rpcUrl <rpcUrl>")
.option("-a, --address <address>", undefined, DEFAULT_ADDR)
.parse(process.argv);
run().then(_ => exit(1)).catch(err => { console.error(err); exit(-1) })
async function run() {
let opts = program.opts();
let provider = new ethers.providers.JsonRpcProvider(opts.rpcUrl);
let contract = new ethers.Contract(opts.address, EPSAbi, provider);
let pubkey = await contract.encryptionPubKey();
let isCountry = (countryCode: string) =>
all().filter(
(country: {
country: string;
alpha2: string;
alpha3: string;
numeric: string;
}) => {
return (
countryCode === country.alpha2
);
}
).length === 1;
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// @ts-ignore
let question: (str: string) => Promise<string> = util.promisify(rl.question).bind(rl);
let split = await question("Encrypt address (a) or message (m)?\n")
if (split === "a") {
let addressLine1 = await question("Address Line 1?\n");
let addressLine2 = await question("Address Line 2?\n");
let city = await question("City?\n");
let countryCode = await question("Country code?\n");
if (!isCountry(countryCode)) {
console.error(`${countryCode} is an invalid country code.`);
exit(-1);
}
let postalOrZip = await question("Postal or zip?\n")
let name = await question("Name?\n")
let address: Address = {
addressLine1,
addressLine2,
city,
countryCode,
postalOrZip,
name
}
let encryptedAddr = encryptAddress(address, pubkey);
console.log("Encrypted address: ", encryptedAddr);
} else if (split === "m") {
let msg: string = await question("Message?\n");
let encrypted = encrypt(msg, pubkey);
console.log("Encrypted: ", encrypted);
} else {
console.error("Invalid.");
exit(-1);
}
}
export { }