Skip to content

Commit

Permalink
feat: change api
Browse files Browse the repository at this point in the history
  • Loading branch information
vivalaakam committed Jan 30, 2023
1 parent d4d2439 commit 85d093c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 26 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@haqq/provider-ledger-react-native",
"version": "0.0.11",
"version": "0.0.12",
"description": "Provider for react-native ledger",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -30,7 +30,7 @@
},
"devDependencies": {
"@types/react-native": "^0.70.6",
"@haqq/provider-base": "^0.0.7",
"@haqq/provider-base": "^0.0.10",
"@types/jest": "^29.2.5",
"@types/node": "^18.11.18",
"jest": "^29.3.1",
Expand Down
33 changes: 14 additions & 19 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from '@haqq/provider-base';
import AppEth, {ledgerService} from '@ledgerhq/hw-app-eth';
import TransportBLE from '@ledgerhq/react-native-hw-transport-ble';
import {UnsignedTransaction, utils} from 'ethers';
import {utils, UnsignedTransaction} from 'ethers';
import {suggestApp} from './commands/suggest-app';
import {getDeviceConnection} from './get-device-connection';
import {sleep} from './sleep';
Expand All @@ -16,6 +16,11 @@ export class ProviderLedgerReactNative extends Provider<ProviderLedgerReactNativ
public stop: boolean = false;
private _transport: TransportBLE | null = null

async getEthAddress(hdPath: string): Promise<string> {
const {address} = await this.getPublicKeyAndAddressForHDPath(hdPath)
return address
}

async getPublicKeyAndAddressForHDPath(hdPath: string) {
let resp = {publicKey: '', address: ''}
try {
Expand Down Expand Up @@ -48,21 +53,12 @@ export class ProviderLedgerReactNative extends Provider<ProviderLedgerReactNativ
return resp
}

async getBase64PublicKey() {
let resp = ''
try {
const {publicKey} = await this.getPublicKeyAndAddressForHDPath(this._options.hdPath)
resp = Buffer.from(publicKey, 'hex').toString('base64');
this.emit('getBase64PublicKey', true);
} catch (e) {
if (e instanceof Error) {
this.catchError(e, 'getBase64PublicKey');
}
}
return resp
async getPublicKey(hdPath: string) {
const {publicKey} = await this.getPublicKeyAndAddressForHDPath(hdPath)
return publicKey
}

async getSignedTx(transaction: TransactionRequest) {
async getSignedTx(hdPath: string, transaction: TransactionRequest) {
let resp = ''
try {
this.stop = false;
Expand All @@ -87,7 +83,7 @@ export class ProviderLedgerReactNative extends Provider<ProviderLedgerReactNativ

const eth = new AppEth(transport);

const signature = await eth.signTransaction(this._options.hdPath, unsignedTx, resolution);
const signature = await eth.signTransaction(hdPath, unsignedTx, resolution);

resp = utils.serializeTransaction(transaction as UnsignedTransaction, {
...signature,
Expand All @@ -106,7 +102,7 @@ export class ProviderLedgerReactNative extends Provider<ProviderLedgerReactNativ
return resp
}

async signTypedData(domainHash: string, valuesHash: string) {
async signTypedData(hdPath: string, domainHash: string, valuesHash: string) {
let resp = ''
try {
this.stop = false;
Expand All @@ -123,7 +119,7 @@ export class ProviderLedgerReactNative extends Provider<ProviderLedgerReactNativ

const eth = new AppEth(transport);

const signature = await eth.signEIP712HashedMessage(this._options.hdPath, domainHash, valuesHash);
const signature = await eth.signEIP712HashedMessage(hdPath, domainHash, valuesHash);

const v = (signature.v - 27).toString(16).padStart(2, '0');
resp = '0x' + signature.r + signature.s + v;
Expand Down Expand Up @@ -215,8 +211,7 @@ export class ProviderLedgerReactNative extends Provider<ProviderLedgerReactNativ
}
break;
default:
this.emit(source, false, e.message, e.name);
throw new Error(e.message);
super.catchError(e, source);
break;
}
}
Expand Down
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,5 @@ export enum State {

export type ProviderLedgerReactNativeOptions = {
deviceId: string;
hdPath: string;
appName?: string;
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1115,10 +1115,10 @@
dependencies:
"@hapi/hoek" "^9.0.0"

"@haqq/provider-base@^0.0.7":
version "0.0.7"
resolved "https://registry.yarnpkg.com/@haqq/provider-base/-/provider-base-0.0.7.tgz#884155e4e661f849f364df93c361009b6d0a59e5"
integrity sha512-mfH3peWrlBDcpj+eKHyRmkWDKjI878Ml2XdTKX+zhjKwwRH9LLetq4uXi+weAcJ5bW70qtxwR4XLY/rX49Qj8g==
"@haqq/provider-base@^0.0.10":
version "0.0.10"
resolved "https://registry.yarnpkg.com/@haqq/provider-base/-/provider-base-0.0.10.tgz#5785c0fb46badee94844253fdb865ecbae07a2ec"
integrity sha512-rF555m98H1JIiSZt1OC9bVz6qBooYAnAQiATbB2SkHj0/Mrgnm+j8/dv8v71XPEVozG/YMjLBlRWagwtAPxnjA==
dependencies:
"@ethersproject/abstract-provider" "^5.7.0"
bech32-converting "^1.0.9"
Expand Down

0 comments on commit 85d093c

Please sign in to comment.