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

feat: add MsgExecCompat and pick other minor changes #16

Merged
merged 12 commits into from
Aug 28, 2023

Conversation

vinhphuctadang
Copy link

@vinhphuctadang vinhphuctadang commented Aug 22, 2023

Context

https://www.notion.so/injective/EIP712-compatible-MsgExec-22c134266b434f0caa8ebd407053ac57

Changes

Create MsgExecCompat message that copies behavior from MsgExec, the only difference is to unmarshal JSON each message in msg[]

Also, we need to add GlobalCdc in order to parse proto JSON

Test

Using client script to execute the message:

  • Invalid message format:
import {
    getEthereumAddress,
    PrivateKey,
} from "@injectivelabs/sdk-ts";
import axios, { Axios } from "axios";
import https from "https";

const baseURL = "https://devnet.api.injective.dev";
const privateKey = PrivateKey.fromHex(
    "5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e",
);
const injectiveAddress = privateKey.toAddress().toBech32();
const granterAddress = 'inj17gkuet8f6pssxd8nycm3qr9d9y699rupv6397z'
const ethereumAddress = getEthereumAddress(injectiveAddress);

export const prepareSignBroadcast = async (): Promise<unknown> => {
    const msgSend = {
        '@type': '/cosmos.bank.v1beta1.MsgSend',
        from_address: granterAddress,
        to_address: "inj1995xnrrtnmtdgjmx0g937vf28dwefhkhy6gy5e",
        amount: [
            {
                denom:"inj", amount:"10000000000000000"
            },
        ]
    }

    const msgAuthz = {
        grantee: 'inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku',
        msgs: [
            JSON.stringify(msgSend).substring(0, 15),
        ],
        '@type': '/cosmos.authz.v1beta1.MsgExecCompat'
    }

    console.log('msg to send:', msgAuthz)
    let client = new Axios({
        baseURL: baseURL,
        transformRequest: axios.defaults.transformRequest,
        transformResponse: axios.defaults.transformResponse,
        httpsAgent: new https.Agent({  
            rejectUnauthorized: false
        })
    })

    let msgJson = JSON.stringify(msgAuthz)
    let base64Msg = Buffer.from(msgJson).toString('base64')
    let prepareTxPayload = {
        "chainID": 5,
        "fee": {
        "delegateFee": false,
        "gas": 200000,
        "price": [
            {
                "amount": "10000000000000000000",
                "denom": "inj"
            }
        ]
        },
        "memo": "",
        "msgs": [
            base64Msg,
        ],
        "sequence": 0,
        "signerAddress": ethereumAddress,
        "timeoutHeight": 0
    }

    const prepareTxResponse = await client.post('/api/exchange/gateway/v1/prepareTx', prepareTxPayload);
    const prepareTxResponseBody = prepareTxResponse.data
    console.log('prepareTx response:', prepareTxResponseBody)

    const signature = await privateKey.signTypedData(
        JSON.parse(prepareTxResponseBody.data),
    );
    
    const parsedTypeData = JSON.parse(prepareTxResponseBody.data)
    const tx = parsedTypeData.message
    const msgs = [
    base64Msg,
    ]
    
    const broadcastPayload = {
    "chainID": 5,
    "feePayer": prepareTxResponseBody.feePayer,
    "feePayerSig": prepareTxResponseBody.feePayerSig,
    "msgs": msgs,
    "pubKey": {
        "key": '0x' + privateKey.toPublicKey().toHex(),
        "type": "/injective.crypto.v1beta1.ethsecp256k1.PubKey"
    },
    "signature": '0x' + Buffer.from(signature).toString('hex'),
    "tx": Buffer.from(JSON.stringify(tx)).toString('base64'),
    "mode": 'block',
    }
    const broadcastResponse = await client.post('/api/exchange/gateway/v1/broadcastTx', broadcastPayload)
    console.log('broadcastTx response:', broadcastResponse.data)
    return '' // broadcastResponse.data;
};

(async () => {
    await prepareSignBroadcast();
})();

Error happens:

image

Using valid message format:

import {
    getEthereumAddress,
    PrivateKey,
} from "@injectivelabs/sdk-ts";
import axios, { Axios } from "axios";
import https from "https";

// const baseURL = "https://localhost:4448";
const baseURL = "https://devnet.api.injective.dev";
const privateKey = PrivateKey.fromHex(
    "5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e",
);
const injectiveAddress = privateKey.toAddress().toBech32();
const granterAddress = 'inj17gkuet8f6pssxd8nycm3qr9d9y699rupv6397z'
const ethereumAddress = getEthereumAddress(injectiveAddress);

