Skip to content
This repository was archived by the owner on Mar 7, 2023. It is now read-only.

Commit b39e7d7

Browse files
authored
codegen typescript coin type and protobuf messages (trustwallet#1024)
* codegen coin type and protobuf messages * move protobufjs to dependencies * add check version script * map step outputs to job outputs * read current script dir * remove .npmrc * npm publish public * add npm version check script * add filter paths * update outputs
1 parent 4ff8fad commit b39e7d7

14 files changed

+1521
-2
lines changed

.github/workflows/ts-ci.yml

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: Typescript CI
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
paths:
7+
- typescript/**
8+
pull_request:
9+
branches: [ master ]
10+
paths:
11+
- typescript/**
12+
13+
jobs:
14+
build:
15+
runs-on: ubuntu-latest
16+
strategy:
17+
matrix:
18+
node-version: [14.x]
19+
outputs:
20+
publish_npm: ${{ steps.version.outputs.publish_npm }}
21+
publish_gpr: ${{ steps.version.outputs.publish_gpr }}
22+
steps:
23+
- uses: actions/checkout@v2
24+
- name: Use Node.js ${{ matrix.node-version }}
25+
uses: actions/setup-node@v1
26+
with:
27+
node-version: ${{ matrix.node-version }}
28+
- run: yarn install
29+
working-directory: typescript
30+
- name: Build and test
31+
run: yarn build && yarn test
32+
working-directory: typescript
33+
- name: Check if needed to publish
34+
id: version
35+
run: |
36+
echo "::set-output name=publish_npm::$(tools/check-npm-version)"
37+
echo "::set-output name=publish_gpr::$(tools/check-gpr-version)"
38+
working-directory: typescript
39+
env:
40+
TOKEN: ${{secrets.GITHUB_TOKEN}}
41+
publish-npm:
42+
needs: build
43+
if: needs.build.outputs.publish_npm == 'true'
44+
runs-on: ubuntu-latest
45+
steps:
46+
- uses: actions/checkout@v2
47+
- uses: actions/setup-node@v1
48+
with:
49+
node-version: 14
50+
registry-url: https://registry.npmjs.org/
51+
- name: Publish
52+
run: |
53+
yarn install && yarn build && npm publish --access public
54+
working-directory: typescript
55+
env:
56+
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
57+
publish-gpr:
58+
needs: build
59+
if: needs.build.outputs.publish_gpr == 'true'
60+
runs-on: ubuntu-latest
61+
steps:
62+
- uses: actions/checkout@v2
63+
- uses: actions/setup-node@v1
64+
with:
65+
node-version: 14
66+
registry-url: https://npm.pkg.github.com/
67+
- name: Publish
68+
run: |
69+
yarn install && yarn build && npm publish --access public
70+
working-directory: typescript
71+
env:
72+
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Trust Wallet Core is a cross-platform library that implements low-level cryptogr
55
![iOS CI](https://github.com/trustwallet/wallet-core/workflows/iOS%20CI/badge.svg)
66
![Android CI](https://github.com/trustwallet/wallet-core/workflows/Android%20CI/badge.svg)
77
![Linux CI](https://github.com/trustwallet/wallet-core/workflows/Linux%20CI/badge.svg)
8+
![Docker CI](https://github.com/trustwallet/wallet-core/workflows/Docker%20CI/badge.svg)
9+
![Typescript CI](https://github.com/trustwallet/wallet-core/workflows/Typescript%20CI/badge.svg)
810

911
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/82e76f6ea4ba4f0d9029e8846c04c093)](https://www.codacy.com/app/hewigovens/wallet-core?utm_source=github.com&utm_medium=referral&utm_content=TrustWallet/wallet-core&utm_campaign=Badge_Grade)
1012
![Codecov](https://codecov.io/gh/TrustWallet/wallet-core/branch/master/graph/badge.svg)

coins.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -906,10 +906,10 @@
906906
"symbol": "IOTX",
907907
"decimals": 18,
908908
"blockchain": "IoTeX",
909-
"hrp": "io",
910909
"derivationPath": "m/44'/304'/0'/0/0",
911910
"curve": "secp256k1",
912911
"publicKeyType": "secp256k1Extended",
912+
"hrp": "io",
913913
"explorer": {
914914
"url": "https://iotexscan.io",
915915
"txPath": "/action/",
@@ -1119,10 +1119,10 @@
11191119
"symbol": "ONE",
11201120
"decimals": 18,
11211121
"blockchain": "Harmony",
1122-
"hrp": "one",
11231122
"derivationPath": "m/44'/1023'/0'/0/0",
11241123
"curve": "secp256k1",
11251124
"publicKeyType": "secp256k1Extended",
1125+
"hrp": "one",
11261126
"explorer": {
11271127
"url": "https://explorer.harmony.one",
11281128
"txPath": "/#/tx/",

tools/android-release.sh

100644100755
File mode changed.

typescript/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist/
2+
src/generated/

typescript/codegen/bin/codegen

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#! /usr/bin/env node
2+
const fs = require('fs').promises;
3+
const path = require('path');
4+
const ejs = require('ejs');
5+
const prettier = require("prettier");
6+
7+
const main = async () => {
8+
const coins = require('../../../coins.json')
9+
coins.forEach((coin) => {
10+
coin.slip44 = coin['derivationPath'].split('/')[2].replace('\'', '');
11+
})
12+
await generateCoinType(coins);
13+
};
14+
15+
const generateCoinType = async (coins) => {
16+
const methods = [
17+
{
18+
name: 'id',
19+
returnType: 'string',
20+
body: (coin) => `return '${coin.id}'`
21+
},
22+
{
23+
name: 'decimals',
24+
returnType: 'number',
25+
body: (coin) => `return ${coin.decimals}`
26+
},
27+
{
28+
name: 'name',
29+
returnType: 'string',
30+
body: (coin) => `return '${coin.name}'`
31+
},
32+
{
33+
name: 'derivationPath',
34+
returnType: 'string',
35+
body: (coin) => `return "${coin.derivationPath}"`
36+
},
37+
{
38+
name: 'symbol',
39+
returnType: 'string',
40+
body: (coin) => `return '${coin.symbol}'`
41+
}
42+
];
43+
44+
const template = await fs.readFile(path.resolve(__dirname, '../templates/core_types.ejs'), 'utf8');
45+
let data = await ejs.render(template, { coins, methods });
46+
data = await prettier.format(data, { parser: 'typescript', singleQuote: true, trailingComma: 'es5' });
47+
await fs.writeFile(path.resolve(__dirname, '../../src/generated/core_types.ts'), data);
48+
};
49+
50+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright © 2017-2020 Trust Wallet.
2+
//
3+
// This file is part of Trust. The full Trust copyright notice, including
4+
// terms governing use, modification, and redistribution, is contained in the
5+
// file LICENSE at the root of the source code distribution tree.
6+
7+
export enum CoinType {
8+
<% coins.forEach((coin) => { -%>
9+
<%-coin.id%> = <%-coin.slip44%>,
10+
<% }) %>
11+
}
12+
13+
export namespace CoinType {
14+
<% methods.forEach((method) => { -%>
15+
export function <%-method.name%>(coin: CoinType): <%-method.returnType%> {
16+
switch (coin) {
17+
<% coins.forEach((coin) => { -%>
18+
case CoinType.<%-coin.id%>: <%-method.body(coin)%>;
19+
<% }) %>
20+
}
21+
}
22+
<% }) %>
23+
}

typescript/package.json

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "@trustwallet/wallet-core",
3+
"version": "0.0.6",
4+
"description": "wallet core types and protobuf messages",
5+
"main": "dist/index.js",
6+
"types": "dist/index.d.ts",
7+
"scripts": {
8+
"test": "mocha -r ts-node/register tests/**/*.test.ts",
9+
"generate": "yarn codegen:coin && yarn codegen:js && yarn codegen:ts",
10+
"codegen:coin": "codegen/bin/codegen",
11+
"codegen:js": "pbjs -t static-module ../src/proto/Ethereum.proto --no-delimited --force-long -o src/generated/core_proto.js",
12+
"codegen:ts": "pbts -o src/generated/core_proto.d.ts src/generated/core_proto.js",
13+
"clean": "rm -rf dist src/generated && mkdir -p dist/generated src/generated",
14+
"build": "yarn clean && yarn generate && cp src/generated/core_proto.* dist/generated && tsc --skipLibCheck"
15+
},
16+
"repository": {
17+
"type": "git",
18+
"url": "git://github.com/trustwallet/wallet-core.git"
19+
},
20+
"author": "",
21+
"license": "MIT",
22+
"bugs": {
23+
"url": "https://github.com/trustwallet/wallet-core/issues"
24+
},
25+
"homepage": "https://github.com/trustwallet/wallet-core#readme",
26+
"files": [
27+
"dist"
28+
],
29+
"dependencies": {
30+
"protobufjs": "^6.9.0"
31+
},
32+
"devDependencies": {
33+
"@types/chai": "^4.2.11",
34+
"@types/mocha": "^7.0.2",
35+
"buffer": "^5.6.0",
36+
"chai": "^4.2.0",
37+
"ejs": "^3.1.3",
38+
"escodegen": "^1.14.3",
39+
"jsdoc": "^3.6.4",
40+
"mocha": "^8.0.1",
41+
"prettier": "^2.0.5",
42+
"ts-node": "^8.10.2",
43+
"typescript": "^3.9.5"
44+
}
45+
}

