Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
{},
{
/*
* We could really do with some BN.js flow types declarations :(
*/
gasPrice: hexSequenceNormalizer(
/*
* @TODO Add `bigNumber` `toHexString` wrapper method
*
* Flow confuses bigNumber's `toString` with the String object
* prototype `toString` method
*/
/* $FlowFixMe */
multipleOfTwoHexValueNormalizer(gasPrice.toString(16)),
),
gasLimit: hexSequenceNormalizer(
/*
* @TODO Add `bigNumber` `toHexString` wrapper method
*
* Flow confuses bigNumber's `toString` with the String object
* prototype `toString` method
*/
/* $FlowFixMe */
multipleOfTwoHexValueNormalizer(gasLimit.toString(16)),
),
/*
* Nonces needs to be sent in as a hex string, and to be padded as a multiple of two.
* Eg: '3' to be '03', `12c` to be `012c`
*/
nonce: hexSequenceNormalizer(
/*
* @TODO Add `bigNumber` `toHexString` wrapper method
* prototype `toString` method
*/
/* $FlowFixMe */
nonce: multipleOfTwoHexValueNormalizer(nonce.toString(16)),
/*
* @TODO Add `bigNumber` `toHexString` wrapper method
*
* Flow confuses bigNumber's `toString` with the String object
* prototype `toString` method
*/
/* $FlowFixMe */
value: multipleOfTwoHexValueNormalizer(value.toString(16)),
/*
* Trezor service requires the prefix from the input data to be stripped
*/
data: hexSequenceNormalizer(inputData, false),
},
/*
* Only send (and normalize) the destingation address if one was
* provided in the initial transaction object.
*
* Trezor service requires the prefix from the address to be stripped
*/
to ? { to: addressNormalizer(to, false) } : {},
);
/*
* We need to catch the cancelled error since it's part of a normal user workflow
*/
try {
/*
* See fundamentals of Elliptic Curve Digital Signature Algorithm (ECDSA) to
* get an general idea of where the three components come from:
*/
/* $FlowFixMe */
multipleOfTwoHexValueNormalizer(value.toString(16)),
),
data: hexSequenceNormalizer(inputData),
/*
* The transaction object needs to be seeded with the (R) and (S) signature components with
* empty data, and the Reco(V)ery param as the chain id (all, im hex string format).
*
* See this issue for context:
* https://github.com/LedgerHQ/ledgerjs/issues/43
*/
r: hexSequenceNormalizer(
multipleOfTwoHexValueNormalizer(String(SIGNATURE.R)),
),
s: hexSequenceNormalizer(
multipleOfTwoHexValueNormalizer(String(SIGNATURE.S)),
),
v: hexSequenceNormalizer(
/*
* @TODO Add `bigNumber` `toHexString` wrapper method
*
* Flow confuses bigNumber's `toString` with the String object
* prototype `toString` method
*/
/* $FlowFixMe */
multipleOfTwoHexValueNormalizer(chainId.toString(16)),
),
},
to ? { to: addressNormalizer(to) } : {},
),
getChainDefinition(chainId),
),
gasLimit: hexSequenceNormalizer(
/*
* @TODO Add `bigNumber` `toHexString` wrapper method
*
* Flow confuses bigNumber's `toString` with the String object
* prototype `toString` method
*/
/* $FlowFixMe */
multipleOfTwoHexValueNormalizer(gasLimit.toString(16)),
),
/*
* Nonces needs to be sent in as a hex string, and to be padded as a multiple of two.
* Eg: '3' to be '03', `12c` to be `012c`
*/
nonce: hexSequenceNormalizer(
/*
* @TODO Add `bigNumber` `toHexString` wrapper method
*
* Flow confuses bigNumber's `toString` with the String object
* prototype `toString` method
*/
/* $FlowFixMe */
multipleOfTwoHexValueNormalizer(nonce.toString(16)),
),
value: hexSequenceNormalizer(
/*
* @TODO Add `bigNumber` `toHexString` wrapper method
*
* Flow confuses bigNumber's `toString` with the String object
* prototype `toString` method
*/
* We also normalize it first (but for some reason Flow doesn't pick up
* the default value value of `path` and assumes it's undefined -- it can be,
* but it will not pass the validator)
*/
path: fromString(
derivationPathNormalizer(derivationPath),
true,
).toPathArray(),
// $FlowFixMe need Buffer types
message: Buffer.from(toSign).toString(HEX_HASH_TYPE),
}),
});
/*
* Add the hex `0x` prefix
*/
return hexSequenceNormalizer(signedMessage);
} catch (caughtError) {
/*
* If the user cancels signing the message we still throw,
* but we customize the message
*/
if (caughtError.message === STD_ERRORS.CANCEL_TX_SIGN) {
throw new Error(messages.userSignTxCancel);
}
/*
* But throw otherwise, so we can see what's going on
*/
throw new Error(
`${messages.userSignTxGenericError}: message: (${message}) ${
caughtError.message
}`,
);
message,
messageData,
}: Object = {}): Promise => {
/*
* Validate input values: derivationPath and message
*/
derivationPathValidator(derivationPath);
const toSign = messageOrDataValidator({ message, messageData });
try {
const ledger: LedgerInstanceType = await ledgerConnection();
/*
* Sign the message object via your Ledger Wallet
*
* We also warn the user here, since the device will need confirmation, but only in dev mode.
*/
warning(messages.userSignMessageInteractionWarning);
const {
r: rSignatureComponent,
s: sSignatureComponent,
v: recoveryParameter,
/*
* Flow bugs out here claiming the `r` property is not available on the return object.
*/
/* $FlowFixMe */
} = await ledger.signPersonalMessage(
derivationPathNormalizer(derivationPath),
/*
* The message needs to be sent in as an hex string
*
* Also, Flow don't know about Buffer
*/
/* $FlowFixMe */
* prototype `toString` method
*/
/* $FlowFixMe */
multipleOfTwoHexValueNormalizer(chainId.toString(16)),
),
},
to ? { to: addressNormalizer(to) } : {},
),
getChainDefinition(chainId),
);
/*
* Sign the transaction object via your Ledger Wallet
*
* We also warn the user here, since the device will need confirmation, but only in dev mode.
*/
warning(messages.userSignInteractionWarning);
const {
r: rSignatureComponent,
s: sSignatureComponent,
v: recoveryParameter,
} = await ledger.signTransaction(
derivationPathNormalizer(derivationPath),
unsignedTransaction.serialize().toString(HEX_HASH_TYPE),
);
/*
* Proving that we signed the above transaction.
*
* @NOTE We need to modify the original transaction
* Otherwise EthereumTx will complain because internally it checks for the valid instance
*/
unsignedTransaction.r = hexSequenceNormalizer(rSignatureComponent);
unsignedTransaction.s = hexSequenceNormalizer(sSignatureComponent);
),
s: hexSequenceNormalizer(
multipleOfTwoHexValueNormalizer(String(SIGNATURE.S)),
),
v: hexSequenceNormalizer(
/*
* @TODO Add `bigNumber` `toHexString` wrapper method
*
* Flow confuses bigNumber's `toString` with the String object
* prototype `toString` method
*/
/* $FlowFixMe */
multipleOfTwoHexValueNormalizer(chainId.toString(16)),
),
},
to ? { to: addressNormalizer(to) } : {},
),
getChainDefinition(chainId),
);
/*
* Sign the transaction object via your Ledger Wallet
*
* We also warn the user here, since the device will need confirmation, but only in dev mode.
*/
warning(messages.userSignInteractionWarning);
const {
r: rSignatureComponent,
s: sSignatureComponent,
v: recoveryParameter,
} = await ledger.signTransaction(
derivationPathNormalizer(derivationPath),
unsignedTransaction.serialize().toString(HEX_HASH_TYPE),
* prototype `toString` method
*/
/* $FlowFixMe */
value: multipleOfTwoHexValueNormalizer(value.toString(16)),
/*
* Trezor service requires the prefix from the input data to be stripped
*/
data: hexSequenceNormalizer(inputData, false),
},
/*
* Only send (and normalize) the destingation address if one was
* provided in the initial transaction object.
*
* Trezor service requires the prefix from the address to be stripped
*/
to ? { to: addressNormalizer(to, false) } : {},
);
/*
* We need to catch the cancelled error since it's part of a normal user workflow
*/
try {
/*
* See fundamentals of Elliptic Curve Digital Signature Algorithm (ECDSA) to
* get an general idea of where the three components come from:
* https://en.wikipedia.org/wiki/Elliptic_Curve_Digital_Signature_Algorithm
*
* Also, see EIP-155 for the 27 and 28 magic numbers expected in the recovery
* parameter:
* https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md
*
* Now, trezor will give you the recovery paramenter already encoded, but if you
* want to derive the magic numbers again:
{
/*
* Path needs to be sent in as an derivation path array
*/
address_n: fromString(
derivationPathNormalizer(derivationPath),
true,
).toPathArray(),
/*
* @TODO Add `bigNumber` `toHexString` wrapper method
*
* Flow confuses bigNumber's `toString` with the String object
* prototype `toString` method
*/
/* $FlowFixMe */
gas_price: multipleOfTwoHexValueNormalizer(gasPrice.toString(16)),
/*
* @TODO Add `bigNumber` `toHexString` wrapper method
*
* Flow confuses bigNumber's `toString` with the String object
* prototype `toString` method
*/
/* $FlowFixMe */
gas_limit: multipleOfTwoHexValueNormalizer(gasLimit.toString(16)),
chain_id: chainId,
/*
* Nonces needs to be sent in as a hex string, and to be padded as a multiple of two.
* Eg: '3' to be '03', `12c` to be `012c`
*
* @TODO Add `bigNumber` `toHexString` wrapper method
*
* Flow confuses bigNumber's `toString` with the String object