Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/Lattice ETH signer #3

Open
wants to merge 2 commits into
base: gp/add-gridplus-signer
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{
"name": "@xdefi-tech/chains",
"version": "1.0.0",
"workspaces": ["utility-packages/*", "packages/*", "examples/*"],
"private": true,
"workspaces": [
"utility-packages/*",
"packages/*",
"examples/*"
],
"scripts": {
"prepare": "husky install",
"build": "turbo run build",
Expand Down
2 changes: 1 addition & 1 deletion packages/bitcoin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"bitcoinjs-lib": "5.2.0",
"coinselect": "3.1.13",
"eslint-config-custom": "*",
"gridplus-sdk": "^2.5.2",
"reflect-metadata": "0.1.13",
"rimraf": "4.4.0",
"rxjs": "7.8.0",
Expand All @@ -62,6 +61,7 @@
"src/signers/react-native.ts",
"src/index.ts"
],
"outDir": "dist",
"format": "cjs",
"splitting": false,
"dts": true,
Expand Down
17 changes: 17 additions & 0 deletions packages/bitcoin/src/signers/lattice.signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ import type { ChainMsg } from '../msg';

@SignerDecorator(Signer.SignerType.LATTICE)
export class LatticeSigner extends Signer.LatticeProvider {
static async create({
deviceId,
password,
name,
}: {
deviceId: string;
password: string;
name: string;
}): Promise<LatticeSigner> {
const { clientData, isPaired } = await super.init({
deviceId,
password,
name,
});
return new LatticeSigner(clientData, isPaired);
}

verifyAddress(address: string): boolean {
try {
Bitcoin.address.toOutputScript(address);
Expand Down
2 changes: 1 addition & 1 deletion packages/bitcoin/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"isolatedModules": false
},
"allowJs": true,
"include": ["./src/custom.d.ts", "./src/**/*"],
"include": ["./src/custom.d.ts", "./src/**/*"]
}
27 changes: 21 additions & 6 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
"import": "./dist/index.js"
}
},
"files": ["dist", "CHANGELOG.md", "README.md"],
"files": [
"dist",
"CHANGELOG.md",
"README.md"
],
"devDependencies": {
"@types/lodash": "4.14.182",
"@types/uuid": "9.0.1",
Expand All @@ -32,7 +36,7 @@
"@next/bundle-analyzer": "12.3.0",
"@trezor/connect-web": "9.1.4",
"bignumber.js": "9.1.2",
"gridplus-sdk": "^2.5.2",
"gridplus-sdk": "^2.6.0",
"graphql": "16.6.0",
"graphql-ws": "5.11.1",
"inversify": "6.0.1",
Expand All @@ -59,12 +63,18 @@
},
"type": "commonjs",
"tsup": {
"entry": ["src/index.ts", "!src/**/*.spec.*", "!src/custom.d.ts"],
"entry": [
"src/index.ts",
"!src/**/*.spec.*",
"!src/custom.d.ts"
],
"outDir": "dist",
"format": "cjs",
"splitting": false,
"dts": true,
"types": ["./dist/index.d.ts"],
"types": [
"./dist/index.d.ts"
],
"platform": "browser",
"target": "ES6"
},
Expand All @@ -74,9 +84,14 @@
"transform": {
".+\\.(t|j)s$": "ts-jest"
},
"modulePathIgnorePatterns": ["<rootDir>/dist/"],
"modulePathIgnorePatterns": [
"<rootDir>/dist/"
],
"testEnvironment": "jsdom",
"watchPlugins": ["jest-watch-typeahead/filename", "jest-watch-typeahead/testname"],
"watchPlugins": [
"jest-watch-typeahead/filename",
"jest-watch-typeahead/testname"
],
"testTimeout": 15000
}
}
17 changes: 5 additions & 12 deletions packages/core/src/core/signer/lattice.provider.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { setup } from 'gridplus-sdk';

import { Injectable } from '../../common';
import type { MsgData } from '../msg';

import { Provider } from './signer.provider';

const HARDENED_OFFSET = 0x80000000; // Hardened offset

