-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpublic-key.js
More file actions
93 lines (82 loc) · 2.92 KB
/
public-key.js
File metadata and controls
93 lines (82 loc) · 2.92 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
import { Ed25519Keypair, Secp256k1Keypair } from "@mysten/sui.js";
import { generateEd25519Address, generateSecp256k1Address } from "./index.js";
let privateKey = process.argv[2];
if (!privateKey) {
console.error("Must provide a private key:");
console.error("public-key.js {PRIVATE_KEY}");
process.exit();
}
if (privateKey.startsWith("0x")) {
privateKey = Buffer.from(privateKey.slice(2), "hex");
} else {
privateKey = Buffer.from(privateKey, "base64");
}
// If `privateKey` length is 33 (or 65 for legacy keys) then there is likely a prepended schema byte
if ((privateKey.length == 33) | (privateKey.length == 65)) {
let schema = privateKey[0];
privateKey = privateKey.slice(1, 33);
if (schema === 0) {
console.log("Found prepended schema byte, assuming `ED25519`");
let publicKey = Ed25519Keypair.fromSecretKey(privateKey);
displayAddresses(
publicKey,
generateEd25519Address(publicKey.getPublicKey())
);
} else if (schema === 1) {
console.log("Found prepended schema byte, assuming `Secp256k1`");
let publicKey = Secp256k1Keypair.fromSecretKey(privateKey);
displayAddresses(
publicKey,
generateSecp256k1Address(publicKey.getPublicKey())
);
} else {
console.error("Found prepended schema byte but unknown schema");
}
} else if ((privateKey.length == 32) | (privateKey.length == 64)) {
// Could not identify schema based on prepended byte
let schema = process.argv[3];
if (!schema) {
console.error(
"Must provide a valid encryption schema (`ED25519` or `Secp256k1`):"
);
console.error("public-key.js {PRIVATE_KEY} ED25519");
process.exit();
}
if (schema != "ED25519" && schema != `Secp256k1`) {
console.error(
"Did not provide valid signature schema (`ED25519` or `Secp256k1`)"
);
console.error(`Provided: ${schema}`);
process.exit();
}
privateKey = privateKey.slice(0, 32);
let prependedPrivateKey = Buffer.alloc(33);
privateKey.copy(prependedPrivateKey, 1);
if (schema == "ED25519") {
prependedPrivateKey.writeUint8(0, 0);
let publicKey = Ed25519Keypair.fromSecretKey(privateKey);
displayAddresses(
publicKey,
generateEd25519Address(publicKey.getPublicKey()),
prependedPrivateKey.toString("base64")
);
} else if (schema === "Secp256k1") {
prependedPrivateKey.writeUint8(1, 0);
let publicKey = Secp256k1Keypair.fromSecretKey(privateKey);
displayAddresses(
publicKey,
generateSecp256k1Address(publicKey.getPublicKey()),
prependedPrivateKey.toString("base64")
);
}
} else {
console.error("Private key must be 32 bytes");
}
function displayAddresses(publicKey, addresses, prependedPrivateKey) {
if (prependedPrivateKey) {
console.log(`prependedPrivateKey: ${prependedPrivateKey}`);
}
console.log(`publicKey: ${publicKey.getPublicKey().toString()}`);
console.log(`oldAddress: ${addresses[0]}`);
console.log(`newAddress: ${addresses[1]}`);
}