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 1 commit
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
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"name": "@xdefi-tech/chains",
"version": "1.0.0",
"workspaces": ["utility-packages/*", "packages/*", "examples/*"],
"workspaces": [
"utility-packages/*",
"packages/*",
"examples/*"
],
"scripts": {
"prepare": "husky install",
"build": "turbo run build",
Expand Down
1 change: 1 addition & 0 deletions packages/bitcoin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,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.create({
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/**/*"]
}
9 changes: 5 additions & 4 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 @@ -29,7 +27,7 @@ export class LatticeProvider extends Provider {
deviceId: string;
password: string;
name: string;
}): Promise<LatticeProvider> {
}): Promise<any> {

Choose a reason for hiding this comment

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

can we type this?

let clientData = '';

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

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

static convertDerivationPathToArray(path: string): number[] {
Expand Down
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
50 changes: 41 additions & 9 deletions packages/evm/src/signers/lattice.signer.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
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 { TransactionFactory } from '@ethereumjs/tx';

Choose a reason for hiding this comment

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

👀


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.create({
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 +42,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,
};
22 changes: 20 additions & 2 deletions packages/evm/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,26 @@
"compilerOptions": {
"lib": ["esnext", "dom"],
"baseUrl": "./",
"outDir": "./dist"
"outDir": "./dist",
"paths": {
"stream": [
"node_modules/stream-browserify",
"../../node_modules/stream-browserify"
],
"http": [
"node_modules/http-browserify",
"../../node_modules/http-browserify"
],
"https": [
"node_modules/https-browserify",
"../../node_modules/https-browserify"
],
"zlib": [
"node_modules/zlib-browserify",
"../../node_modules/zlib-browserify"
]
}
},
"allowJs": true,
"include": ["./src/custom.d.ts", "./src/**/*"],
"include": ["./src/custom.d.ts", "./src/**/*"]
}
Loading