typescript/src/index.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright © 2017-2020 Trust Wallet.
2+
//
3+
// This file is part of Trust. The full Trust copyright notice, including
4+
// terms governing use, modification, and redistribution, is contained in the
5+
// file LICENSE at the root of the source code distribution tree.
6+
7+
import { CoinType } from './generated/core_types'
8+
import { TW } from './generated/core_proto'
9+
10+
export { TW, CoinType }

typescript/tests/index.test.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright © 2017-2020 Trust Wallet.
2+
//
3+
// This file is part of Trust. The full Trust copyright notice, including
4+
// terms governing use, modification, and redistribution, is contained in the
5+
// file LICENSE at the root of the source code distribution tree.
6+
7+
import 'mocha'
8+
import { expect } from 'chai'
9+
import { Buffer } from 'buffer'
10+
import { TW, CoinType } from '../dist'
11+
12+
describe('Wallet Core types tests', () => {
13+
14+
it('test CoinType.ethereum', () => {
15+
const coin = CoinType.ethereum;
16+
expect(coin).to.equal(60)
17+
expect(CoinType.id(coin)).to.equal('ethereum')
18+
expect(CoinType.name(coin)).to.equal('Ethereum')
19+
expect(CoinType.symbol(coin)).to.equal('ETH')
20+
expect(CoinType.decimals(coin)).to.equal(18)
21+
expect(CoinType.derivationPath(coin)).to.equal(`m/44'/60'/0'/0/0`)
22+
})
23+
24+
it('test Ethereum encoding SigningInput', () => {
25+
const input = TW.Ethereum.Proto.SigningInput.create({
26+
toAddress: '0x3535353535353535353535353535353535353535',
27+
chainId: Buffer.from('01', 'hex'),
28+
nonce: Buffer.from('09', 'hex'),
29+
gasPrice: Buffer.from('04a817c800', 'hex'),
30+
gasLimit: Buffer.from('5208', 'hex'),
31+
amount: Buffer.from('0de0b6b3a7640000', 'hex'),
32+
privateKey: Buffer.from('4646464646464646464646464646464646464646464646464646464646464646', 'hex')
33+
});
34+
35+
const encoded = TW.Ethereum.Proto.SigningInput.encode(input).finish()
36+
expect(Buffer.from(encoded).toString('hex')).to.equal("0a01011201091a0504a817c800220252082a2a30783335333533353335333533353335333533353335333533353335333533353335333533353335333532080de0b6b3a764000042204646464646464646464646464646464646464646464646464646464646464646")
37+
})
38+
})

