Skip to content

Commit

Permalink
Use @noble/curves instead of @noble/ed25519 (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva authored May 17, 2023
1 parent 5f2a0e7 commit 9ca68e2
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-pillows-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@metaplex-foundation/umi-eddsa-web3js': patch
---

Use @noble/curves instead of @noble/ed25519
2 changes: 1 addition & 1 deletion packages/umi-eddsa-web3js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:^",
Expand Down
1 change: 1 addition & 0 deletions packages/umi-eddsa-web3js/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import pkg from './package.json';

export default createConfigs({
pkg,
additionalExternals: ['@noble/curves/ed25519'],
builds: [
{
dir: 'dist/esm',
Expand Down
6 changes: 3 additions & 3 deletions packages/umi-eddsa-web3js/src/createWeb3JsEddsa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
38 changes: 35 additions & 3 deletions packages/umi-eddsa-web3js/test/Web3JsEddsa.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
16 changes: 13 additions & 3 deletions pnpm-lock.yaml

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

0 comments on commit 9ca68e2

Please sign in to comment.