Skip to content

Commit

Permalink
Merge pull request #22 from lPernod-sorare/loicp/modern-bigint
Browse files Browse the repository at this point in the history
[BREAKING] Use BigInt instead of bn.js

- Change type of generateKey
- Deprecate ec.KeyPair exports
- Deprecate USE_STARKNET_CPP
  • Loading branch information
lPernod-sorare committed Jul 20, 2023
2 parents a8e87a9 + a07f955 commit de102c2
Show file tree
Hide file tree
Showing 17 changed files with 506 additions and 3,980 deletions.
3 changes: 1 addition & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"presets": ["@babel/env", "@babel/typescript"],
"plugins": ["macros"]
"presets": ["@babel/env", "@babel/typescript"]
}
3 changes: 1 addition & 2 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,5 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: USE_STARKWARE_CRYPTO_CPP=true npm install
- run: npm install
- run: npm test
- run: USE_STARKWARE_CRYPTO_CPP=true npm test
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 2.0.0

- Breaking change: Add dependency on BigInt support for all operations
- Previously only necessary when using USE_STARKWARE_CPP=true
- DEPRECATE C++ bindings
- Breaking change, remove all references to `elliptic` and `bn.js`
- `generateKey` now returns the hex encoded private Key instead of elliptic KeyPair (no hex prefix)

## Migration guide