typescript/tools/check-gpr-version

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
set -e
4+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
5+
VERSION=$(cat "$DIR/../package.json" | jq '.version')
6+
GHR_KEY_PATH='.data.repository.packages.edges[0].node.latestVersion.version'
7+
GHR_VERSION=$(curl -X "POST" "https://api.github.com/graphql" \
8+
-H "Authorization: Bearer $TOKEN" \
9+
-H 'Content-Type: application/json; charset=utf-8' \
10+
-d $'{"query": "query { repository(owner: \\"trustwallet\\", name:\\"wallet-core\\") { name packages(names: \\"wallet-core\\", first: 1) { edges { node { name latestVersion { version summary } } } } } } "}' \
11+
| jq $GHR_KEY_PATH)
12+
13+
if [[ $VERSION != $GHR_VERSION ]]; then
14+
echo true
15+
else
16+
echo false
17+
fi

typescript/tools/check-npm-version

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
6+
VERSION=$(cat "$DIR/../package.json" | jq '.version')
7+
NPM_VERSION=\"$(npm view @trustwallet/wallet-core version)\"
8+
9+
if [[ $VERSION != $NPM_VERSION ]]; then
10+
echo true
11+
else
12+
echo false
13+
fi

typescript/tsconfig.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "commonjs",
5+
"declaration": true,
6+
"outDir": "./dist",
7+
"strict": true,
8+
"typeRoots": [
9+
"./node_modules/@types"
10+
],
11+
"types": [
12+
"node"
13+
],
14+
"noImplicitAny": false
15+
},
16+
"exclude": [
17+
"node_modules",
18+
"./tests/**/*.ts"
19+
],
20+
}

0 commit comments

Comments
 (0)