export const prepareSignBroadcast = async (): Promise<unknown> => {
    const msgSend = {
        '@type': '/cosmos.bank.v1beta1.MsgSend',
        from_address: granterAddress,
        to_address: "inj1995xnrrtnmtdgjmx0g937vf28dwefhkhy6gy5e",
        amount: [
            {
                denom:"inj", amount:"10000000000000000"
            },
        ]
    }

    const msgCreateDenom = {
        '@type': '/injective.tokenfactory.v1beta1.MsgCreateDenom',
        'sender': granterAddress,
        'subdenom': 'exampledenom'
    }

    const msgAuthz = {
        grantee: 'inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku',
        msgs: [
            JSON.stringify(msgSend),
            JSON.stringify(msgCreateDenom),
        ],
        '@type': '/cosmos.authz.v1beta1.MsgExecCompat'
    }

    console.log('msg to send:', msgAuthz)
    let client = new Axios({
        baseURL: baseURL,
        transformRequest: axios.defaults.transformRequest,
        transformResponse: axios.defaults.transformResponse,
        httpsAgent: new https.Agent({  
            rejectUnauthorized: false
        })
    })

    let msgJson = JSON.stringify(msgAuthz)
    let base64Msg = Buffer.from(msgJson).toString('base64')
    let prepareTxPayload = {
        "chainID": 5,
        "fee": {
        "delegateFee": false,
        "gas": 200000,
        "price": [
            {
                "amount": "10000000000000000000",
                "denom": "inj"
            }
        ]
        },
        "memo": "",
        "msgs": [
            base64Msg,
        ],
        "sequence": 0,
        "signerAddress": ethereumAddress,
        "timeoutHeight": 0
    }

    const prepareTxResponse = await client.post('/api/exchange/gateway/v1/prepareTx', prepareTxPayload);
    const prepareTxResponseBody = prepareTxResponse.data
    console.log('prepareTx response:', prepareTxResponseBody)

    const signature = await privateKey.signTypedData(
        JSON.parse(prepareTxResponseBody.data),
    );
    
    const parsedTypeData = JSON.parse(prepareTxResponseBody.data)
    const tx = parsedTypeData.message
    const msgs = [
    base64Msg,
    ]
    
    const broadcastPayload = {
    "chainID": 5,
    "feePayer": prepareTxResponseBody.feePayer,
    "feePayerSig": prepareTxResponseBody.feePayerSig,
    "msgs": msgs,
    "pubKey": {
        "key": '0x' + privateKey.toPublicKey().toHex(),
        "type": "/injective.crypto.v1beta1.ethsecp256k1.PubKey"
    },
    "signature": '0x' + Buffer.from(signature).toString('hex'),
    "tx": Buffer.from(JSON.stringify(tx)).toString('base64'),
    "mode": 'block',
    }
    const broadcastResponse = await client.post('/api/exchange/gateway/v1/broadcastTx', broadcastPayload)
    console.log('broadcastTx response:', broadcastResponse.data)
    return '' // broadcastResponse.data;
};

(async () => {
    await prepareSignBroadcast();
})();

According to events, messages are executed correctly:

image image image

@vinhphuctadang vinhphuctadang changed the title F/v0.47.3 inj 3 feat: add MsgExecCompat and pick other changes Aug 22, 2023
@vinhphuctadang vinhphuctadang changed the title feat: add MsgExecCompat and pick other changes feat: add MsgExecCompat and pick other minor changes Aug 22, 2023
Comment on lines +12 to +18
go func() {
app.StreamEvents <- StreamEvents{
Events: events,
Height: uint64(height),
Flush: flush,
}
}()

Check notice

Code scanning / CodeQL

Spawning a Go routine Note

Spawning a Go routine may be a possible source of non-determinism
@vinhphuctadang vinhphuctadang marked this pull request as ready for review August 22, 2023 04:43
x/authz/msgs.go Outdated Show resolved Hide resolved
x/authz/codec/cdc.go Outdated Show resolved Hide resolved
@dbrajovic dbrajovic self-requested a review August 28, 2023 08:27
@vinhphuctadang vinhphuctadang merged commit 1881d8c into f/v0.47.3-inj-1 Aug 28, 2023
14 of 26 checks passed
@vinhphuctadang vinhphuctadang deleted the f/v0.47.3-inj-3 branch August 28, 2023 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants