Skip to content
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
47 changes: 27 additions & 20 deletions .pnp.cjs

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

This file was deleted.

Git LFS file not shown
3 changes: 0 additions & 3 deletions .yarn/cache/@noble-curves-npm-1.9.2-2584df26a7-21d049ae45.zip

This file was deleted.

3 changes: 3 additions & 0 deletions .yarn/cache/@noble-curves-npm-2.0.1-cd0988b6b8-e0b329eb92.zip
Git LFS file not shown
3 changes: 3 additions & 0 deletions .yarn/cache/@noble-hashes-npm-2.0.1-3deaaa8c92-e81769ce21.zip
Git LFS file not shown
3 changes: 0 additions & 3 deletions .yarn/cache/@scure-bip39-npm-1.6.0-63a27ac0b7-73a54b5566.zip

This file was deleted.

3 changes: 3 additions & 0 deletions .yarn/cache/@scure-bip39-npm-2.0.1-715af0e367-ed8a0788bc.zip
Git LFS file not shown
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to
### Changed

- all: Drop support for Node.js < 22.
- @cosmjs/crypto: Upgrade dependencies @noble/ciphers, @noble/curves,
@noble/hashes and @scure/bip39 to v2. ([#1935])

[#1935]: https://github.com/cosmos/cosmjs/pull/1935

## [0.38.0] - 2025-12-30

Expand Down
8 changes: 4 additions & 4 deletions packages/crypto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
"@cosmjs/encoding": "workspace:^",
"@cosmjs/math": "workspace:^",
"@cosmjs/utils": "workspace:^",
"@noble/ciphers": "^1.3.0",
"@noble/curves": "^1.9.2",
"@noble/hashes": "^1.8.0",
"@scure/bip39": "^1.6.0",
"@noble/ciphers": "^2.1.1",
"@noble/curves": "^2.0.1",
"@noble/hashes": "^2.0.1",
"@scure/bip39": "^2.0.1",
"hash-wasm": "^4.12.0"
},
"devDependencies": {
Expand Down
16 changes: 8 additions & 8 deletions packages/crypto/src/bip39.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@ describe("Bip39", () => {

it("throws for invalid input", () => {
// invalid input length
expect(() => Bip39.encode(fromHex(""))).toThrowError(/got length/);
expect(() => Bip39.encode(fromHex("00"))).toThrowError(/got length/);
expect(() => Bip39.encode(fromHex(""))).toThrowError(/invalid entropy length/);
expect(() => Bip39.encode(fromHex("00"))).toThrowError(/invalid entropy length/);
expect(() => Bip39.encode(fromHex("000000000000000000000000000000"))).toThrowError(
/expected of length .*, got length=15/,
/invalid entropy length/,
);
expect(() => Bip39.encode(fromHex("0000000000000000000000000000000000"))).toThrowError(
/expected of length .*, got length=17/,
/invalid entropy length/,
);
expect(() => Bip39.encode(fromHex("0000000000000000000000000000000000000000000000"))).toThrowError(
/got length/,
/invalid entropy length/,
);
expect(() => Bip39.encode(fromHex("00000000000000000000000000000000000000000000000000"))).toThrowError(
/got length/,
/invalid entropy length/,
);
expect(() =>
Bip39.encode(fromHex("00000000000000000000000000000000000000000000000000000000000000")),
).toThrowError(/got length/);
).toThrowError(/invalid entropy length/);
expect(() =>
Bip39.encode(fromHex("000000000000000000000000000000000000000000000000000000000000000000")),
).toThrowError(/got length/);
).toThrowError(/invalid entropy length/);
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/crypto/src/bip39.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fixUint8Array } from "@cosmjs/encoding";
import { entropyToMnemonic, mnemonicToEntropy, mnemonicToSeed } from "@scure/bip39";
import { wordlist } from "@scure/bip39/wordlists/english";
import { wordlist } from "@scure/bip39/wordlists/english.js";

export class EnglishMnemonic {
public static readonly wordlist: readonly string[] = wordlist;
Expand Down
8 changes: 6 additions & 2 deletions packages/crypto/src/ed25519.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,17 @@ describe("Libsodium", () => {
{
// seed too short
const seed = fromHex("43a9c17ccbb0e767ea29ce1f10813afde5f1e0a7a504e89b4d2cc2b952b8e0");
await expectAsync(Ed25519.makeKeypair(seed)).toBeRejectedWithError(/key of length 32 expected/);
await expectAsync(Ed25519.makeKeypair(seed)).toBeRejectedWithError(
/"secretKey" expected Uint8Array of length 32, got length=31/,
);
}

{
// seed too long
const seed = fromHex("43a9c17ccbb0e767ea29ce1f10813afde5f1e0a7a504e89b4d2cc2b952b8e0b9aa");
await expectAsync(Ed25519.makeKeypair(seed)).toBeRejectedWithError(/key of length 32 expected/);
await expectAsync(Ed25519.makeKeypair(seed)).toBeRejectedWithError(
/"secretKey" expected Uint8Array of length 32, got length=33/,
);
}
});

Expand Down
23 changes: 16 additions & 7 deletions packages/crypto/src/secp256k1.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fixUint8Array, fromHex, toHex } from "@cosmjs/encoding";
import { assert } from "@cosmjs/utils";
import { secp256k1 } from "@noble/curves/secp256k1";
import { secp256k1 } from "@noble/curves/secp256k1.js";

import { ExtendedSecp256k1Signature, Secp256k1Signature } from "./secp256k1signature";

Expand Down Expand Up @@ -41,7 +41,7 @@ export class Secp256k1 {
throw new Error("input data is not a valid secp256k1 private key");
}

if (!secp256k1.utils.isValidPrivateKey(privkey)) {
if (!secp256k1.utils.isValidSecretKey(privkey)) {
// not strictly smaller than N
throw new Error("input data is not a valid secp256k1 private key");
}
Expand Down Expand Up @@ -71,9 +71,15 @@ export class Secp256k1 {
throw new Error("Message hash length must not exceed 32 bytes");
}

const { recovery, r, s } = secp256k1.sign(messageHash, privkey, {
lowS: true,
});
const signature = secp256k1.Signature.fromBytes(
secp256k1.sign(messageHash, privkey, {
prehash: false,
lowS: true,
format: "recovered",
}),
"recovered",
);
const { recovery, r, s } = signature;
if (typeof recovery !== "number") throw new Error("Recovery param missing");
return new ExtendedSecp256k1Signature(unsignedBigIntToBytes(r), unsignedBigIntToBytes(s), recovery);
}
Expand All @@ -90,8 +96,11 @@ export class Secp256k1 {
throw new Error("Message hash length must not exceed 32 bytes");
}

const encodedSig = secp256k1.Signature.fromDER(signature.toDer());
return secp256k1.verify(encodedSig, messageHash, pubkey, { lowS: false });
return secp256k1.verify(signature.toDer(), messageHash, pubkey, {
prehash: false,
lowS: false,
format: "der",
});
}

public static recoverPubkey(
Expand Down
2 changes: 1 addition & 1 deletion packages/crypto/src/slip10.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fixUint8Array, fromHex, toAscii, toHex } from "@cosmjs/encoding";
import { Uint32, Uint53 } from "@cosmjs/math";
import { assert } from "@cosmjs/utils";
import { secp256k1 } from "@noble/curves/secp256k1";
import { secp256k1 } from "@noble/curves/secp256k1.js";

import { Hmac } from "./hmac";
import { Sha512 } from "./sha";
Expand Down
8 changes: 4 additions & 4 deletions packages/crypto/src/xchacha20poly1305.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ describe("Xchacha20poly1305Ietf", () => {
// empty
const key = fromHex("");
await expectAsync(Xchacha20poly1305Ietf.encrypt(message, key, nonce)).toBeRejectedWithError(
/key, got length=0/,
/"arx key" expected Uint8Array of length 32, got length=0/,
);
}
{
// 31 bytes
const key = fromHex("1324cdddc4b94e625bbabcac862c9429ba011e2184a1ccad60e7c3f6ff4916");
await expectAsync(Xchacha20poly1305Ietf.encrypt(message, key, nonce)).toBeRejectedWithError(
/key, got length=31/,
/"arx key" expected Uint8Array of length 32, got length=31/,
);
}
{
// 33 bytes
const key = fromHex("1324cdddc4b94e625bbabcac862c9429ba011e2184a1ccad60e7c3f6ff4916d8aa");
await expectAsync(Xchacha20poly1305Ietf.encrypt(message, key, nonce)).toBeRejectedWithError(
/key, got length=33/,
/"arx key" expected Uint8Array of length 32, got length=33/,
);
}
{
Expand All @@ -49,7 +49,7 @@ describe("Xchacha20poly1305Ietf", () => {
"1324cdddc4b94e625bbabcac862c9429ba011e2184a1ccad60e7c3f6ff4916d81324cdddc4b94e625bbabcac862c9429ba011e2184a1ccad60e7c3f6ff4916d8",
);
await expectAsync(Xchacha20poly1305Ietf.encrypt(message, key, nonce)).toBeRejectedWithError(
/key, got length=64/,
/"arx key" expected Uint8Array of length 32, got length=64/,
);
}
});
Expand Down
Loading
Loading