```js
// @sorare.crypto@^1.0.0
const keypair = generateKey();
const privateKey = exportPrivateKey(keypair);
const publicKey = exportPublicKey(keypair);

// @sorare/crypto@2.0.0
const privateKey = generateKey();
const publicKey = exportPublicKey(privateKey);
```
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,8 @@ const limitOrder = /* The LimitOrder object you get from GraphQL */;
const signature = signLimitOrder(privateKey, limitOrder);
```

# C++ bindings

Starting from v1.3.2, C++ bindings can be used for faster `verifyTransfer` and `verifyLimitOrder` operations. It uses Starkware's [crypto-cpp](https://github.com/starkware-libs/crypto-cpp) library and can be enabled by adding `USE_STARKWARE_CRYPTO_CPP=true` to your env. Please note that this variable also needs to be set when `yarn install` runs in order to build the C++ bindings using [node-gyp](https://github.com/nodejs/node-gyp).

# Release

- Clone git submodules
- Bump version in package.json
- Run `yarn release`

Expand Down
35 changes: 0 additions & 35 deletions binding.gyp

This file was deleted.

1 change: 0 additions & 1 deletion deps/crypto-cpp
Submodule crypto-cpp deleted from 658055
36 changes: 8 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,23 @@
"description": "Crypto used in the Sorare stack",
"author": "Sorare",
"license": "MIT",
"version": "1.3.5",
"version": "2.0.0",
"main": "lib.js",
"dependencies": {
"bigint-buffer": "^1.1.5",
"bip39": "^3.0.4",
"bn.js": "^5.2.0",
"elliptic": "^6.5.4",
"enc-utils": "^3.0.0",
"ethereumjs-wallet": "^1.0.1",
"ffi-napi": "^4.0.3",
"hash.js": "^1.1.7",
"patch-package": "^6.4.7"
"@noble/curves": "^1.0.0",
"@noble/hashes": "~1.3.0",
"@scure/bip32": "1.3.0",
"@scure/bip39": "^1.2.1",
"micro-starknet": "0.2.3"
},
"devDependencies": {
"@babel/cli": "^7.16.0",
"@babel/core": "^7.16.5",
"@babel/plugin-transform-destructuring": "^7.16.5",
"@babel/preset-env": "^7.16.5",
"@babel/preset-typescript": "^7.16.5",
"@babel/runtime": "^7.16.5",
"@types/elliptic": "^6.4.14",
"@types/jest": "^27.0.3",
"@types/webpack-env": "^1.16.3",
"@typescript-eslint/eslint-plugin": "^5.7.0",
"@typescript-eslint/parser": "^5.7.0",
"babel-plugin-macros": "^3.1.0",
"eslint": "^8.4.1",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^8.3.0",
Expand All @@ -37,11 +28,7 @@
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-prettier": "^4.0.0",
"jest": "^27.4.5",
"patch-package": "^6.4.7",
"prettier": "^2.5.1",
"prettier-eslint": "^13.0.0",
"source-map-explorer": "^2.5.2",
"ts-loader": "^9.2.6",
"typescript": "^4.5.4"
},
"prettier": {
Expand All @@ -56,27 +43,20 @@
"build:types": "tsc --emitDeclarationOnly",
"build:js": "babel src --out-dir dist --extensions \".js,.ts\" --source-maps inline",
"prepublish": "yarn run build",
"install": "(test -n \"$USE_STARKWARE_CRYPTO_CPP\" && node-gyp rebuild) || true",
"postinstall": "patch-package",
"test": "jest ./src",
"package": "yarn build && rm dist/index.test.* && yarn pack",
"release": "yarn clean && yarn version && yarn package && npm publish *.tgz --access public"
},
"files": [
"deps/crypto-cpp/LICENSE",
"deps/crypto-cpp/src/**/*.cc",
"deps/crypto-cpp/src/**/*.inl",
"deps/crypto-cpp/src/**/*.hpp",
"deps/crypto-cpp/src/**/*.h",
"dist",
"binding.gyp",
"lib.js",
"lib.d.ts"
],
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
"not op_mini all",
"supports bigint"
]
}
13 changes: 0 additions & 13 deletions patches/secp256k1+4.0.2.patch

This file was deleted.

52 changes: 18 additions & 34 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { starkEc } from './starkware/signature';

import {
generateKey,
exportPublicKey,
signTransfer,
signLimitOrder,
verifyTransfer,
Expand All @@ -16,11 +15,11 @@ describe('generateKey', () => {
'range mountain blast problem vibrant void vivid doctor cluster enough melody ' +
'salt layer language laptop boat major space monkey unit glimpse pause change vibrant';

const keyPair = generateKey(mnemonic);
const privateKey = generateKey(mnemonic);

it('generates the expected private key', () => {
expect(keyPair.getPrivate('hex')).toEqual(
'0646baa9aefb054dfa205c94b43781baa7b2ec2dfbcee64dbd9d2172283de902'
expect(privateKey).toEqual(
'0x0646baa9aefb054dfa205c94b43781baa7b2ec2dfbcee64dbd9d2172283de902'
);
});
});
Expand Down Expand Up @@ -53,13 +52,8 @@ describe('transfer', () => {
});

it('can be verified', () => {
const publicKey = starkEc
.keyFromPrivate(privateKey.substring(2), 'hex')
.getPublic(true, 'hex');

expect(verifyTransfer(`0x${publicKey}`, transfer, { r, s })).toEqual(
true
);
const publicKey = exportPublicKey(privateKey);
expect(verifyTransfer(publicKey, transfer, { r, s })).toEqual(true);
});
});

Expand All @@ -85,13 +79,11 @@ describe('transfer', () => {
});

it('can be verified', () => {
const publicKey = starkEc
.keyFromPrivate(privateKey.substring(2), 'hex')
.getPublic(true, 'hex');
const publicKey = exportPublicKey(privateKey);

expect(
verifyTransfer(`0x${publicKey}`, transferWithFee, { r, s })
).toEqual(true);
expect(verifyTransfer(publicKey, transferWithFee, { r, s })).toEqual(
true
);
});
});
});
Expand Down Expand Up @@ -126,13 +118,9 @@ describe('limitOrder', () => {
});

it('can be verified', () => {
const publicKey = starkEc
.keyFromPrivate(privateKey.substring(2), 'hex')
.getPublic(true, 'hex');
const publicKey = exportPublicKey(privateKey);

expect(verifyLimitOrder(`0x${publicKey}`, limitOrder, { r, s })).toEqual(
true
);
expect(verifyLimitOrder(publicKey, limitOrder, { r, s })).toEqual(true);
});
});

Expand All @@ -159,13 +147,11 @@ describe('limitOrder', () => {
});

it('can be verified', () => {
const publicKey = starkEc
.keyFromPrivate(privateKey.substring(2), 'hex')
.getPublic(true, 'hex');
const publicKey = exportPublicKey(privateKey);

expect(
verifyLimitOrder(`0x${publicKey}`, limitOrderWithFee, { r, s })
).toEqual(true);
expect(verifyLimitOrder(publicKey, limitOrderWithFee, { r, s })).toEqual(
true
);
});
});
});
Expand All @@ -174,12 +160,10 @@ describe('signMessage', () => {
const message = 'random message';
const privateKey =
'0x03c1e9550e66958296d11b60f8e8e7a7ad990d07fa65d5f7652c4a6c87d4e3cc';
const publicKey = starkEc
.keyFromPrivate(privateKey.substring(2), 'hex')
.getPublic(true, 'hex');
const publicKey = exportPublicKey(privateKey);

it('generates a signature that can be verified', () => {
const signature = signMessage(privateKey, message);
expect(verifyMessage(`0x${publicKey}`, message, signature)).toBeTruthy();
expect(verifyMessage(publicKey, message, signature)).toBeTruthy();
});
});

0 comments on commit de102c2

Please sign in to comment.