@Injectable()
export class LatticeProvider extends Provider {
private static instance: LatticeProvider;
public initialized = false;
Expand All @@ -21,15 +19,7 @@ export class LatticeProvider extends Provider {
this.initialized = true;
}

static async create({
deviceId,
password,
name,
}: {
deviceId: string;
password: string;
name: string;
}): Promise<LatticeProvider> {
static async init({ deviceId, password, name }: { deviceId: string; password: string; name: string }) {
let clientData = '';

const getStoredClient = () => clientData;
Expand All @@ -45,7 +35,10 @@ export class LatticeProvider extends Provider {
setStoredClient,
});

return new LatticeProvider(clientData, isPaired);
return {
clientData,
isPaired,
};
}

static convertDerivationPathToArray(path: string): number[] {
Expand Down
21 changes: 3 additions & 18 deletions packages/cosmos/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
{
"extends": "tsconfig/base.json",
"compilerOptions": {
"lib": [
"esnext",
"dom"
],
"lib": ["esnext", "dom"],
"baseUrl": "./",
"outDir": "./dist",
"paths": {
"crypto": [
"node_modules/crypto-browserify",
"../../node_modules/crypto-browserify"
],
"stream": [
"node_modules/stream-browserify",
"../../node_modules/stream-browserify"
]
}
"outDir": "./dist"
},
"include": [
"./src/**/*"
]
"include": ["./src/**/*"]
}
1 change: 1 addition & 0 deletions packages/evm/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.eslintrc.cjs
jest-setup-file.ts
jest.config.ts
webpack*
4 changes: 2 additions & 2 deletions packages/evm/jest-setup-file.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import 'reflect-metadata';
import { TextEncoder } from 'util';
global.TextEncoder = TextEncoder;
import { TextEncoder, TextDecoder } from 'util';
Object.assign(global, { TextDecoder, TextEncoder });
21 changes: 21 additions & 0 deletions packages/evm/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { Config } from 'jest';

const config: Config = {
setupFiles: ['./jest-setup-file.ts'],
preset: 'ts-jest',
transform: {
'.+\\.(t|j)s$': 'ts-jest',
},
modulePathIgnorePatterns: ['<rootDir>/dist/'],
testEnvironment: 'jsdom',
watchPlugins: [
'jest-watch-typeahead/filename',
'jest-watch-typeahead/testname',
],
testTimeout: 15000,
globals: {
Uint8Array: Uint8Array,
},
};

export default config;
35 changes: 9 additions & 26 deletions packages/evm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
"@types/jest": "27.4.1",
"cross-fetch": "3.1.5",
"eslint-config-custom": "*",
"jest": "27.5.1",
"jest-environment-jsdom": "27.5.1",
"jest": "29.7.0",
"jest-environment-jsdom": "29.7.0",
"jest-watch-typeahead": "1.0.0",
"ts-jest": "27.1.4",
"ts-jest": "29.2.0",
"tsup": "6.6.3",
"typescript": "4.8.3"
},
"dependencies": {
"@ethereumjs/tx": "5.3.0",
"@ledgerhq/cryptoassets": "9.1.0",
"@ledgerhq/hw-app-eth": "6.35.4",
"@ledgerhq/hw-transport": "6.30.3",
Expand All @@ -31,12 +32,15 @@
"@xdefi-tech/chains-core": "*",
"@xdefi-tech/chains-graphql": "*",
"bignumber.js": "9.1.2",
"ethers": "5.6.4",
"http-browserify": "^1.7.0",
"https-browserify": "^1.0.0",
"lodash": "4.17.21",
"reflect-metadata": "0.1.13",
"rimraf": "4.4.0",
"rxjs": "7.8.0",
"ts-node": "10.7.0"
"stream-browserify": "3.0.0",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how many of these changes can we push down into the sdk?

"ts-node": "10.7.0",
"zlib-browserify": "^0.0.3"
},
"scripts": {
"build": "tsup --minify --clean",
Expand Down Expand Up @@ -68,26 +72,5 @@
],
"platform": "browser",
"target": "ES6"
},
"jest": {
"setupFiles": [
"./jest-setup-file.ts"
],
"preset": "ts-jest",
"transform": {
".+\\.(t|j)s$": "ts-jest"
},
"modulePathIgnorePatterns": [
"<rootDir>/dist/"
],
"moduleNameMapper": {
"axios": "axios/dist/node/axios.cjs"
},
"testEnvironment": "jsdom",
"watchPlugins": [
"jest-watch-typeahead/filename",
"jest-watch-typeahead/testname"
],
"testTimeout": 15000
}
}
2 changes: 1 addition & 1 deletion packages/evm/src/signers/lattice.signer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('lattice.signer', () => {

beforeEach(async () => {
provider = new EvmProvider(new IndexerDataSource(EVM_MANIFESTS.ethereum));
derivationPath = "m/84'/0'/0'/0/0";
derivationPath = "m/44'/60'/0'/0/0";

txInput = {
from: '0xdfb2682febe6ea96682b1018702958980449b7db',
Expand Down
49 changes: 40 additions & 9 deletions packages/evm/src/signers/lattice.signer.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
import { Signer, SignerDecorator } from '@xdefi-tech/chains-core';
import { fetchAddresses, sign, Constants } from 'gridplus-sdk';
import { type UnsignedTransaction, utils } from 'ethers';
import { type UnsignedTransaction, utils, BigNumber } from 'ethers';

import type { ChainMsg, TxData } from '../msg';

@SignerDecorator(Signer.SignerType.LATTICE)
export class LatticeSigner extends Signer.LatticeProvider {
static async create({
deviceId,
password,
name,
}: {
deviceId: string;
password: string;
name: string;
}): Promise<LatticeSigner> {
const { clientData, isPaired } = await super.init({
deviceId,
password,
name,
});
return new LatticeSigner(clientData, isPaired);
}

verifyAddress(address: string): boolean {
return utils.isAddress(address);
}

async getAddress(derivation: string): Promise<string> {
const startPath =
Signer.LatticeProvider.convertDerivationPathToArray(derivation);
const addresses = await fetchAddresses({
startPath:
Signer.LatticeProvider.convertDerivationPathToArray(derivation),
startPath,
n: 1,
});
return addresses[0];
Expand All @@ -23,18 +41,31 @@ export class LatticeSigner extends Signer.LatticeProvider {
const baseTx = await msg.buildTx();
const txData = convertTransactionToEthData(baseTx, derivation);
const res = await sign([], txData);
msg.sign(res.txHash);
const r = utils.hexlify(res.sig.r);
const s = utils.hexlify(res.sig.s);
const v = BigNumber.from(utils.hexlify(res.sig.v)).toNumber();
const tx = toEthersTx(baseTx);
const signedTx = utils.serializeTransaction(tx, {
r,
s,
v,
});
msg.sign(signedTx);
}
}

function toEthersTx(tx: TxData): UnsignedTransaction {
return {
...tx,
nonce: Number.parseInt(tx.nonce),
chainId: Number.parseInt(tx.chainId),
};
}

function convertTransactionToEthData(transaction: TxData, derivation: string) {
const signerPath =
Signer.LatticeProvider.convertDerivationPathToArray(derivation);
const tx: UnsignedTransaction = {
...transaction,
nonce: Number.parseInt(transaction.nonce),
chainId: Number.parseInt(transaction.chainId),
};
const tx = toEthersTx(transaction);
const serializedTx = utils.serializeTransaction(tx);
const payload = utils.arrayify(serializedTx);
return {
Expand Down
9 changes: 8 additions & 1 deletion packages/evm/src/signers/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@ import { LedgerSigner } from './ledger.signer';
import { PrivateKeySigner } from './private-key.signer';
import { SeedPhraseSigner } from './seed-phrase.signer';
import { TrezorSigner } from './trezor.signer';
import { LatticeSigner } from './lattice.signer';

export { LedgerSigner, PrivateKeySigner, SeedPhraseSigner, TrezorSigner };
export {
LedgerSigner,
PrivateKeySigner,
SeedPhraseSigner,
TrezorSigner,
LatticeSigner,
};
2 changes: 1 addition & 1 deletion packages/evm/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"outDir": "./dist"
},
"allowJs": true,
"include": ["./src/custom.d.ts", "./src/**/*"],
"include": ["./src/custom.d.ts", "./src/**/*"]
}
Loading