Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Fireblocks #65

Open
anxolin opened this issue Jul 29, 2022 · 0 comments
Open

Support for Fireblocks #65

anxolin opened this issue Jul 29, 2022 · 0 comments
Labels
help wanted Extra attention is needed

Comments

@anxolin
Copy link
Contributor

anxolin commented Jul 29, 2022

Some team has implemented EIP-712 signatures in CowProtocol using Fireblocks and managed to trade using the SDK.

Right now the SDK is not web3 library agnostic. This issue would be to allow to use Fireblocks library in the SDK.

They provided the snippet of conde on how they created the order. We already have the signing schema, so this issue would be just to abstract the instantiation of the SDK to accept fireblocks, so the SDK can sign orders using the library.

Snippet the team shared:

        const orderForSigning: UnsignedOrder = {
            sellTokenBalance: OrderBalance.ERC20,
            buyTokenBalance: OrderBalance.ERC20,
            appData:
                "0x0000000000000000000000000000000000000000000000000000000000000000",
            ...order,
        };

        // Sign the order.
        const fbOrderRequest = {
            type: "EIP712",
            index: 0,
            content: {
                types: {
                    Order: [
                        { name: "sellToken", type: "address" },
                        { name: "buyToken", type: "address" },
                        { name: "receiver", type: "address" },
                        { name: "sellAmount", type: "uint256" },
                        { name: "buyAmount", type: "uint256" },
                        { name: "validTo", type: "uint32" },
                        { name: "appData", type: "bytes32" },
                        { name: "feeAmount", type: "uint256" },
                        { name: "kind", type: "string" },
                        { name: "partiallyFillable", type: "bool" },
                        { name: "sellTokenBalance", type: "string" },
                        { name: "buyTokenBalance", type: "string" },
                    ],
                    EIP712Domain: [
                        { name: "name", type: "string" },
                        { name: "version", type: "string" },
                        { name: "chainId", type: "uint256" },
                        { name: "verifyingContract", type: "address" },
                    ],
                },
                domain: {
                    name: "Gnosis Protocol",
                    chainId: "1",
                    version: "v2",
                    verifyingContract:
                        "0x9008d19f58aabd9ed0d60971565aa8510560ab41",
                },
                message: order,
            },
        };

        const { signedMessages, id } = await this.fireblocks.signData(
            fbOrderRequest,
            "defi gateway cowswap"
        );

        const v = signedMessages[0].signature.v;
        invariant(v !== undefined, "no v");
        const cowOrderId = await this.cowswapApi.createOrder(
            orderForSigning,
            signedMessages[0].signature.fullSig,
            v,
            this.walletAddress
        );

And here is the createOrder implementation:


    async createOrder(
        order: UnsignedOrder,
        signature: string,
        v: number,
        walletAddress: string
    ) {
        let vStr = "";
        if (v == 0) {
            // 27 hex
            vStr = "1b";
        } else if (v == 1) {
            // 28 hex
            vStr = "1c";
        } else {
            invariant(false, "invalid v");
        }

        const data = {
            // Add 0x and v field.
            signature: "0x" + signature + vStr,
            from: walletAddress,
            signingScheme: "eip712",
            ...order,
        };
        console.log(data);
        const response = await axios.post(
            COWSWAP_BASE_URL + "/api/v1/orders",
            data
        );
        return response.data;

the fireblocks payload above requires a primaryType: “Order” field

@anxolin anxolin added the help wanted Extra attention is needed label Jul 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant