Initial types

This commit is contained in:
HenryNguyen5 2018-04-27 16:54:46 -04:00
parent a67f18122f
commit e31648b7b2
5 changed files with 311 additions and 0 deletions

View File

@ -0,0 +1,56 @@
import LedgerTransport from 'ledgerhq__hw-transport';
declare module 'ledgerhq__hw-app-eth' {
export default class Eth<T extends LedgerTransport<any>> {
constructor(transport: T);
/**
*
* @description get Ethereum address for a given BIP 32 path.
* @param {string} path a path in BIP 32 format
* @param {boolean} [boolDisplay] enable or not the display
* @param {boolean} [boolChaincode] enable or not the chaincode request
* @returns {Promise<{ publicKey: string; address: string; chainCode?: string }>}
* @memberof Eth
*/
public getAddress(
path: string,
boolDisplay?: boolean,
boolChaincode?: boolean
): Promise<{ publicKey: string; address: string; chainCode?: string }>;
/**
*
* @description signs a raw transaction and returns v,r,s
* @param {string} path
* @param {string} rawTxHex
* @returns {Promise<{s: string, v: string, r: string}>}
* @memberof Eth
*/
public signTransaction(
path: string,
rawTxHex: string
): Promise<{ s: string; v: string; r: string }>;
/**
*
*
* @returns {Promise<{ arbitraryDataEnabled: number; version: string }>}
* @memberof Eth
*/
public getAppConfiguration(): Promise<{ arbitraryDataEnabled: number; version: string }>;
/**
*
* @description sign a message according to eth_sign RPC call
* @param {string} path
* @param {string} messageHex
* @returns {Promise<{v: number, s: string, r: string}>}
* @memberof Eth
*/
public signPersonalMessage(
path: string,
messageHex: string
): Promise<{ v: number; s: string; r: string }>;
}
}

View File

@ -0,0 +1,37 @@
import LedgerTransport, { Observer, DescriptorEvent } from 'ledgerhq__hw-transport';
import { HID, Device } from 'node-hid';
type ObserverEvents = {} | {} | {};
declare module 'ledgerhq__hw-transport-node-hid' {
export default class TransportNodeHid extends LedgerTransport<string> {
/**
* Creates an instance of TransportNodeHid.
* @param {HID} device
* @param {boolean} [ledgerTransport]
* @param {number} [timeout]
* @param {boolean} [debug]
* @memberof TransportNodeHid
*/
constructor(device: HID, ledgerTransport?: boolean, timeout?: number, debug?: boolean);
/**
*
* @description Check if an HID instance is active
* @static
* @returns {Promise<boolean>}
* @memberof TransportNodeHid
*/
public static isSupported(): Promise<boolean>;
/**
*
* @description Lists all available HID device's paths
* @static
* @returns {Promise<string[]>}
* @memberof TransportNodeHid
*/
public static list(): Promise<Device['path'][]>;
public static listen(observer: Observer<DescriptorEvent<string, HID>, Device['path']>);
}
}

View File

@ -0,0 +1,208 @@
declare module 'ledgerhq__hw-transport' {
export interface Subscription {
unsubscribe: () => void;
}
export interface Observer<Ev, NextRet, ErrParam, ErrRet, CompleteRet> {
next: (event: Ev) => NextRet;
error: (e: ErrParam) => ErrRet;
complete: () => CompleteRet;
}
export interface DescriptorEvent<Descriptor, Device> {
type: 'add' | 'remove';
descriptor: Descriptor;
device?: Device;
}
export type FunctionPropertyNames<T> = {
[K in keyof T]: T[K] extends Function ? K : never
}[keyof T];
export type ExtractPromise<T> = T extends Promise<infer U> ? U : T;
export default abstract class LedgerTransport<Descriptor> {
/**
*
* @description Check if a transport is supported on the user's platform/browser.
* @static
* @returns {Promise<boolean>}
* @memberof LedgerTransport
*/
public static isSupported(): Promise<boolean>;
/**
*
* @description List once all available descriptors. For a better granularity, checkout listen().
* @static
* @template Descriptor
* @returns {Promise<Descriptor[]>}
* @memberof LedgerTransport
*/
public static list<Descriptor>(): Promise<Descriptor[]>;
/**
*
* @description Listen all device events for a given Transport.
* The method takes an Observer of DescriptorEvent and returns a Subscription
* according to Observable paradigm https://github.com/tc39/proposal-observable
* a DescriptorEvent is a { descriptor, type } object.
* Type can be "add" or "remove" and descriptor is a value you can pass to open(descriptor).
* Each listen() call will first emit all potential device already connected and then will emit events can come over times,
* for instance if you plug a USB device after listen() or a bluetooth device become discoverable.
* @static
* @template Ev
* @template NextRet
* @template ErrParam
* @template ErrRet
* @template CompleteRet
* @param {Observer<Ev, NextRet, ErrParam, ErrRet, CompleteRet>} observer
* @returns {Subscription}
* @memberof LedgerTransport
*/
public static listen<
Descriptor,
Device = any,
NextRet = any,
ErrParam = any,
ErrRet = any,
CompleteRet = any
>(
observer: Observer<
DescriptorEvent<Descriptor, Device>,
NextRet,
ErrParam,
ErrRet,
CompleteRet
>
): Subscription;
/**
*
* @description Attempt to create a Transport instance with potentially a descriptor.
* @static
* @template Descriptor
* @param {Descriptor} descriptor
* @param {number} [timeout]
* @returns {Promise<LedgerTransport<Descriptor>>}
* @memberof LedgerTransport
*/
public static open<Descriptor>(
descriptor: Descriptor,
timeout?: number
): Promise<LedgerTransport<Descriptor>>;
/**
*
* @description create() attempts open the first descriptor available or throw if:
* - there is no descriptor
* - if either timeout is reached
*
* This is a light alternative to using listen() and open() that you may need for any advanced usecases
* @static
* @template Descriptor
* @param {number} [openTimeout]
* @param {number} [listenTimeout]
* @returns {Promise<LedgerTransport<Descriptor>>}
* @memberof LedgerTransport
*/
public static create<Descriptor>(
openTimeout?: number,
listenTimeout?: number
): Promise<LedgerTransport<Descriptor>>;
/**
*
* @description Low level api to communicate with the device.
* This method is for implementations to implement but should not be directly called.
* Instead, the recommended way is to use send() method
* @param {Buffer} apdu
* @returns {Promise<Buffer>}
* @memberof LedgerTransport
*/
public abstract exchange(apdu: Buffer): Promise<Buffer>;
/**
*
* @description Set the "scramble key" for the next exchange with the device.
* Each App can have a different scramble key and they internally will set it at instantiation.
* @param {string} scrambleKey
* @memberof LedgerTransport
*/
public setScrambleKey(scrambleKey: string): void;
/**
*
* @description Close the exchange with the device.
* @returns {Promise<void>}
* @memberof LedgerTransport
*/
public close(): Promise<void>;
/**
*
* @description Listen to an event on an instance of transport.
* Transport implementation can have specific events. Here are the common events:
* - "disconnect" : triggered if Transport is disconnected
* @param {string} eventName
* @param {Listener} cb
* @memberof LedgerTransport
*/
public on(eventName: string | 'listen', cb: (...args: any[]) => any): void;
/**
*
* @description Stop listening to an event on an instance of transport.
* @param {string} eventName
* @param {Listener} cb
* @memberof LedgerTransport
*/
public off(eventName: string, cb: (...args: any[]) => any): void;
/**
*
* @description Toggle logs of binary exchange
* @param {boolean} debug
* @memberof LedgerTransport
*/
public setDebugMode(debug: boolean): void;
/**
* @description Set a timeout (in milliseconds) for the exchange call.
* Only some transport might implement it. (e.g. U2F)
* @param {number} exchangeTimeout
* @memberof LedgerTransport
*/
public setExchangeTimeout?(exchangeTimeout: number): void;
/**
* @description Used to decorate all callable public methods of an app so that they
* are mutually exclusive. Scramble key is application specific, e.g hw-app-eth will set
* its own scramblekey
* @param self
* @param methods
* @param scrambleKey
*/
public decorateAppAPIMethods<T>(
self: T,
methods: FunctionPropertyNames<T>[],
scrambleKey: string
): void;
/**
* @description Decorates a function so that it uses a global mutex, if an
* exchange is already in process, then calling the function will throw an
* error about being locked
* @param methodName
* @param functionToDecorate
* @param thisContext
* @param scrambleKey
*/
public decorateAppAPIMethod<T, FArgs = any, FRet = any>(
methodName: FunctionPropertyNames<T>,
functionToDecorate: (...args: FArgs[]) => FRet,
thisContext: T,
scrambleKey: string
): (...args: FArgs[]) => Promise<ExtractPromise<FRet>>; // make sure we dont wrap promises twice
}
}

View File

@ -62,10 +62,12 @@
"@types/classnames": "2.2.3",
"@types/enzyme": "3.1.8",
"@types/enzyme-adapter-react-16": "1.0.1",
"@types/events": "^1.2.0",
"@types/history": "4.6.2",
"@types/jest": "22.2.3",
"@types/lodash": "4.14.107",
"@types/moment-timezone": "0.5.4",
"@types/node-hid": "^0.7.0",
"@types/qrcode": "0.8.0",
"@types/qrcode.react": "0.6.3",
"@types/query-string": "5.1.0",

View File

@ -88,6 +88,10 @@
"@types/cheerio" "*"
"@types/react" "*"
"@types/events@^1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@types/events/-/events-1.2.0.tgz#81a6731ce4df43619e5c8c945383b3e62a89ea86"
"@types/history@*", "@types/history@4.6.2":
version "4.6.2"
resolved "https://registry.yarnpkg.com/@types/history/-/history-4.6.2.tgz#12cfaba693ba20f114ed5765467ff25fdf67ddb0"
@ -106,6 +110,10 @@
dependencies:
moment ">=2.14.0"
"@types/node-hid@^0.7.0":
version "0.7.0"
resolved "https://registry.yarnpkg.com/@types/node-hid/-/node-hid-0.7.0.tgz#f696f39c528059116236e41df90c8fcba077d711"
"@types/node@*", "@types/node@^9.6.2":
version "9.6.5"
resolved "https://registry.yarnpkg.com/@types/node/-/node-9.6.5.tgz#ee700810fdf49ac1c399fc5980b7559b3e5a381d"