Skip to content

Commit

Permalink
feat: typescript support (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-ziv committed May 7, 2023
1 parent 1a945b2 commit bcdb84b
Show file tree
Hide file tree
Showing 25 changed files with 765 additions and 390 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
dist
13 changes: 10 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@
"es2021": true,
"node": true
},
"extends": ["prettier", "eslint:recommended"],
"parser": "@typescript-eslint/parser",
"extends": [
"prettier",
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"parserOptions": {
"ecmaVersion": 12
"ecmaVersion": 12,
"sourceType": "module"
},
"plugins": ["prettier"],
"plugins": ["@typescript-eslint", "prettier"],
"rules": {
"prettier/prettier": "error",
"no-console": "warn"
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@ jobs:
- name: Install node_modules
run: yarn

- name: Build
run: yarn run build

- name: Run tests
run: yarn run test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*
*.log

docs
6 changes: 0 additions & 6 deletions .mocharc.js

This file was deleted.

1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
dist
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const starkwareCrypto = require('@starkware-industries/starkware-crypto-utils');
keyDerivation: {
StarkExEc: ec.n, // Data.
getPrivateKeyFromEthSignature,
privateToStarkKey,
privateToStarkKey,
getKeyPairFromPath,
getAccountPath,
grindKey // Function.
Expand Down
39 changes: 24 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,33 @@
},
"license": "Apache-2.0",
"author": "StarkWare Industries Ltd.",
"main": "src/js/index.js",
"directories": {
"test": "test"
},
"main": "dist/src/js/index.js",
"types": "dist/src/js/index.d.ts",
"files": [
"src/config",
"src/js",
"dist",
"package.json",
"README.md",
"CHANGELOG.md"
],
"scripts": {
"test": "mocha",
"prebuild": "npm run clean",
"build": "tsc --build",
"clean": "rm -rf ./dist",
"docs:generate": "typedoc",
"format": "prettier --write \"**/*.+(ts|js|json|md|html|yml)\"",
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"pre-release": "standard-version --prerelease --skip.changelog --releaseCommitMessageFormat 'chore(prerelease): {{currentTag}}'",
"pushTaggedRelease": "git push --follow-tags --no-verify origin master",
"release": "standard-version",
"release:dry": "standard-version --dry-run",
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"format": "prettier --write \"**/*.+(ts|js|json|md|html|yml)\""
"test": "tsc --build && mocha 'dist/test/**/*.spec.js' --recursive"
},
"dependencies": {
"bip39": "^3.0.4",
"bn.js": "^4.12.0",
"brorand": "^1.1.0",
"elliptic": "v6.5.4",
"elliptic": "^6.5.4",
"enc-utils": "^3.0.0",
"ethereumjs-wallet": "^1.0.2",
"hash.js": "^1.1.7",
Expand All @@ -56,12 +57,20 @@
"minimalistic-crypto-utils": "^1.0.1"
},
"devDependencies": {
"chai": "^4.3.6",
"@types/bn.js": "^5.1.1",
"@types/chai": "^4.3.4",
"@types/elliptic": "^6.4.14",
"@types/mocha": "^10.0.1",
"@typescript-eslint/eslint-plugin": "^5.59.0",
"@typescript-eslint/parser": "^5.59.0",
"chai": "^4.3.7",
"eslint": "^8.11.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"mocha": "^9.2.2",
"prettier": "^2.5.1",
"standard-version": "^9.3.2"
"mocha": "^10.2.0",
"prettier": "^2.8.7",
"standard-version": "^9.3.2",
"typedoc": "^0.23.16",
"typescript": "^4.8.4"
}
}
43 changes: 22 additions & 21 deletions src/js/asset.js → src/js/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
// and limitations under the License. //
/////////////////////////////////////////////////////////////////////////////////

const BN = require('bn.js');
const encUtils = require('enc-utils');
const sha3 = require('js-sha3');
const assert = require('assert');
import assert from 'assert';
import BN from 'bn.js';
import * as encUtils from 'enc-utils';
import sha3 from 'js-sha3';
import {AssetDict} from './types';

// Generate BN of 1.
const oneBn = new BN('1', 16);
Expand All @@ -39,7 +40,7 @@ const maskMintabilityBit = new BN(
16
);

function getAssetType(assetDict) {
function getAssetType(assetDict: AssetDict) {
const assetSelector = getAssetSelector(assetDict.type);

// Expected length is maintained to fix the length of the resulting asset info string in case of
Expand Down Expand Up @@ -67,16 +68,16 @@ function getAssetType(assetDict) {
assetInfo = assetInfo.ushln(256).add(quantum);
expectedLen += 64;

let assetType = sha3.keccak_256(
const assetEncoded = sha3.keccak_256(
encUtils.hexToBuffer(addLeadingZeroes(assetInfo.toJSON(), expectedLen))
);
assetType = new BN(assetType, 16);
let assetType = new BN(assetEncoded, 16);
assetType = assetType.and(mask);

return '0x' + assetType.toJSON();
}

/*
/**
Computes the hash representing the encoded asset ID for a given asset.
asset is a dictionary containing the type and data of the asset to parse. the asset type is
represented by a string describing the associated asset while the data is a dictionary
Expand All @@ -95,7 +96,7 @@ function getAssetType(assetDict) {
'0x352386d5b7c781d47ecd404765307d74edc4d43b0490b8e03c71ac7a7429653'.
*/
function getAssetId(assetDict) {
function getAssetId(assetDict: AssetDict) {
const assetType = new BN(
encUtils.removeHexPrefix(getAssetType(assetDict)),
16
Expand All @@ -115,11 +116,11 @@ function getAssetId(assetDict) {
assetInfo = assetInfo.ushln(256).add(assetType);
assetInfo = assetInfo
.ushln(256)
.add(new BN(parseInt(assetDict.data.tokenId), 16));
assetId = sha3.keccak_256(
.add(new BN(parseInt(assetDict.data.tokenId as string), 16));
const assetEncoded = sha3.keccak_256(
encUtils.hexToBuffer(addLeadingZeroes(assetInfo.toJSON(), expectedLen))
);
assetId = new BN(assetId, 16);
assetId = new BN(assetEncoded, 16);
assetId = assetId.and(mask);
} else if (
assetDict.type === 'MINTABLE_ERC721' ||
Expand All @@ -134,25 +135,25 @@ function getAssetId(assetDict) {
// calculation discarded them in the process.
const expectedLen = 18 + 64 + 64;
assetInfo = assetInfo.ushln(256).add(assetType);
const blobHash = blobToBlobHash(assetDict.data.blob);
const blobHash = blobToBlobHash(assetDict.data.blob as string);
assetInfo = assetInfo
.ushln(256)
.add(new BN(encUtils.removeHexPrefix(blobHash), 16));
assetId = sha3.keccak_256(
const assetEncoded = sha3.keccak_256(
encUtils.hexToBuffer(addLeadingZeroes(assetInfo.toJSON(), expectedLen))
);
assetId = new BN(assetId, 16);
assetId = new BN(assetEncoded, 16);
assetId = assetId.and(mask240);
assetId = assetId.or(maskMintabilityBit);
}

return '0x' + assetId.toJSON();
}

/*
/**
Computes the given asset's unique selector based on its type.
*/
function getAssetSelector(assetDictType) {
function getAssetSelector(assetDictType: string) {
let seed = '';
switch (assetDictType.toUpperCase()) {
case 'ETH':
Expand All @@ -176,10 +177,10 @@ function getAssetSelector(assetDictType) {
return encUtils.sanitizeHex(sha3.keccak_256(seed).slice(0, 8));
}

/*
/**
Adds leading zeroes to the input hex-string to complement the expected length.
*/
function addLeadingZeroes(hexStr, expectedLen) {
function addLeadingZeroes(hexStr: string, expectedLen: number) {
let res = hexStr;
assert(res.length <= expectedLen);
while (res.length < expectedLen) {
Expand All @@ -188,11 +189,11 @@ function addLeadingZeroes(hexStr, expectedLen) {
return res;
}

function blobToBlobHash(blob) {
function blobToBlobHash(blob: string) {
return '0x' + sha3.keccak_256(blob);
}

module.exports = {
export {
getAssetType,
getAssetSelector,
getAssetId // Function.
Expand Down
6 changes: 0 additions & 6 deletions src/js/index.js

This file was deleted.

6 changes: 6 additions & 0 deletions src/js/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as signature from './signature';
import * as asset from './asset';
import * as keyDerivation from './key_derivation';
import * as messageUtils from './message_utils';

module.exports = {...signature, asset, keyDerivation, messageUtils};
Loading

0 comments on commit bcdb84b

Please sign in to comment.