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: replaces secp256k1 npm package with @noble/curves #814

Merged
merged 6 commits into from
Jan 2, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/generated/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ <h1>Agent-JS Changelog</h1>
<section>
<h2>Version x.x.x</h2>
<ul>
<li>feat: replaces `secp256k1` npm package with `@noble/curves`</li>
<li>feat: enhances `.from` methods on public key classes to support unknown types, including PublicKey instances, ArrayBuffer-like objects, DER encoded public keys, and hex strings. Also introduces a new `bufFromBufLike` util</li>
<li>feat: introduces partial identities from public keys for authentication flows</li>
<li>fix: honor disableIdle flag</li>
</ul>
Expand Down
48 changes: 10 additions & 38 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
{
"name": "@dfinity/identity-secp256k1",
"path": "./packages/identity-secp256k1/dist/index.js",
"limit": "265 kB"
"limit": "269 kB"
}
],
"release-it": {
Expand Down
3 changes: 2 additions & 1 deletion packages/agent/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export type Signature = ArrayBuffer & { __signature__: void };
*/
export interface PublicKey {
toDer(): DerEncodedPublicKey;
// rawKey and derKey are optional for backwards compatibility.
// rawKey, toRaw, and derKey are optional for backwards compatibility.
toRaw?(): ArrayBuffer;
rawKey?: ArrayBuffer;
derKey?: DerEncodedPublicKey;
}
Expand Down
22 changes: 21 additions & 1 deletion packages/agent/src/utils/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function toHex(buffer: ArrayBuffer): string {
return [...new Uint8Array(buffer)].map(x => x.toString(16).padStart(2, '0')).join('');
}

const hexRe = new RegExp(/^([0-9A-F]{2})*$/i);
const hexRe = new RegExp(/^[0-9a-fA-F]+$/);

/**
* Transforms a hexadecimal string into an array buffer.
Expand Down Expand Up @@ -79,3 +79,23 @@ export function bufEquals(b1: ArrayBuffer, b2: ArrayBuffer): boolean {
export function uint8ToBuf(arr: Uint8Array): ArrayBuffer {
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength).buffer;
}

/**
* Returns a true ArrayBuffer from an ArrayBufferLike object.
* @param bufLike a buffer-like object
* @returns ArrayBuffer
*/
export function bufFromBufLike(
bufLike: ArrayBuffer | Uint8Array | DataView | ArrayBufferView | ArrayBufferLike,
): ArrayBuffer {
if (bufLike instanceof Uint8Array) {
return uint8ToBuf(bufLike);
}
if (bufLike instanceof ArrayBuffer) {
return bufLike;
}
if ('buffer' in bufLike) {
return bufLike.buffer;
}
return new Uint8Array(bufLike);
}
7 changes: 3 additions & 4 deletions packages/identity-secp256k1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
},
"dependencies": {
"@dfinity/agent": "^0.20.2",
"@noble/curves": "^1.3.0",
"@noble/hashes": "^1.3.1",
"bip39": "^3.1.0",
"bs58check": "^3.0.1",
"hdkey": "^2.1.0",
"secp256k1": "^5.0.0"
"hdkey": "^2.1.0"
},
"devDependencies": {
"@types/bs58check": "^2.1.0",
"@types/hdkey": "^2.0.3",
"@types/secp256k1": "^4.0.6"
"@types/hdkey": "^2.0.3"
}
}
219 changes: 3 additions & 216 deletions packages/identity-secp256k1/src/__snapshots__/secp256k1.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,220 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Secp256k1PublicKey Tests create from an existing public key 1`] = `
Secp256k1PublicKey {
"derKey": ArrayBuffer [],
"rawKey": Uint8Array [
4,
1,
236,
3,
10,
205,
125,
17,
153,
247,
58,
227,
70,
147,
41,
193,
20,
148,
78,
6,
147,
200,
149,
2,
248,
80,
188,
198,
186,
211,
151,
165,
149,
103,
103,
199,
155,
65,
12,
41,
172,
111,
88,
126,
236,
132,
135,
128,
32,
253,
181,
75,
160,
2,
167,
155,
2,
170,
21,
63,
228,
123,
111,
253,
51,
],
}
`;
exports[`Secp256k1PublicKey Tests create from an existing public key 1`] = `Secp256k1PublicKey {}`;

exports[`Secp256k1PublicKey Tests create from an existing public key 2`] = `
Secp256k1PublicKey {
"derKey": ArrayBuffer [],
"rawKey": Uint8Array [
4,
235,
60,
188,
129,
189,
250,
29,
19,
196,
16,
21,
19,
8,
180,
172,
101,
204,
50,
134,
32,
227,
104,
254,
83,
2,
226,
207,
44,
183,
23,
81,
50,
153,
157,
194,
175,
255,
135,
26,
104,
175,
214,
109,
118,
221,
156,
124,
57,
17,
116,
142,
142,
84,
238,
47,
183,
29,
241,
169,
123,
180,
129,
28,
140,
],
}
`;
exports[`Secp256k1PublicKey Tests create from an existing public key 2`] = `Secp256k1PublicKey {}`;

exports[`Secp256k1PublicKey Tests create from an existing public key 3`] = `
Secp256k1PublicKey {
"derKey": ArrayBuffer [],
"rawKey": Uint8Array [
4,
253,
172,
9,
234,
147,
161,
185,
183,
68,
181,
241,
159,
9,
26,
218,
121,
120,
206,
178,
240,
69,
135,
91,
202,
142,
249,
183,
95,
168,
6,
23,
4,
231,
109,
224,
35,
198,
162,
61,
119,
161,
24,
197,
200,
208,
245,
239,
175,
13,
187,
252,
195,
112,
45,
85,
144,
96,
71,
23,
246,
57,
246,
240,
13,
],
}
`;
exports[`Secp256k1PublicKey Tests create from an existing public key 3`] = `Secp256k1PublicKey {}`;