diff --git a/.changeset/swift-pillows-obey.md b/.changeset/swift-pillows-obey.md new file mode 100644 index 00000000..9080039d --- /dev/null +++ b/.changeset/swift-pillows-obey.md @@ -0,0 +1,5 @@ +--- +'@metaplex-foundation/umi-eddsa-web3js': patch +--- + +Use @noble/curves instead of @noble/ed25519 diff --git a/packages/umi-eddsa-web3js/package.json b/packages/umi-eddsa-web3js/package.json index 3cccf2bb..e3b45aeb 100644 --- a/packages/umi-eddsa-web3js/package.json +++ b/packages/umi-eddsa-web3js/package.json @@ -28,7 +28,7 @@ }, "dependencies": { "@metaplex-foundation/umi-web3js-adapters": "workspace:^", - "@noble/ed25519": "^1.7.1" + "@noble/curves": "^1.0.0" }, "peerDependencies": { "@metaplex-foundation/umi": "workspace:^", diff --git a/packages/umi-eddsa-web3js/rollup.config.js b/packages/umi-eddsa-web3js/rollup.config.js index ba38fd10..e37ad289 100644 --- a/packages/umi-eddsa-web3js/rollup.config.js +++ b/packages/umi-eddsa-web3js/rollup.config.js @@ -3,6 +3,7 @@ import pkg from './package.json'; export default createConfigs({ pkg, + additionalExternals: ['@noble/curves/ed25519'], builds: [ { dir: 'dist/esm', diff --git a/packages/umi-eddsa-web3js/src/createWeb3JsEddsa.ts b/packages/umi-eddsa-web3js/src/createWeb3JsEddsa.ts index 4b21f161..1536567b 100644 --- a/packages/umi-eddsa-web3js/src/createWeb3JsEddsa.ts +++ b/packages/umi-eddsa-web3js/src/createWeb3JsEddsa.ts @@ -11,7 +11,7 @@ import { fromWeb3JsPublicKey, toWeb3JsPublicKey, } from '@metaplex-foundation/umi-web3js-adapters'; -import * as ed25519 from '@noble/ed25519'; +import { ed25519 } from '@noble/curves/ed25519'; import { Keypair as Web3JsKeypair, PublicKey as Web3JsPublicKey, @@ -39,13 +39,13 @@ export function createWeb3JsEddsa(): EddsaInterface { }; const sign = (message: Uint8Array, keypair: Keypair): Uint8Array => - ed25519.sync.sign(message, keypair.secretKey.slice(0, 32)); + ed25519.sign(message, keypair.secretKey.slice(0, 32)); const verify = ( message: Uint8Array, signature: Uint8Array, publicKey: PublicKey - ): boolean => ed25519.sync.verify(signature, message, publicKey.bytes); + ): boolean => ed25519.verify(signature, message, publicKey.bytes); return { generateKeypair, diff --git a/packages/umi-eddsa-web3js/test/Web3JsEddsa.test.ts b/packages/umi-eddsa-web3js/test/Web3JsEddsa.test.ts index 5526cf0e..29ae4f82 100644 --- a/packages/umi-eddsa-web3js/test/Web3JsEddsa.test.ts +++ b/packages/umi-eddsa-web3js/test/Web3JsEddsa.test.ts @@ -1,6 +1,38 @@ +import { isPublicKey, utf8 } from '@metaplex-foundation/umi'; import test from 'ava'; -import { web3JsEddsa } from '../src'; +import { createWeb3JsEddsa } from '../src'; -test('example test', async (t) => { - t.is(typeof web3JsEddsa, 'function'); +test('it can create a new private key', async (t) => { + // Given an EDDSA interface. + const eddsa = createWeb3JsEddsa(); + + // When we generate a new keypair. + const keypair = eddsa.generateKeypair(); + + // Then the public key is valid. + t.true(isPublicKey(keypair.publicKey)); + t.true(eddsa.isOnCurve(keypair.publicKey)); + + // And the secret key is valid. + t.true( + typeof keypair.secretKey === 'object' && + typeof keypair.secretKey.BYTES_PER_ELEMENT === 'number' && + typeof keypair.secretKey.length === 'number' && + keypair.secretKey.BYTES_PER_ELEMENT === 1 && + keypair.secretKey.length === 64 + ); +}); + +test('it can sign and verify messages', async (t) => { + // Given a keypair. + const eddsa = createWeb3JsEddsa(); + const keypair = eddsa.generateKeypair(); + + // When that keypair signs a message and then verifies it. + const message = utf8.serialize('Hello world!'); + const signature = eddsa.sign(message, keypair); + const verified = eddsa.verify(message, signature, keypair.publicKey); + + // Then we expect the signature to be valid. + t.true(verified); }); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 707d0328..f404ec63 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -212,9 +212,9 @@ importers: '@metaplex-foundation/umi-web3js-adapters': specifier: workspace:^ version: link:../umi-web3js-adapters - '@noble/ed25519': - specifier: ^1.7.1 - version: 1.7.1 + '@noble/curves': + specifier: ^1.0.0 + version: 1.0.0 devDependencies: '@ava/typescript': specifier: ^3.0.1 @@ -3928,12 +3928,22 @@ packages: - utf-8-validate dev: false + /@noble/curves@1.0.0: + resolution: {integrity: sha512-2upgEu0iLiDVDZkNLeFV2+ht0BAVgQnEmCk6JsOch9Rp8xfkMCbvbAZlA2pBHQc73dbl+vFOXfqkf4uemdn0bw==} + dependencies: + '@noble/hashes': 1.3.0 + dev: false + /@noble/ed25519@1.7.1: resolution: {integrity: sha512-Rk4SkJFaXZiznFyC/t77Q0NKS4FL7TLJJsVG2V2oiEq3kJVeTdxysEe/yRWSpnWMe808XRDJ+VFh5pt/FN5plw==} /@noble/hashes@1.2.0: resolution: {integrity: sha512-FZfhjEDbT5GRswV3C6uvLPHMiVD6lQBmpoX5+eSiPaMTXte/IKqI5dykDxzZB/WBeK/CDuQRBWarPdi3FNY2zQ==} + /@noble/hashes@1.3.0: + resolution: {integrity: sha512-ilHEACi9DwqJB0pw7kv+Apvh50jiiSyR/cQ3y4W7lOR5mhvn/50FLUfsnfJz0BDZtl/RR16kXvptiv6q1msYZg==} + dev: false + /@noble/secp256k1@1.7.0: resolution: {integrity: sha512-kbacwGSsH/CTout0ZnZWxnW1B+jH/7r/WAAKLBtrRJ/+CUH7lgmQzl3GTrQua3SGKWNSDsS6lmjnDpIJ5Dxyaw==}