2017-10-19 02:29:49 +00:00
|
|
|
import Big from 'bignumber.js';
|
2017-08-28 17:43:57 +00:00
|
|
|
import EthTx from 'ethereumjs-tx';
|
|
|
|
import { addHexPrefix } from 'ethereumjs-util';
|
2017-09-25 02:06:28 +00:00
|
|
|
import { RawTransaction } from 'libs/transaction';
|
2017-09-15 19:29:38 +00:00
|
|
|
import { stripHexPrefixAndLower } from 'libs/values';
|
2017-09-25 02:06:28 +00:00
|
|
|
import TrezorConnect from 'vendor/trezor-connect';
|
2017-11-08 18:16:43 +00:00
|
|
|
import { DeterministicWallet } from './deterministic';
|
|
|
|
import { IWallet } from '../IWallet';
|
|
|
|
export class TrezorWallet extends DeterministicWallet implements IWallet {
|
2017-09-25 02:06:28 +00:00
|
|
|
public signRawTransaction(tx: RawTransaction): Promise<string> {
|
2017-08-28 17:43:57 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-09-25 02:06:28 +00:00
|
|
|
(TrezorConnect as any).ethereumSignTx(
|
2017-08-28 17:43:57 +00:00
|
|
|
// Args
|
|
|
|
this.getPath(),
|
2017-09-15 19:29:38 +00:00
|
|
|
// stripHexPrefixAndLower identical to ethFuncs.getNakedAddress
|
|
|
|
stripHexPrefixAndLower(tx.nonce),
|
|
|
|
stripHexPrefixAndLower(tx.gasPrice.toString()),
|
|
|
|
stripHexPrefixAndLower(tx.gasLimit.toString()),
|
|
|
|
stripHexPrefixAndLower(tx.to),
|
|
|
|
stripHexPrefixAndLower(tx.value),
|
|
|
|
stripHexPrefixAndLower(tx.data),
|
2017-08-28 17:43:57 +00:00
|
|
|
tx.chainId,
|
|
|
|
// Callback
|
|
|
|
result => {
|
|
|
|
if (!result.success) {
|
|
|
|
return reject(new Error(result.error));
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Explain what's going on here? Add tests? Adapted from:
|
|
|
|
// https://github.com/kvhnuke/etherwallet/blob/v3.10.2.6/app/scripts/uiFuncs.js#L24
|
|
|
|
const txToSerialize = {
|
|
|
|
...tx,
|
|
|
|
v: addHexPrefix(new Big(result.v).toString(16)),
|
|
|
|
r: addHexPrefix(result.r),
|
|
|
|
s: addHexPrefix(result.s)
|
|
|
|
};
|
|
|
|
const eTx = new EthTx(txToSerialize);
|
|
|
|
const signedTx = addHexPrefix(eTx.serialize().toString('hex'));
|
|
|
|
resolve(signedTx);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2017-10-30 19:10:25 +00:00
|
|
|
|
|
|
|
public signMessage = () =>
|
|
|
|
Promise.reject(new Error('Signing via Trezor not yet supported.'));
|
|
|
|
|
|
|
|
// works, but returns a signature that can only be verified with a Trezor device
|
|
|
|
/*
|
|
|
|
public signMessage = (message: string): Promise<string> => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
(TrezorConnect as any).ethereumSignMessage(
|
|
|
|
this.getPath(),
|
|
|
|
message,
|
|
|
|
response => {
|
|
|
|
if (response.success) {
|
|
|
|
resolve(addHexPrefix(response.signature))
|
|
|
|
} else{
|
|
|
|
console.error(response.error)
|
|
|
|
reject(response.error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
*/
|
2017-08-28 17:43:57 +00:00
|
|
|
}
|