Skip to content

Commit

Permalink
Merge pull request #1387 from input-output-hk/fix/lw-10987-sdk-can-no…
Browse files Browse the repository at this point in the history
…t-be-used-in-esm-mode-2

fix: esm build now works correctly when imported in modules
  • Loading branch information
AngelCastilloB authored Jul 25, 2024
2 parents 3d5ae37 + ab46f4c commit d03e8c4
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 63 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"@foxglove/crc": "^0.0.3",
"@scure/base": "^1.1.1",
"fraction.js": "4.0.1",
"ip-address": "^8.1.0",
"ip-address": "^9.0.5",
"lodash": "^4.17.21",
"ts-custom-error": "^3.2.0",
"ts-log": "^2.2.4",
Expand Down
25 changes: 10 additions & 15 deletions packages/crypto/src/Bip32/Bip32KeyDerivation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
/* eslint-disable unicorn/number-literal-case */
import { InvalidArgumentError } from '@cardano-sdk/util';
import { add256bits, add28Mul8 } from './arithmetic';
import {
crypto_auth_hmacsha512,
crypto_core_ed25519_add,
crypto_scalarmult_ed25519_base_noclamp
} from 'libsodium-wrappers-sumo';

import sodium from 'libsodium-wrappers-sumo';
/**
* Check if the index is hardened.
*
Expand Down Expand Up @@ -36,9 +31,9 @@ const deriveHardened = (
iv.copy(data, 1 + 32);

data[0] = 0x00;
const zMac = crypto_auth_hmacsha512(data, chainCode);
const zMac = sodium.crypto_auth_hmacsha512(data, chainCode);
data[0] = 0x01;
const ccMac = crypto_auth_hmacsha512(data, chainCode);
const ccMac = sodium.crypto_auth_hmacsha512(data, chainCode);

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

const vk = Buffer.from(crypto_scalarmult_ed25519_base_noclamp(scalar));
const vk = Buffer.from(sodium.crypto_scalarmult_ed25519_base_noclamp(scalar));

vk.copy(data, 1);

data[0] = 0x02;
const zMac = crypto_auth_hmacsha512(data, chainCode);
const zMac = sodium.crypto_auth_hmacsha512(data, chainCode);
data[0] = 0x03;
const ccMac = crypto_auth_hmacsha512(data, chainCode);
const ccMac = sodium.crypto_auth_hmacsha512(data, chainCode);

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

return crypto_scalarmult_ed25519_base_noclamp(scalar);
return sodium.crypto_scalarmult_ed25519_base_noclamp(scalar);
};

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

pk.copy(data, 1);
data[0] = 0x02;
const z = crypto_auth_hmacsha512(data, cc);
const z = sodium.crypto_auth_hmacsha512(data, cc);
data[0] = 0x03;
const c = crypto_auth_hmacsha512(data, cc);
const c = sodium.crypto_auth_hmacsha512(data, cc);

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

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

const p = pointOfTrunc28Mul8(zl);

return Buffer.concat([crypto_core_ed25519_add(p, pk), chainCode]);
return Buffer.concat([sodium.crypto_core_ed25519_add(p, pk), chainCode]);
};
8 changes: 4 additions & 4 deletions packages/crypto/src/Bip32/Bip32PrivateKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { Bip32PrivateKeyHex } from '../hexTypes';
import { Bip32PublicKey } from './Bip32PublicKey';
import { EXTENDED_ED25519_PRIVATE_KEY_LENGTH, Ed25519PrivateKey } from '../Ed25519e';
import { InvalidArgumentError } from '@cardano-sdk/util';
import { crypto_scalarmult_ed25519_base_noclamp, ready } from 'libsodium-wrappers-sumo';
import { pbkdf2Sync } from 'pbkdf2';
import sodium from 'libsodium-wrappers-sumo';

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

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

return Bip32PublicKey.fromBytes(
Buffer.concat([publicKey, this.#key.slice(CHAIN_CODE_INDEX, CHAIN_CODE_INDEX + CHAIN_CODE_SIZE)])
Expand Down
8 changes: 4 additions & 4 deletions packages/crypto/src/Bip32/Bip32PublicKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as Bip32KeyDerivation from './Bip32KeyDerivation';
import { BIP32_PUBLIC_KEY_HASH_LENGTH, Bip32PublicKeyHashHex, Bip32PublicKeyHex } from '../hexTypes';
import { ED25519_PUBLIC_KEY_LENGTH, Ed25519PublicKey } from '../Ed25519e';
import { InvalidArgumentError } from '@cardano-sdk/util';
import { crypto_generichash, ready } from 'libsodium-wrappers-sumo';
import sodium from 'libsodium-wrappers-sumo';

export const BIP32_ED25519_PUBLIC_KEY_LENGTH = 64;

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

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

/** Gets the blake2 hash of the key. */
async hash(): Promise<Bip32PublicKeyHashHex> {
await ready;
const hash = crypto_generichash(BIP32_PUBLIC_KEY_HASH_LENGTH, this.#key);
await sodium.ready;
const hash = sodium.crypto_generichash(BIP32_PUBLIC_KEY_HASH_LENGTH, this.#key);
return Bip32PublicKeyHashHex(Buffer.from(hash).toString('hex'));
}
}
38 changes: 16 additions & 22 deletions packages/crypto/src/Ed25519e/Ed25519PrivateKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,7 @@ import { Ed25519PrivateExtendedKeyHex, Ed25519PrivateNormalKeyHex } from '../hex
import { Ed25519PublicKey } from './Ed25519PublicKey';
import { Ed25519Signature } from './Ed25519Signature';
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util';
import {
crypto_core_ed25519_scalar_add,
crypto_core_ed25519_scalar_mul,
crypto_core_ed25519_scalar_reduce,
crypto_hash_sha512,
crypto_scalarmult_ed25519_base_noclamp,
crypto_sign_detached,
crypto_sign_seed_keypair,
ready
} from 'libsodium-wrappers-sumo';
import sodium from 'libsodium-wrappers-sumo';

const SCALAR_INDEX = 0;
const SCALAR_SIZE = 32;
Expand Down Expand Up @@ -46,17 +37,20 @@ const extendedIv = (extendedKey: Uint8Array) => extendedKey.slice(IV_INDEX, IV_I
*/
const signExtendedDetached = (extendedKey: Uint8Array, message: Uint8Array) => {
const scalar = extendedScalar(extendedKey);
const publicKey = crypto_scalarmult_ed25519_base_noclamp(scalar);
const nonce = crypto_core_ed25519_scalar_reduce(
crypto_hash_sha512(Buffer.concat([extendedIv(extendedKey), message]))
const publicKey = sodium.crypto_scalarmult_ed25519_base_noclamp(scalar);
const nonce = sodium.crypto_core_ed25519_scalar_reduce(
sodium.crypto_hash_sha512(Buffer.concat([extendedIv(extendedKey), message]))
);

const r = crypto_scalarmult_ed25519_base_noclamp(nonce);
const r = sodium.crypto_scalarmult_ed25519_base_noclamp(nonce);

let hram = crypto_hash_sha512(Buffer.concat([r, publicKey, message]));
hram = crypto_core_ed25519_scalar_reduce(hram);
let hram = sodium.crypto_hash_sha512(Buffer.concat([r, publicKey, message]));
hram = sodium.crypto_core_ed25519_scalar_reduce(hram);

return Buffer.concat([r, crypto_core_ed25519_scalar_add(crypto_core_ed25519_scalar_mul(hram, scalar), nonce)]);
return Buffer.concat([
r,
sodium.crypto_core_ed25519_scalar_add(sodium.crypto_core_ed25519_scalar_mul(hram, scalar), nonce)
]);
};

/** Ed25519 private key type. */
Expand Down Expand Up @@ -87,12 +81,12 @@ export class Ed25519PrivateKey {
* @returns the public key.
*/
async toPublic(): Promise<Ed25519PublicKey> {
await ready;
await sodium.ready;

return Ed25519PublicKey.fromBytes(
this.__type === Ed25519PrivateKeyType.Extended
? crypto_scalarmult_ed25519_base_noclamp(extendedScalar(this.#keyMaterial))
: crypto_sign_seed_keypair(this.#keyMaterial).publicKey
? sodium.crypto_scalarmult_ed25519_base_noclamp(extendedScalar(this.#keyMaterial))
: sodium.crypto_sign_seed_keypair(this.#keyMaterial).publicKey
);
}

Expand All @@ -103,11 +97,11 @@ export class Ed25519PrivateKey {
* @returns The Ed25519 digital signature.
*/
async sign(message: HexBlob): Promise<Ed25519Signature> {
await ready;
await sodium.ready;
return Ed25519Signature.fromBytes(
this.__type === Ed25519PrivateKeyType.Extended
? signExtendedDetached(this.#keyMaterial, Buffer.from(message, 'hex'))
: crypto_sign_detached(
: sodium.crypto_sign_detached(
Buffer.from(message, 'hex'),
Buffer.concat([this.#keyMaterial, (await this.toPublic()).bytes()])
)
Expand Down
10 changes: 5 additions & 5 deletions packages/crypto/src/Ed25519e/Ed25519PublicKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ED25519_PUBLIC_KEY_HASH_LENGTH, Ed25519KeyHash } from './Ed25519KeyHash
import { Ed25519PublicKeyHex } from '../hexTypes';
import { Ed25519Signature } from './Ed25519Signature';
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util';
import { crypto_generichash, crypto_sign_verify_detached, ready } from 'libsodium-wrappers-sumo';
import sodium from 'libsodium-wrappers-sumo';

export const ED25519_PUBLIC_KEY_LENGTH = 32;

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

/**
Expand Down Expand Up @@ -60,8 +60,8 @@ export class Ed25519PublicKey {

/** Gets the blake2 hash of the key material. */
async hash(): Promise<Ed25519KeyHash> {
await ready;
const hash = crypto_generichash(ED25519_PUBLIC_KEY_HASH_LENGTH, this.#keyMaterial);
await sodium.ready;
const hash = sodium.crypto_generichash(ED25519_PUBLIC_KEY_HASH_LENGTH, this.#keyMaterial);
return Ed25519KeyHash.fromBytes(hash);
}

Expand Down
4 changes: 2 additions & 2 deletions yarn-project.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1554,7 +1554,7 @@ cacheEntries = {
"internal-slot@npm:1.0.3" = { filename = "internal-slot-npm-1.0.3-9e05eea002-1944f92e98.zip"; sha512 = "1944f92e981e47aebc98a88ff0db579fd90543d937806104d0b96557b10c1f170c51fb777b97740a8b6ddeec585fca8c39ae99fd08a8e058dfc8ab70937238bf"; };
"interpret@npm:2.2.0" = { filename = "interpret-npm-2.2.0-3603a544e1-f51efef7cb.zip"; sha512 = "f51efef7cb8d02da16408ffa3504cd6053014c5aeb7bb8c223727e053e4235bf565e45d67028b0c8740d917c603807aa3c27d7bd2f21bf20b6417e2bb3e5fd6e"; };
"into-stream@npm:6.0.0" = { filename = "into-stream-npm-6.0.0-663ab596b2-8df24c9ead.zip"; sha512 = "8df24c9eadd7cdd1cbc160bc20914b961dfd0ca29767785b69e698f799e85466b6f7c637d237dca1472d09d333399f70cc05a2fb8d08cb449dc9a80d92193980"; };
"ip-address@npm:8.1.0" = { filename = "ip-address-npm-8.1.0-647e78fa39-abea527881.zip"; sha512 = "abea52788176040b45d35548b369157c11b31a331f5e36517b2e8192068cce78fdca567ecdfab0690ee8b4ad9df55cd2940ac3f20871eeb3687e4447208c4803"; };
"ip-address@npm:9.0.5" = { filename = "ip-address-npm-9.0.5-9fa024d42a-aa15f12cfd.zip"; sha512 = "aa15f12cfd0ef5e38349744e3654bae649a34c3b10c77a674a167e99925d1549486c5b14730eebce9fea26f6db9d5e42097b00aa4f9f612e68c79121c71652dc"; };
"ip-regex@npm:4.3.0" = { filename = "ip-regex-npm-4.3.0-4ac12c6be9-7ff904b891.zip"; sha512 = "7ff904b891221b1847f3fdf3dbb3e6a8660dc39bc283f79eb7ed88f5338e1a3d1104b779bc83759159be266249c59c2160e779ee39446d79d4ed0890dfd06f08"; };
"ip@npm:1.1.8" = { filename = "ip-npm-1.1.8-abea558b72-a2ade53eb3.zip"; sha512 = "a2ade53eb339fb0cbe9e69a44caab10d6e3784662285eb5d2677117ee4facc33a64679051c35e0dfdb1a3983a51ce2f5d2cb36446d52e10d01881789b76e28fb"; };
"ip@npm:2.0.0" = { filename = "ip-npm-2.0.0-204facb3cc-cfcfac6b87.zip"; sha512 = "cfcfac6b873b701996d71ec82a7dd27ba92450afdb421e356f44044ed688df04567344c36cbacea7d01b1c39a4c732dc012570ebe9bebfb06f27314bca625349"; };
Expand Down Expand Up @@ -2342,7 +2342,7 @@ cacheEntries = {
"split2@npm:4.1.0" = { filename = "split2-npm-4.1.0-1c1a4bd984-ec581597cb.zip"; sha512 = "ec581597cb74c13cdfb5e2047543dd40cb1e8e9803c7b1e0c29ede05f2b4f049b2d6e7f2788a225d544549375719658b8f38e9366364dec35dc7a12edfda5ee5"; };
"split@npm:1.0.1" = { filename = "split-npm-1.0.1-88871d88a2-12f4554a57.zip"; sha512 = "12f4554a5792c7e98bb3e22b53c63bfa5ef89aa704353e1db608a55b51f5b12afaad6e4a8ecf7843c15f273f43cdadd67b3705cc43d48a75c2cf4641d51f7e7a"; };
"sprintf-js@npm:1.0.3" = { filename = "sprintf-js-npm-1.0.3-73f0a322fa-19d79aec21.zip"; sha512 = "19d79aec211f09b99ec3099b5b2ae2f6e9cdefe50bc91ac4c69144b6d3928a640bb6ae5b3def70c2e85a2c3d9f5ec2719921e3a59d3ca3ef4b2fd1a4656a0df3"; };
"sprintf-js@npm:1.1.2" = { filename = "sprintf-js-npm-1.1.2-ea16269a6d-d4bb464646.zip"; sha512 = "d4bb46464632b335e5faed381bd331157e0af64915a98ede833452663bc672823db49d7531c32d58798e85236581fb7342fd0270531ffc8f914e186187bf1c90"; };
"sprintf-js@npm:1.1.3" = { filename = "sprintf-js-npm-1.1.3-b99efd75b2-a3fdac7b49.zip"; sha512 = "a3fdac7b49643875b70864a9d9b469d87a40dfeaf5d34d9d0c5b1cda5fd7d065531fcb43c76357d62254c57184a7b151954156563a4d6a747015cfb41021cad0"; };
"sqlstring@npm:2.3.3" = { filename = "sqlstring-npm-2.3.3-2db6939570-1e7e2d51c3.zip"; sha512 = "1e7e2d51c38a0cf7372e875408ca100b6e0c9a941ab7773975ea41fb36e5528e404dc787689be855780cf6d0a829ff71027964ae3a05a7446e91dce26672fda7"; };
"sqs-consumer@npm:5.8.0" = { filename = "sqs-consumer-npm-5.8.0-27adc9a7c3-634f289dbe.zip"; sha512 = "634f289dbe1b9901cd4f12508c2d0186d5b4533a8b475bb12bf419f69eb0c92db68a01cebf40b417ccd12823be106034dd7fef1686daff3d386a4069503f9663"; };
"ssh2@npm:1.11.0" = { filename = "ssh2-npm-1.11.0-ba52882820-e40cb9f171.zip"; sha512 = "e40cb9f171741a807c170dc555078aa8c49dc93dd36fc9c8be026fce1cfd31f0d37078d9b60a0f2cfb11d0e007ed5407376b72f8a0ef9f2cb89574632bbfb824"; };
Expand Down
20 changes: 10 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3212,7 +3212,7 @@ __metadata:
delay: ^5.0.0
eslint: ^7.32.0
fraction.js: 4.0.1
ip-address: ^8.1.0
ip-address: ^9.0.5
jest: ^28.1.3
lodash: ^4.17.21
madge: ^5.0.1
Expand Down Expand Up @@ -15875,13 +15875,13 @@ __metadata:
languageName: node
linkType: hard

"ip-address@npm:^8.1.0":
version: 8.1.0
resolution: "ip-address@npm:8.1.0"
"ip-address@npm:^9.0.5":
version: 9.0.5
resolution: "ip-address@npm:9.0.5"
dependencies:
jsbn: 1.1.0
sprintf-js: 1.1.2
checksum: abea52788176040b45d35548b369157c11b31a331f5e36517b2e8192068cce78fdca567ecdfab0690ee8b4ad9df55cd2940ac3f20871eeb3687e4447208c4803
sprintf-js: ^1.1.3
checksum: aa15f12cfd0ef5e38349744e3654bae649a34c3b10c77a674a167e99925d1549486c5b14730eebce9fea26f6db9d5e42097b00aa4f9f612e68c79121c71652dc
languageName: node
linkType: hard

Expand Down Expand Up @@ -23643,10 +23643,10 @@ __metadata:
languageName: node
linkType: hard

"sprintf-js@npm:1.1.2":
version: 1.1.2
resolution: "sprintf-js@npm:1.1.2"
checksum: d4bb46464632b335e5faed381bd331157e0af64915a98ede833452663bc672823db49d7531c32d58798e85236581fb7342fd0270531ffc8f914e186187bf1c90
"sprintf-js@npm:^1.1.3":
version: 1.1.3
resolution: "sprintf-js@npm:1.1.3"
checksum: a3fdac7b49643875b70864a9d9b469d87a40dfeaf5d34d9d0c5b1cda5fd7d065531fcb43c76357d62254c57184a7b151954156563a4d6a747015cfb41021cad0
languageName: node
linkType: hard

Expand Down

0 comments on commit d03e8c4

Please sign in to comment.