-
Notifications
You must be signed in to change notification settings - Fork 1
/
plug.js
52 lines (35 loc) · 1.38 KB
/
plug.js
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
import fs from "fs";
import { Secp256k1KeyIdentity } from "@dfinity/identity";
import { Cover, sign } from "@psychedelic/cover";
function getIdentityFromPem(pem) {
const PEM_BEGIN = "-----BEGIN PRIVATE KEY-----";
const PEM_END = "-----END PRIVATE KEY-----";
const PRIV_KEY_INIT =
"308184020100301006072a8648ce3d020106052b8104000a046d306b0201010420";
const KEY_SEPARATOR = "a144034200";
pem = pem.replace(PEM_BEGIN, "");
pem = pem.replace(PEM_END, "");
pem = pem.replace("\n", "");
const pemBuffer = Buffer.from(pem, "base64");
const pemHex = pemBuffer.toString("hex");
const keys = pemHex.replace(PRIV_KEY_INIT, "");
const [privateKey, publicKey] = keys.split(KEY_SEPARATOR);
const identity = Secp256k1KeyIdentity.fromParsedJson([publicKey, privateKey]);
// CONFIRM
console.log("Principal: ", identity.getPrincipal().toText());
const pair = identity.getKeyPair();
console.log("Private key: ", Buffer.from(pair.secretKey).toString("hex"));
console.log(
"Public key: ",
Buffer.from(pair.publicKey.toRaw()).toString("hex")
);
const cover = new Cover(identity);
// sign a signature
const timestamp = new Date().getTime();
console.log("timestamp", timestamp);
sign(identity, timestamp).then((signature) => {
console.log("signature", signature);
});
}
const pem = fs.readFileSync(process.env.PEM_PATH).toString();
getIdentityFromPem(pem);