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
}`,
);
* but we customize the message
*/
if (error.message.includes(STD_ERRORS.CANCEL_MSG_SIGN)) {
throw new Error(messages.cancelMessageSign);
}
throw new Error(error.message);
}
/*
* Validate that the signature is in the correct format
*/
hexSequenceValidator(messageSignature);
/*
* Add the `0x` prefix to the message's signature
*/
const normalizedSignature: string = hexSequenceNormalizer(
messageSignature,
);
return resolve(normalizedSignature);
} catch (caughtError) {
return reject(caughtError);
}
};
(error: Error, signature: string) => {
try {
/*
* Validate that the signature is in the correct format
*/
hexSequenceValidator(signature);
const recoveredPublicKey: string = recoverPublicKeyHelper({
message: PUBLICKEY_RECOVERY_MESSAGE,
signature,
});
/*
* Add the `0x` prefix to the recovered public key
*/
const normalizedPublicKey: string = hexSequenceNormalizer(
recoveredPublicKey,
);
/*
* Also set the internal public key
*/
internalPublicKey = normalizedPublicKey;
return resolve(normalizedPublicKey);
} catch (caughtError) {
/*
* Don't throw an Error if the user just cancels signing the message.
* This is normal UX, not an exception
*/
if (error.message.includes(STD_ERRORS.CANCEL_MSG_SIGN)) {
return warning(staticMethodsMessages.cancelMessageSign);
}
throw new Error(error.message);
return (async () => {
const privateKey: string = await this.privateKey;
const reversedPublicKey: string = privateToPublic(privateKey).toString(
HEX_HASH_TYPE,
);
/*
* Validate the reversed public key
*/
hexSequenceValidator(reversedPublicKey);
/*
* Then normalize it to ensure it has the `0x` prefix
*/
const normalizedPublicKey: string = hexSequenceNormalizer(
reversedPublicKey,
);
/*
* Memoizing the getter
*
* While this is not an expensive operation, it's still a good idea
* to memoize it so it returns a tiny bit faster.
*/
Object.defineProperty(
(this: any),
'publicKey',
Object.assign({}, GETTERS, {
value: Promise.resolve(normalizedPublicKey),
}),
);
return normalizedPublicKey;
*
* recoveryParam - 35 - (chainId * 2)
*
* If the result is odd, then V is 27, if it's even, it's 28
*/
const {
r: rSignatureComponent,
s: sSignatureComponent,
v: recoveryParameter,
}: Object = await payloadListener({ payload: modifiedPayloadObject });
/*
* Add the signature values to the unsigned trasaction
*/
unsignedTransaction.r = hexSequenceNormalizer(rSignatureComponent);
unsignedTransaction.s = hexSequenceNormalizer(sSignatureComponent);
unsignedTransaction.v = hexSequenceNormalizer(
bigNumber(recoveryParameter).toString(16),
);
return hexSequenceNormalizer(
unsignedTransaction.serialize().toString(HEX_HASH_TYPE),
);
} catch (caughtError) {
/*
* If the user cancels signing the transaction 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
*/
data: signedData,
gasLimit: new BigNumber(gas),
gasPrice: new BigNumber(signedGasPrice),
nonce: new BigNumber(nonce),
r,
s,
to: signedTo,
v,
value: new BigNumber(signedValue),
},
getChainDefinition(chainId),
);
const serializedSignedTransaction = signedTransaction
.serialize()
.toString(HEX_HASH_TYPE);
const normalizedSignedTransaction = hexSequenceNormalizer(
serializedSignedTransaction,
);
return resolve(normalizedSignedTransaction);
} catch (caughtError) {
return reject(caughtError);
}
};