Skip to content

Commit d03e8c4

Browse files
Merge pull request #1387 from input-output-hk/fix/lw-10987-sdk-can-not-be-used-in-esm-mode-2
fix: esm build now works correctly when imported in modules
2 parents 3d5ae37 + ab46f4c commit d03e8c4

File tree

8 files changed

+52
-63
lines changed

8 files changed

+52
-63
lines changed

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"@foxglove/crc": "^0.0.3",
6464
"@scure/base": "^1.1.1",
6565
"fraction.js": "4.0.1",
66-
"ip-address": "^8.1.0",
66+
"ip-address": "^9.0.5",
6767
"lodash": "^4.17.21",
6868
"ts-custom-error": "^3.2.0",
6969
"ts-log": "^2.2.4",

packages/crypto/src/Bip32/Bip32KeyDerivation.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22
/* eslint-disable unicorn/number-literal-case */
33
import { InvalidArgumentError } from '@cardano-sdk/util';
44
import { add256bits, add28Mul8 } from './arithmetic';
5-
import {
6-
crypto_auth_hmacsha512,
7-
crypto_core_ed25519_add,
8-
crypto_scalarmult_ed25519_base_noclamp
9-
} from 'libsodium-wrappers-sumo';
10-
5+
import sodium from 'libsodium-wrappers-sumo';
116
/**
127
* Check if the index is hardened.
138
*
@@ -36,9 +31,9 @@ const deriveHardened = (
3631
iv.copy(data, 1 + 32);
3732

3833
data[0] = 0x00;
39-
const zMac = crypto_auth_hmacsha512(data, chainCode);
34+
const zMac = sodium.crypto_auth_hmacsha512(data, chainCode);
4035
data[0] = 0x01;
41-
const ccMac = crypto_auth_hmacsha512(data, chainCode);
36+
const ccMac = sodium.crypto_auth_hmacsha512(data, chainCode);
4237

4338
return { ccMac, zMac };
4439
};
@@ -54,14 +49,14 @@ const deriveSoft = (index: number, scalar: Buffer, chainCode: Buffer): { zMac: U
5449
const data = Buffer.allocUnsafe(1 + 32 + 4);
5550
data.writeUInt32LE(index, 1 + 32);
5651

57-
const vk = Buffer.from(crypto_scalarmult_ed25519_base_noclamp(scalar));
52+
const vk = Buffer.from(sodium.crypto_scalarmult_ed25519_base_noclamp(scalar));
5853

5954
vk.copy(data, 1);
6055

6156
data[0] = 0x02;
62-
const zMac = crypto_auth_hmacsha512(data, chainCode);
57+
const zMac = sodium.crypto_auth_hmacsha512(data, chainCode);
6358
data[0] = 0x03;
64-
const ccMac = crypto_auth_hmacsha512(data, chainCode);
59+
const ccMac = sodium.crypto_auth_hmacsha512(data, chainCode);
6560

6661
return { ccMac, zMac };
6762
};
@@ -74,7 +69,7 @@ const deriveSoft = (index: number, scalar: Buffer, chainCode: Buffer): { zMac: U
7469
const pointOfTrunc28Mul8 = (sk: Uint8Array) => {
7570
const scalar = add28Mul8(new Uint8Array(32).fill(0), sk);
7671

77-
return crypto_scalarmult_ed25519_base_noclamp(scalar);
72+
return sodium.crypto_scalarmult_ed25519_base_noclamp(scalar);
7873
};
7974

8075
/**
@@ -139,15 +134,15 @@ export const derivePublic = (key: Buffer, index: number): Buffer => {
139134

140135
pk.copy(data, 1);
141136
data[0] = 0x02;
142-
const z = crypto_auth_hmacsha512(data, cc);
137+
const z = sodium.crypto_auth_hmacsha512(data, cc);
143138
data[0] = 0x03;
144-
const c = crypto_auth_hmacsha512(data, cc);
139+
const c = sodium.crypto_auth_hmacsha512(data, cc);
145140

146141
const chainCode = c.slice(32, 64);
147142

148143
const zl = z.slice(0, 32);
149144

150145
const p = pointOfTrunc28Mul8(zl);
151146

152-
return Buffer.concat([crypto_core_ed25519_add(p, pk), chainCode]);
147+
return Buffer.concat([sodium.crypto_core_ed25519_add(p, pk), chainCode]);
153148
};

packages/crypto/src/Bip32/Bip32PrivateKey.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { Bip32PrivateKeyHex } from '../hexTypes';
44
import { Bip32PublicKey } from './Bip32PublicKey';
55
import { EXTENDED_ED25519_PRIVATE_KEY_LENGTH, Ed25519PrivateKey } from '../Ed25519e';
66
import { InvalidArgumentError } from '@cardano-sdk/util';
7-
import { crypto_scalarmult_ed25519_base_noclamp, ready } from 'libsodium-wrappers-sumo';
87
import { pbkdf2Sync } from 'pbkdf2';
8+
import sodium from 'libsodium-wrappers-sumo';
99

1010
const SCALAR_INDEX = 0;
1111
const SCALAR_SIZE = 32;
@@ -127,7 +127,7 @@ export class Bip32PrivateKey {
127127
* @returns The child BIP-32 key.
128128
*/
129129
async derive(derivationIndices: number[]): Promise<Bip32PrivateKey> {
130-
await ready;
130+
await sodium.ready;
131131
let key = Buffer.from(this.#key);
132132

133133
for (const index of derivationIndices) {
@@ -148,9 +148,9 @@ export class Bip32PrivateKey {
148148
* @returns the public key.
149149
*/
150150
async toPublic(): Promise<Bip32PublicKey> {
151-
await ready;
151+
await sodium.ready;
152152
const scalar = extendedScalar(this.#key.slice(0, EXTENDED_ED25519_PRIVATE_KEY_LENGTH));
153-
const publicKey = crypto_scalarmult_ed25519_base_noclamp(scalar);
153+
const publicKey = sodium.crypto_scalarmult_ed25519_base_noclamp(scalar);
154154

155155
return Bip32PublicKey.fromBytes(
156156
Buffer.concat([publicKey, this.#key.slice(CHAIN_CODE_INDEX, CHAIN_CODE_INDEX + CHAIN_CODE_SIZE)])

packages/crypto/src/Bip32/Bip32PublicKey.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Bip32KeyDerivation from './Bip32KeyDerivation';
22
import { BIP32_PUBLIC_KEY_HASH_LENGTH, Bip32PublicKeyHashHex, Bip32PublicKeyHex } from '../hexTypes';
33
import { ED25519_PUBLIC_KEY_LENGTH, Ed25519PublicKey } from '../Ed25519e';
44
import { InvalidArgumentError } from '@cardano-sdk/util';
5-
import { crypto_generichash, ready } from 'libsodium-wrappers-sumo';
5+
import sodium from 'libsodium-wrappers-sumo';
66

77
export const BIP32_ED25519_PUBLIC_KEY_LENGTH = 64;
88

@@ -57,7 +57,7 @@ export class Bip32PublicKey {
5757
* @returns The child extended private key.
5858
*/
5959
async derive(derivationIndices: number[]): Promise<Bip32PublicKey> {
60-
await ready;
60+
await sodium.ready;
6161
let key = Buffer.from(this.#key);
6262

6363
for (const index of derivationIndices) {
@@ -79,8 +79,8 @@ export class Bip32PublicKey {
7979

8080
/** Gets the blake2 hash of the key. */
8181
async hash(): Promise<Bip32PublicKeyHashHex> {
82-
await ready;
83-
const hash = crypto_generichash(BIP32_PUBLIC_KEY_HASH_LENGTH, this.#key);
82+
await sodium.ready;
83+
const hash = sodium.crypto_generichash(BIP32_PUBLIC_KEY_HASH_LENGTH, this.#key);
8484
return Bip32PublicKeyHashHex(Buffer.from(hash).toString('hex'));
8585
}
8686
}

packages/crypto/src/Ed25519e/Ed25519PrivateKey.ts

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,7 @@ import { Ed25519PrivateExtendedKeyHex, Ed25519PrivateNormalKeyHex } from '../hex
33
import { Ed25519PublicKey } from './Ed25519PublicKey';
44
import { Ed25519Signature } from './Ed25519Signature';
55
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util';
6-
import {
7-
crypto_core_ed25519_scalar_add,
8-
crypto_core_ed25519_scalar_mul,
9-
crypto_core_ed25519_scalar_reduce,
10-
crypto_hash_sha512,
11-
crypto_scalarmult_ed25519_base_noclamp,
12-
crypto_sign_detached,
13-
crypto_sign_seed_keypair,
14-
ready
15-
} from 'libsodium-wrappers-sumo';
6+
import sodium from 'libsodium-wrappers-sumo';
167

178
const SCALAR_INDEX = 0;
189
const SCALAR_SIZE = 32;
@@ -46,17 +37,20 @@ const extendedIv = (extendedKey: Uint8Array) => extendedKey.slice(IV_INDEX, IV_I
4637
*/
4738
const signExtendedDetached = (extendedKey: Uint8Array, message: Uint8Array) => {
4839
const scalar = extendedScalar(extendedKey);
49-
const publicKey = crypto_scalarmult_ed25519_base_noclamp(scalar);
50-
const nonce = crypto_core_ed25519_scalar_reduce(
51-
crypto_hash_sha512(Buffer.concat([extendedIv(extendedKey), message]))
40+
const publicKey = sodium.crypto_scalarmult_ed25519_base_noclamp(scalar);
41+
const nonce = sodium.crypto_core_ed25519_scalar_reduce(
42+
sodium.crypto_hash_sha512(Buffer.concat([extendedIv(extendedKey), message]))
5243
);
5344

54-
const r = crypto_scalarmult_ed25519_base_noclamp(nonce);
45+
const r = sodium.crypto_scalarmult_ed25519_base_noclamp(nonce);
5546

56-
let hram = crypto_hash_sha512(Buffer.concat([r, publicKey, message]));
57-
hram = crypto_core_ed25519_scalar_reduce(hram);
47+
let hram = sodium.crypto_hash_sha512(Buffer.concat([r, publicKey, message]));
48+
hram = sodium.crypto_core_ed25519_scalar_reduce(hram);
5849

59-
return Buffer.concat([r, crypto_core_ed25519_scalar_add(crypto_core_ed25519_scalar_mul(hram, scalar), nonce)]);
50+
return Buffer.concat([
51+
r,
52+
sodium.crypto_core_ed25519_scalar_add(sodium.crypto_core_ed25519_scalar_mul(hram, scalar), nonce)
53+
]);
6054
};
6155

6256
/** Ed25519 private key type. */
@@ -87,12 +81,12 @@ export class Ed25519PrivateKey {
8781
* @returns the public key.
8882
*/
8983
async toPublic(): Promise<Ed25519PublicKey> {
90-
await ready;
84+
await sodium.ready;
9185

9286
return Ed25519PublicKey.fromBytes(
9387
this.__type === Ed25519PrivateKeyType.Extended
94-
? crypto_scalarmult_ed25519_base_noclamp(extendedScalar(this.#keyMaterial))
95-
: crypto_sign_seed_keypair(this.#keyMaterial).publicKey
88+
? sodium.crypto_scalarmult_ed25519_base_noclamp(extendedScalar(this.#keyMaterial))
89+
: sodium.crypto_sign_seed_keypair(this.#keyMaterial).publicKey
9690
);
9791
}
9892

@@ -103,11 +97,11 @@ export class Ed25519PrivateKey {
10397
* @returns The Ed25519 digital signature.
10498
*/
10599
async sign(message: HexBlob): Promise<Ed25519Signature> {
106-
await ready;
100+
await sodium.ready;
107101
return Ed25519Signature.fromBytes(
108102
this.__type === Ed25519PrivateKeyType.Extended
109103
? signExtendedDetached(this.#keyMaterial, Buffer.from(message, 'hex'))
110-
: crypto_sign_detached(
104+
: sodium.crypto_sign_detached(
111105
Buffer.from(message, 'hex'),
112106
Buffer.concat([this.#keyMaterial, (await this.toPublic()).bytes()])
113107
)

packages/crypto/src/Ed25519e/Ed25519PublicKey.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ED25519_PUBLIC_KEY_HASH_LENGTH, Ed25519KeyHash } from './Ed25519KeyHash
22
import { Ed25519PublicKeyHex } from '../hexTypes';
33
import { Ed25519Signature } from './Ed25519Signature';
44
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util';
5-
import { crypto_generichash, crypto_sign_verify_detached, ready } from 'libsodium-wrappers-sumo';
5+
import sodium from 'libsodium-wrappers-sumo';
66

77
export const ED25519_PUBLIC_KEY_LENGTH = 32;
88

@@ -31,8 +31,8 @@ export class Ed25519PublicKey {
3131
* @returns true if the signature is valid; otherwise; false.
3232
*/
3333
async verify(signature: Ed25519Signature, message: HexBlob): Promise<boolean> {
34-
await ready;
35-
return crypto_sign_verify_detached(signature.bytes(), Buffer.from(message, 'hex'), this.#keyMaterial);
34+
await sodium.ready;
35+
return sodium.crypto_sign_verify_detached(signature.bytes(), Buffer.from(message, 'hex'), this.#keyMaterial);
3636
}
3737

3838
/**
@@ -60,8 +60,8 @@ export class Ed25519PublicKey {
6060

6161
/** Gets the blake2 hash of the key material. */
6262
async hash(): Promise<Ed25519KeyHash> {
63-
await ready;
64-
const hash = crypto_generichash(ED25519_PUBLIC_KEY_HASH_LENGTH, this.#keyMaterial);
63+
await sodium.ready;
64+
const hash = sodium.crypto_generichash(ED25519_PUBLIC_KEY_HASH_LENGTH, this.#keyMaterial);
6565
return Ed25519KeyHash.fromBytes(hash);
6666
}
6767

yarn-project.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,7 @@ cacheEntries = {
15541554
"internal-slot@npm:1.0.3" = { filename = "internal-slot-npm-1.0.3-9e05eea002-1944f92e98.zip"; sha512 = "1944f92e981e47aebc98a88ff0db579fd90543d937806104d0b96557b10c1f170c51fb777b97740a8b6ddeec585fca8c39ae99fd08a8e058dfc8ab70937238bf"; };
15551555
"interpret@npm:2.2.0" = { filename = "interpret-npm-2.2.0-3603a544e1-f51efef7cb.zip"; sha512 = "f51efef7cb8d02da16408ffa3504cd6053014c5aeb7bb8c223727e053e4235bf565e45d67028b0c8740d917c603807aa3c27d7bd2f21bf20b6417e2bb3e5fd6e"; };
15561556
"into-stream@npm:6.0.0" = { filename = "into-stream-npm-6.0.0-663ab596b2-8df24c9ead.zip"; sha512 = "8df24c9eadd7cdd1cbc160bc20914b961dfd0ca29767785b69e698f799e85466b6f7c637d237dca1472d09d333399f70cc05a2fb8d08cb449dc9a80d92193980"; };
1557-
"ip-address@npm:8.1.0" = { filename = "ip-address-npm-8.1.0-647e78fa39-abea527881.zip"; sha512 = "abea52788176040b45d35548b369157c11b31a331f5e36517b2e8192068cce78fdca567ecdfab0690ee8b4ad9df55cd2940ac3f20871eeb3687e4447208c4803"; };
1557+
"ip-address@npm:9.0.5" = { filename = "ip-address-npm-9.0.5-9fa024d42a-aa15f12cfd.zip"; sha512 = "aa15f12cfd0ef5e38349744e3654bae649a34c3b10c77a674a167e99925d1549486c5b14730eebce9fea26f6db9d5e42097b00aa4f9f612e68c79121c71652dc"; };
15581558
"ip-regex@npm:4.3.0" = { filename = "ip-regex-npm-4.3.0-4ac12c6be9-7ff904b891.zip"; sha512 = "7ff904b891221b1847f3fdf3dbb3e6a8660dc39bc283f79eb7ed88f5338e1a3d1104b779bc83759159be266249c59c2160e779ee39446d79d4ed0890dfd06f08"; };
15591559
"ip@npm:1.1.8" = { filename = "ip-npm-1.1.8-abea558b72-a2ade53eb3.zip"; sha512 = "a2ade53eb339fb0cbe9e69a44caab10d6e3784662285eb5d2677117ee4facc33a64679051c35e0dfdb1a3983a51ce2f5d2cb36446d52e10d01881789b76e28fb"; };
15601560
"ip@npm:2.0.0" = { filename = "ip-npm-2.0.0-204facb3cc-cfcfac6b87.zip"; sha512 = "cfcfac6b873b701996d71ec82a7dd27ba92450afdb421e356f44044ed688df04567344c36cbacea7d01b1c39a4c732dc012570ebe9bebfb06f27314bca625349"; };
@@ -2342,7 +2342,7 @@ cacheEntries = {
23422342
"split2@npm:4.1.0" = { filename = "split2-npm-4.1.0-1c1a4bd984-ec581597cb.zip"; sha512 = "ec581597cb74c13cdfb5e2047543dd40cb1e8e9803c7b1e0c29ede05f2b4f049b2d6e7f2788a225d544549375719658b8f38e9366364dec35dc7a12edfda5ee5"; };
23432343
"split@npm:1.0.1" = { filename = "split-npm-1.0.1-88871d88a2-12f4554a57.zip"; sha512 = "12f4554a5792c7e98bb3e22b53c63bfa5ef89aa704353e1db608a55b51f5b12afaad6e4a8ecf7843c15f273f43cdadd67b3705cc43d48a75c2cf4641d51f7e7a"; };
23442344
"sprintf-js@npm:1.0.3" = { filename = "sprintf-js-npm-1.0.3-73f0a322fa-19d79aec21.zip"; sha512 = "19d79aec211f09b99ec3099b5b2ae2f6e9cdefe50bc91ac4c69144b6d3928a640bb6ae5b3def70c2e85a2c3d9f5ec2719921e3a59d3ca3ef4b2fd1a4656a0df3"; };
2345-
"sprintf-js@npm:1.1.2" = { filename = "sprintf-js-npm-1.1.2-ea16269a6d-d4bb464646.zip"; sha512 = "d4bb46464632b335e5faed381bd331157e0af64915a98ede833452663bc672823db49d7531c32d58798e85236581fb7342fd0270531ffc8f914e186187bf1c90"; };
2345+
"sprintf-js@npm:1.1.3" = { filename = "sprintf-js-npm-1.1.3-b99efd75b2-a3fdac7b49.zip"; sha512 = "a3fdac7b49643875b70864a9d9b469d87a40dfeaf5d34d9d0c5b1cda5fd7d065531fcb43c76357d62254c57184a7b151954156563a4d6a747015cfb41021cad0"; };
23462346
"sqlstring@npm:2.3.3" = { filename = "sqlstring-npm-2.3.3-2db6939570-1e7e2d51c3.zip"; sha512 = "1e7e2d51c38a0cf7372e875408ca100b6e0c9a941ab7773975ea41fb36e5528e404dc787689be855780cf6d0a829ff71027964ae3a05a7446e91dce26672fda7"; };
23472347
"sqs-consumer@npm:5.8.0" = { filename = "sqs-consumer-npm-5.8.0-27adc9a7c3-634f289dbe.zip"; sha512 = "634f289dbe1b9901cd4f12508c2d0186d5b4533a8b475bb12bf419f69eb0c92db68a01cebf40b417ccd12823be106034dd7fef1686daff3d386a4069503f9663"; };
23482348
"ssh2@npm:1.11.0" = { filename = "ssh2-npm-1.11.0-ba52882820-e40cb9f171.zip"; sha512 = "e40cb9f171741a807c170dc555078aa8c49dc93dd36fc9c8be026fce1cfd31f0d37078d9b60a0f2cfb11d0e007ed5407376b72f8a0ef9f2cb89574632bbfb824"; };

yarn.lock

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3212,7 +3212,7 @@ __metadata:
32123212
delay: ^5.0.0
32133213
eslint: ^7.32.0
32143214
fraction.js: 4.0.1
3215-
ip-address: ^8.1.0
3215+
ip-address: ^9.0.5
32163216
jest: ^28.1.3
32173217
lodash: ^4.17.21
32183218
madge: ^5.0.1
@@ -15875,13 +15875,13 @@ __metadata:
1587515875
languageName: node
1587615876
linkType: hard
1587715877

15878-
"ip-address@npm:^8.1.0":
15879-
version: 8.1.0
15880-
resolution: "ip-address@npm:8.1.0"
15878+
"ip-address@npm:^9.0.5":
15879+
version: 9.0.5
15880+
resolution: "ip-address@npm:9.0.5"
1588115881
dependencies:
1588215882
jsbn: 1.1.0
15883-
sprintf-js: 1.1.2
15884-
checksum: abea52788176040b45d35548b369157c11b31a331f5e36517b2e8192068cce78fdca567ecdfab0690ee8b4ad9df55cd2940ac3f20871eeb3687e4447208c4803
15883+
sprintf-js: ^1.1.3
15884+
checksum: aa15f12cfd0ef5e38349744e3654bae649a34c3b10c77a674a167e99925d1549486c5b14730eebce9fea26f6db9d5e42097b00aa4f9f612e68c79121c71652dc
1588515885
languageName: node
1588615886
linkType: hard
1588715887

@@ -23643,10 +23643,10 @@ __metadata:
2364323643
languageName: node
2364423644
linkType: hard
2364523645

23646-
"sprintf-js@npm:1.1.2":
23647-
version: 1.1.2
23648-
resolution: "sprintf-js@npm:1.1.2"
23649-
checksum: d4bb46464632b335e5faed381bd331157e0af64915a98ede833452663bc672823db49d7531c32d58798e85236581fb7342fd0270531ffc8f914e186187bf1c90
23646+
"sprintf-js@npm:^1.1.3":
23647+
version: 1.1.3
23648+
resolution: "sprintf-js@npm:1.1.3"
23649+
checksum: a3fdac7b49643875b70864a9d9b469d87a40dfeaf5d34d9d0c5b1cda5fd7d065531fcb43c76357d62254c57184a7b151954156563a4d6a747015cfb41021cad0
2365023650
languageName: node
2365123651
linkType: hard
2365223652

0 commit comments

Comments
 (0)