Skip to content

Commit a124abc

Browse files
committed
Update jsbt. Use isolatedDeclarations in typescript code
1 parent e91f665 commit a124abc

File tree

5 files changed

+70
-86
lines changed

5 files changed

+70
-86
lines changed

.github/workflows/release.yml

+7-49
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,14 @@ name: Publish release
22
on:
33
release:
44
types: [created]
5-
workflow_dispatch:
65
jobs:
7-
publish-jsr:
8-
name: Publish to JSR.io
9-
runs-on: ubuntu-latest
10-
permissions:
11-
contents: read
12-
id-token: write
13-
steps:
14-
- uses: actions/checkout@1e31de5234b9f8995739874a8ce0492dc87873e2 # v4
15-
- run: npm install -g jsr
16-
- run: jsr publish
17-
publish-npm:
18-
name: Publish to NPM
19-
runs-on: ubuntu-latest
20-
permissions:
21-
contents: read
22-
id-token: write
23-
steps:
24-
- uses: actions/checkout@1e31de5234b9f8995739874a8ce0492dc87873e2 # v4
25-
- uses: actions/setup-node@8f152de45cc393bb48ce5d89d36b731f54556e65 # v4
26-
with:
27-
node-version: 22
28-
registry-url: 'https://registry.npmjs.org'
29-
cache: npm
30-
- run: npm ci
31-
- run: npm run build
32-
- run: npm publish --provenance --access public
33-
env:
34-
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
35-
standalone:
36-
name: Upload files to GitHub Releases
37-
runs-on: ubuntu-latest
6+
release-js:
7+
name: "jsbt v0.3.0" # Should match commit below
8+
uses: paulmillr/jsbt/.github/workflows/release.yml@973650a225c0344aa5f993a6cd63835a262077e9
9+
with:
10+
build-path: test/build
11+
secrets:
12+
NPM_PUBLISH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
3813
permissions:
3914
contents: write
4015
id-token: write
41-
steps:
42-
- uses: actions/checkout@1e31de5234b9f8995739874a8ce0492dc87873e2 # v4
43-
- uses: actions/setup-node@8f152de45cc393bb48ce5d89d36b731f54556e65 # v4
44-
with:
45-
node-version: 22
46-
registry-url: 'https://registry.npmjs.org'
47-
cache: npm
48-
- run: npm ci
49-
- run: npm run build
50-
- run: |
51-
cd build
52-
npm ci
53-
npm run build:release
54-
cd ..
55-
- run: gh release upload ${{ github.event.release.tag_name }} build/`npx jsbt outfile`
56-
env:
57-
GH_TOKEN: ${{ github.token }}

.github/workflows/test-js.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ on:
44
- pull_request
55
jobs:
66
test-js:
7-
name: 'JS v0.2.2' # v0.2.2 == commit below
8-
uses: paulmillr/jsbt/.github/workflows/test-js.yml@986dbfea9667eeb0e81bf606cbe9ed169fea89b2
7+
name: "jsbt v0.3.0" # Should match commit below
8+
uses: paulmillr/jsbt/.github/workflows/test-js.yml@973650a225c0344aa5f993a6cd63835a262077e9

index.ts

