-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsign.mjs
More file actions
51 lines (45 loc) · 1.36 KB
/
sign.mjs
File metadata and controls
51 lines (45 loc) · 1.36 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
/* eslint-disable no-console */
import {
PangeaConfig,
VaultService,
PangeaErrors,
Vault,
} from "pangea-node-sdk";
const token = process.env.PANGEA_VAULT_TOKEN;
const config = new PangeaConfig({ domain: process.env.PANGEA_DOMAIN });
const vault = new VaultService(token, config);
(async () => {
try {
console.log("Create...");
// Name should be unique
const name = "Node encrypt example " + Date.now();
const createResponse = await vault.asymmetricGenerate({
algorithm: Vault.AsymmetricAlgorithm.Ed25519,
purpose: Vault.KeyPurpose.SIGNING,
name: name,
});
console.log("Response: %s", createResponse.result);
const keyID = createResponse.result.id;
console.log("Sign...");
const data = Buffer.from("mymessagetosign", "utf8").toString("base64");
const signResponse = await vault.sign(keyID, data);
console.log("Response: %s", signResponse.result);
console.log("Verify...");
const verifyResponse = await vault.verify({
id: keyID,
message: data,
signature: signResponse.result.signature,
});
if (verifyResponse.result.valid_signature) {
console.log("Signature is valid");
} else {
console.log("Signature is invalid");
}
} catch (err) {
if (err instanceof PangeaErrors.APIError) {
console.log(err.toString());
} else {
throw err;
}
}
})();