Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
module.exports = async (efx, order) => {
const orderHash = orderHashUtils.getOrderHashHex(order)
const provider = efx.isMetaMask
? new MetamaskSubprovider(efx.web3.currentProvider)
: efx.web3.currentProvider
const signature = await signatureUtils.ecSignHashAsync(
provider,
orderHash,
efx.get('account')
)
// create a copy of the object and add signature field
order = {
...order,
signature
}
/**
const isValid = signatureUtils.isValidSignatureAsync(orderHash, signedOrder, efx.get('account').toLowerCase())
console.log( "is_valid ->", isValid)
**/
export async function signOrder(
provider: Provider,
zeroExOrder: Order
): Promise {
const orderHash = orderHashUtils.getOrderHashHex({
...zeroExOrder,
expirationTimeSeconds: new BigNumber(zeroExOrder.expirationTimeSeconds),
makerFee: new BigNumber(zeroExOrder.makerFee),
makerAssetAmount: new BigNumber(zeroExOrder.makerAssetAmount),
salt: new BigNumber(zeroExOrder.salt),
takerFee: new BigNumber(zeroExOrder.takerFee),
takerAssetAmount: new BigNumber(zeroExOrder.takerAssetAmount)
});
const signature = await signatureUtils.ecSignHashAsync(
provider,
orderHash,
zeroExOrder.makerAddress
);
// Append signature to order
const signedOrder = {
...zeroExOrder,
signature
};
return signedOrder;
}
public async signOrderHashAsync(orderHash: string): Promise {
utils.assert(!_.isUndefined(this._contractWrappers), 'ContractWrappers must be instantiated.');
const makerAddress = this._userAddressIfExists;
// If makerAddress is undefined, this means they have a web3 instance injected into their browser
// but no account addresses associated with it.
if (_.isUndefined(makerAddress)) {
throw new Error('Tried to send a sign request but user has no associated addresses');
}
this._showFlashMessageIfLedger();
const provider = this._contractWrappers.getProvider();
const ecSignatureString = await signatureUtils.ecSignHashAsync(provider, orderHash, makerAddress);
this._dispatcher.updateSignature(ecSignatureString);
return ecSignatureString;
}
public async mintTestTokensAsync(token: Token): Promise {
try {
signatureHex = await this._eip712SignatureAsync(signerAddress, typedData);
} catch (err) {
// HACK: We are unable to handle specific errors thrown since provider is not an object
// under our control. It could be Metamask Web3, Ethers, or any general RPC provider.
// We check for a user denying the signature request in a way that supports Metamask and
// Coinbase Wallet. Unfortunately for signers with a different error message,
// they will receive two signature requests.
if (err.message.includes('User denied message signature')) {
throw err;
}
const voteHashBuffer = signTypedDataUtils.generateTypedDataHash(typedData);
const voteHashHex = `0x${voteHashBuffer.toString('hex')}`;
signatureHex = await signatureUtils.ecSignHashAsync(providerEngine, voteHashHex, signerAddress);
}
const signedVote = { ...typedData.message, signature: signatureHex };
return signedVote;
}
private readonly _eip712SignatureAsync = async (address: string, typedData: any): Promise => {