Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] support xkeys #32

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/deno.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
deno-version: [1.36.4]
deno-version: [1.40.5]

steps:
- name: checkout project
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/node.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
strategy:
matrix:
deno-version: [1.36.4]
node-version: [20.x, 18.x]
node-version: [21.x]

steps:
- name: checkout nkeys
Expand Down
3 changes: 3 additions & 0 deletions modules/cjs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const helper = {
verify: nacl.sign.detached.verify,
fromSeed: nacl.sign.keyPair.fromSeed,
sign: nacl.sign.detached,
scalarBaseMultiply: nacl.scalarMult.base,
seal: nacl.box,
open: nacl.box.open,
};

// This here to support node 10.
Expand Down
3 changes: 3 additions & 0 deletions modules/esm/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ export const denoHelper = {
sign: nacl.sign.detached,
verify: nacl.sign.detached.verify,
randomBytes: nacl.randomBytes,
scalarBaseMultiply: nacl.scalarMult.base,
seal: nacl.box,
open: nacl.box.open,
} as Ed25519Helper;
149 changes: 148 additions & 1 deletion node_test/basics_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018 The NATS Authors
* Copyright 2018-2024 The NATS Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down Expand Up @@ -161,3 +161,150 @@ test("integration - encoded seed returns stable values albertor", (t) => {
t.is(kp.getPublicKey(), data.public_key, "public key");
t.is(td.decode(kp.getPrivateKey()), data.private_key, "private key");
});

test("integration - curve encrypt", (t) => {
// this is actual data generated by the nkeys go library
const expected = [
120,
107,
118,
49,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
103,
188,
195,
73,
0,
3,
119,
91,
181,
167,
162,
167,
105,
51,
102,
84,
94,
152,
189,
229,
149,
222,
47,
17,
149,
188,
170,
];
const secret = "SXAESVBNK3UN2O24Z53ZJLFMX5OYHLAAVULRILWXRO775SDYFKI7FM3J7Y";
const recipient =
"SXAHMGVNIYUF4CA5G475EN64YWNG32HSIF4MZCPZHV7PAFYTWBE5J5USFE";

// parse the imported secret and public
const kp = fromSeed(new TextEncoder().encode(secret));
const kp2 = fromSeed(new TextEncoder().encode(recipient));
const pub2 = kp2.getPublicKey();

// nonce is 24 empty bytes for ease of inspection
const nonce = new Uint8Array(24);
const data = new TextEncoder().encode("hello world");

const enc = kp.seal(data, pub2, nonce);
t.deepEqual(enc, new Uint8Array(expected));
});

test("integration - curve decrypt", (t) => {
// this is actual data generated by the nkeys go library
const message = [
120,
107,
118,
49,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
103,
188,
195,
73,
0,
3,
119,
91,
181,
167,
162,
167,
105,
51,
102,
84,
94,
152,
189,
229,
149,
222,
47,
17,
149,
188,
170,
];
const sender = "SXAESVBNK3UN2O24Z53ZJLFMX5OYHLAAVULRILWXRO775SDYFKI7FM3J7Y";
const secret = "SXAHMGVNIYUF4CA5G475EN64YWNG32HSIF4MZCPZHV7PAFYTWBE5J5USFE";

// parse the imported secret and public
const kp = fromSeed(new TextEncoder().encode(secret));
const kp2 = fromSeed(new TextEncoder().encode(sender));
const senderPK = kp2.getPublicKey();

const enc = kp.open(new Uint8Array(message), senderPK) ?? new Uint8Array();
t.is(new TextDecoder().decode(enc), "hello world");
});
Loading
Loading