+51-25
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ const modN = (n: bigint) => mod(n, CURVE_ORDER);
5454
// - https://strobe.sourceforge.io/specs/
5555
// We can implement full version, but seems nobody uses this much.
5656
const STROBE_R: number = 166;
57+
// const Flags2 = {
58+
// I: 1,
59+
// A: 1 << 1,
60+
// C: 1 << 2,
61+
// T: 1 << 3,
62+
// M: 1 << 4,
63+
// K: 1 << 5,
64+
// } as const;
5765
const enum Flags {
5866
I = 1,
5967
A = 1 << 1,
@@ -65,7 +73,7 @@ const enum Flags {
6573
// TODO: this is very close to KeccakPRG, try to merge?
6674
// Differences: suffix, additional methods/flags
6775
export class Strobe128 {
68-
state = new Uint8Array(200);
76+
state: Uint8Array = new Uint8Array(200);
6977
state32: Uint32Array;
7078
pos: number = 0;
7179
posBegin: number = 0;
@@ -148,7 +156,7 @@ export class Strobe128 {
148156
this.overwrite(toData(data));
149157
}
150158
// Utils
151-
clone() {
159+
clone(): Strobe128 {
152160
const n = new Strobe128('0'); // tmp
153161
n.pos = this.pos;
154162
n.posBegin = this.posBegin;
@@ -167,15 +175,15 @@ export class Merlin {
167175
this.strobe = new Strobe128('Merlin v1.0');
168176
this.appendMessage('dom-sep', label);
169177
}
170-
appendMessage(label: Data, message: Data) {
178+
appendMessage(label: Data, message: Data): void {
171179
this.strobe.metaAD(label, false);
172180
this.strobe.metaAD(numberToBytesLE(message.length, 4), true);
173181
this.strobe.AD(message, false);
174182
}
175-
appendU64(label: Data, n: number | bigint) {
183+
appendU64(label: Data, n: number | bigint): void {
176184
this.appendMessage(label, numberToBytesLE(n, 8));
177185
}
178-
challengeBytes(label: Data, len: number) {
186+
challengeBytes(label: Data, len: number): Uint8Array {
179187
this.strobe.metaAD(label, false);
180188
this.strobe.metaAD(numberToBytesLE(len, 4), true);
181189
return this.strobe.PRF(len, false);
@@ -190,26 +198,26 @@ export class SigningContext extends Merlin {
190198
) {
191199
super(name);
192200
}
193-
label(label: Data) {
201+
label(label: Data): void {
194202
this.appendMessage('', label);
195203
}
196-
bytes(bytes: Uint8Array) {
204+
bytes(bytes: Uint8Array): this {
197205
this.appendMessage('sign-bytes', bytes);
198206
return this;
199207
}
200-
protoName(label: Data) {
208+
protoName(label: Data): void {
201209
this.appendMessage('proto-name', label);
202210
}
203-
commitPoint(label: Data, point: Point) {
211+
commitPoint(label: Data, point: Point): void {
204212
this.appendMessage(label, point.toRawBytes());
205213
}
206214
challengeScalar(label: Data): bigint {
207215
return modN(bytesToNumberLE(this.challengeBytes(label, 64)));
208216
}
209-
witnessScalar(label: Data, nonceSeeds: Uint8Array[] = []) {
217+
witnessScalar(label: Data, nonceSeeds: Uint8Array[] = []): bigint {
210218
return modN(bytesToNumberLE(this.witnessBytes(label, 64, nonceSeeds)));
211219
}
212-
witnessBytes(label: Data, len: number, nonceSeeds: Uint8Array[] = []) {
220+
witnessBytes(label: Data, len: number, nonceSeeds: Uint8Array[] = []): Uint8Array {
213221
const strobeRng = this.strobe.clone();
214222
for (const ns of nonceSeeds) {
215223
strobeRng.metaAD(label, false);
@@ -232,7 +240,7 @@ const encodeScalar = (n: bigint) => numberToBytesLE((n << _3n) & MASK, 32);
232240
const decodeScalar = (n: Uint8Array) => bytesToNumberLE(n) >> _3n;
233241

234242
// NOTE: secretKey is 64 bytes (key + nonce). This required for HDKD, since key can be derived not only from seed, but from other keys.
235-
export function getPublicKey(secretKey: Uint8Array) {
243+
export function getPublicKey(secretKey: Uint8Array): Uint8Array {
236244
abytes('secretKey', secretKey, 64);
237245
const scalar = decodeScalar(secretKey.subarray(0, 32));
238246
return RistrettoPoint.BASE.multiply(scalar).toRawBytes();
@@ -251,7 +259,7 @@ export function secretFromSeed(seed: Uint8Array): Uint8Array {
251259
}
252260
// Seems like ed25519 keypair? Generates keypair from other keypair in ed25519 format
253261
// NOTE: not exported from wasm. Do we need this at all?
254-
export function fromKeypair(pair: Uint8Array) {
262+
export function fromKeypair(pair: Uint8Array): Uint8Array {
255263
abytes('keypair', pair, 96);
256264
const sk = pair.slice(0, 32);
257265
const nonce = pair.slice(32, 64);
@@ -264,7 +272,11 @@ export function fromKeypair(pair: Uint8Array) {
264272

265273
// Basic sign. NOTE: context is currently constant. Please open issue if you need different one.
266274
const SUBSTRATE_CONTEXT = utf8ToBytes('substrate');
267-
export function sign(secretKey: Uint8Array, message: Uint8Array, rng = randomBytes) {
275+
export function sign(
276+
secretKey: Uint8Array,
277+
message: Uint8Array,
278+
rng: typeof randomBytes = randomBytes
279+
): Uint8Array {
268280
abytes('message', message);
269281
abytes('secretKey', secretKey, 64);
270282
const t = new SigningContext('SigningContext', rng);
@@ -284,7 +296,7 @@ export function sign(secretKey: Uint8Array, message: Uint8Array, rng = randomByt
284296
res[63] |= 128; // add Schnorrkel marker
285297
return res;
286298
}
287-
export function verify(message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array) {
299+
export function verify(message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array): boolean {
288300
abytes('message', message);
289301
abytes('signature', signature, 64);
290302
abytes('publicKey', publicKey, 32);
@@ -306,7 +318,7 @@ export function verify(message: Uint8Array, signature: Uint8Array, publicKey: Ui
306318
const RR = pubPoint.negate().multiply(k).add(sP);
307319
return RR.equals(R);
308320
}
309-
export function getSharedSecret(secretKey: Uint8Array, publicKey: Uint8Array) {
321+
export function getSharedSecret(secretKey: Uint8Array, publicKey: Uint8Array): Uint8Array {
310322
abytes('secretKey', secretKey, 64);
311323
abytes('publicKey', publicKey, 32);
312324
const keyScalar = decodeScalar(secretKey.subarray(0, 32));
@@ -316,7 +328,11 @@ export function getSharedSecret(secretKey: Uint8Array, publicKey: Uint8Array) {
316328

317329
// Derive
318330
export const HDKD = {
319-
secretSoft(secretKey: Uint8Array, chainCode: Uint8Array, rng = randomBytes) {
331+
secretSoft(
332+
secretKey: Uint8Array,
333+
chainCode: Uint8Array,
334+
rng: typeof randomBytes = randomBytes
335+
): Uint8Array {
320336
abytes('secretKey', secretKey, 64);
321337
abytes('chainCode', chainCode, 32);
322338
const masterScalar = decodeScalar(secretKey.subarray(0, 32));
@@ -335,7 +351,7 @@ export const HDKD = {
335351
const key = encodeScalar(modN(masterScalar + scalar));
336352
return concatBytes(key, nonce);
337353
},
338-
publicSoft(publicKey: Uint8Array, chainCode: Uint8Array) {
354+
publicSoft(publicKey: Uint8Array, chainCode: Uint8Array): Uint8Array {
339355
abytes('publicKey', publicKey, 32);
340356
abytes('chainCode', chainCode, 32);
341357
const pubPoint = RistrettoPoint.fromHex(publicKey);
@@ -347,7 +363,7 @@ export const HDKD = {
347363
t.challengeBytes('HDKD-chaincode', 32);
348364
return pubPoint.add(RistrettoPoint.BASE.multiply(scalar)).toRawBytes();
349365
},
350-
secretHard(secretKey: Uint8Array, chainCode: Uint8Array) {
366+
secretHard(secretKey: Uint8Array, chainCode: Uint8Array): Uint8Array {
351367
abytes('secretKey', secretKey, 64);
352368
abytes('chainCode', chainCode, 32);
353369
const key = numberToBytesLE(decodeScalar(secretKey.subarray(0, 32)), 32);
@@ -416,7 +432,13 @@ function initVRF(
416432
return { input, t: transcript };
417433
}
418434
export const vrf = {
419-
sign(msg: Uint8Array, secretKey: Uint8Array, ctx = EMPTY, extra = EMPTY, rng = randomBytes) {
435+
sign(
436+
msg: Uint8Array,
437+
secretKey: Uint8Array,
438+
ctx: Uint8Array = EMPTY,
439+
extra: Uint8Array = EMPTY,
440+
rng: typeof randomBytes = randomBytes
441+
): Uint8Array {
420442
abytes('msg', msg);
421443
abytes('secretKey', secretKey, 64);
422444
abytes('ctx', ctx);
@@ -438,10 +460,10 @@ export const vrf = {
438460
msg: Uint8Array,
439461
signature: Uint8Array,
440462
publicKey: Uint8Array,
441-
ctx = EMPTY,
442-
extra = EMPTY,
443-
rng = randomBytes
444-
) {
463+
ctx: Uint8Array = EMPTY,
464+
extra: Uint8Array = EMPTY,
465+
rng: typeof randomBytes = randomBytes
466+
): boolean {
445467
abytes('msg', msg);
446468
abytes('signature', signature, 96); // O(point) || c(scalar) || s(scalar)
447469
abytes('pubkey', publicKey, 32);
@@ -462,7 +484,11 @@ export const vrf = {
462484
};
463485

464486
// NOTE: for tests only, don't use
465-
export const __tests = {
487+
export const __tests: {
488+
Strobe128: typeof Strobe128;
489+
Merlin: typeof Merlin;
490+
SigningContext: typeof SigningContext;
491+
} = {
466492
Strobe128,
467493
Merlin,
468494
SigningContext,

package-lock.json

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
"@noble/hashes": "~1.6.0"
2121
},
2222
"devDependencies": {
23-
"@paulmillr/jsbt": "0.2.1",
23+
"@paulmillr/jsbt": "0.3.0",
2424
"micro-bmark": "0.3.1",
25-
"micro-should": "0.5.0",
25+
"micro-should": "0.5.1",
2626
"prettier": "3.3.2",
2727
"typescript": "5.5.2"
2828
},

0 commit comments

Comments
 (0)