How to use the @0x/utils.signTypedDataUtils.generateTypedDataHash function in @0x/utils

To help you get started, we’ve selected a few @0x/utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github 0xProject / 0x-monorepo / packages / website / ts / pages / governance / vote_form.tsx View on Github external
const { provider: providerEngine } = this.props;
        let signatureHex;

        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 => {
github 0xProject / 0x-monorepo / packages / website / ts / pages / governance / vote_form.tsx View on Github external
const message = {
            zeip: zeipId,
            preference: votePreference,
            from: makerAddress,
        };
        const typedData = {
            types: {
                EIP712Domain: domainType,
                Vote: voteType,
            },
            domain: domainData,
            message,
            primaryType: 'Vote',
        };

        const voteHashBuffer = signTypedDataUtils.generateTypedDataHash(typedData);
        const voteHashHex = `0x${voteHashBuffer.toString('hex')}`;
        try {
            const signedVote = await this._signVoteAsync(makerAddress, typedData);
            // Store the signed vote
            this.setState(prevState => ({
                ...prevState,
                signedVote,
                voteHash: voteHashHex,
                isSuccessful: true,
                isAwaitingLedgerSignature: false,
            }));

            const voteDomain = utils.isProduction()
                ? `https://${configs.DOMAIN_VOTE}`
                : `https://${configs.DOMAIN_VOTE}/staging`;
            const voteEndpoint = `${voteDomain}/v1/vote`;
github 0xProject / 0x-coordinator-server / ts / src / handlers.ts View on Github external
private async _generateApprovalSignatureAsync(
        txOrigin: string,
        signedTransaction: SignedZeroExTransaction,
        coordinatorOrders: Order[],
        chainId: number,
        approvalExpirationTimeSeconds: number,
    ): Promise {
        const contractWrappers = this._chainIdToContractWrappers[chainId];
        const typedData = await eip712Utils.createCoordinatorApprovalTypedDataAsync(
            signedTransaction,
            contractWrappers.coordinator.address,
            txOrigin,
        );
        const approvalHashBuff = signTypedDataUtils.generateTypedDataHash(typedData);

        // Since a coordinator can have multiple feeRecipientAddresses,
        // we need to make sure we issue a signature for each feeRecipientAddress
        // found in the orders submitted (i.e., someone can batch fill two coordinator
        // orders, each with a different feeRecipientAddress). In that case, we issue a
        // signature/expiration for each feeRecipientAddress
        const feeRecipientAddressSet = new Set();
        _.each(coordinatorOrders, o => {
            feeRecipientAddressSet.add(o.feeRecipientAddress);
        });
        const signatures = [];
        const feeRecipientAddressesUsed = Array.from(feeRecipientAddressSet);
        for (const feeRecipientAddress of feeRecipientAddressesUsed) {
            const feeRecipientIfExists = _.find(
                this._configs.CHAIN_ID_TO_SETTINGS[chainId].FEE_RECIPIENTS,
                f => f.ADDRESS === feeRecipientAddress,
github 0xProject / 0x-monorepo / contracts / test-utils / src / order_hash.ts View on Github external
getOrderHashBuffer(order: SignedOrder | Order): Buffer {
        try {
            assert.doesConformToSchema('order', order, schemas.orderSchema, [schemas.hexSchema]);
        } catch (error) {
            if (_.includes(error.message, INVALID_TAKER_FORMAT)) {
                const errMsg = `Order taker must be of type string. If you want anyone to be able to fill an order - pass ${NULL_ADDRESS}`;
                throw new Error(errMsg);
            }
            throw error;
        }
        const typedData = eip712Utils.createOrderTypedData(order);
        const orderHashBuff = signTypedDataUtils.generateTypedDataHash(typedData);
        return orderHashBuff;
    },
};
github 0xProject / 0x-monorepo / contracts / test-utils / src / transaction_hash.ts View on Github external
getTransactionHashBuffer(transaction: ZeroExTransaction | SignedZeroExTransaction): Buffer {
        const typedData = eip712Utils.createZeroExTransactionTypedData(transaction);
        const transactionHashBuff = signTypedDataUtils.generateTypedDataHash(typedData);
        return transactionHashBuff;
    },
};
github 0xProject / 0x-monorepo / contracts / coordinator / src / hash_utils.ts View on Github external
async getApprovalHashBufferAsync(
        transaction: SignedZeroExTransaction,
        verifyingContract: string,
        txOrigin: string,
    ): Promise {
        const typedData = await eip712Utils.createCoordinatorApprovalTypedDataAsync(
            transaction,
            verifyingContract,
            txOrigin,
        );
        const hashBuffer = signTypedDataUtils.generateTypedDataHash(typedData);
        return hashBuffer;
    },
    async getApprovalHashHexAsync(
github 0xProject / 0x-monorepo / packages / subproviders / src / subproviders / private_key_wallet.ts View on Github external
public async signTypedDataAsync(address: string, typedData: EIP712TypedData): Promise {
        if (typedData === undefined) {
            throw new Error(WalletSubproviderErrors.DataMissingForSignTypedData);
        }
        assert.isETHAddressHex('address', address);
        if (address !== this._address) {
            throw new Error(
                `Requested to sign message with address: ${address}, instantiated with address: ${this._address}`,
            );
        }
        const dataBuff = signTypedDataUtils.generateTypedDataHash(typedData);
        const sig = ethUtil.ecsign(dataBuff, this._privateKeyBuffer);
        const rpcSig = ethUtil.toRpcSig(sig.v, sig.r, sig.s);
        return rpcSig;
    }
}