When I try to Store code with base64 encoded wasm bytecode string. This happens
Error: failed to execute message; message index: 0: Error calling the VM: Error during static Wasm validation: Wasm bytecode could not be deserialized. Deserialization error: "magic header not detected: bad magic number (at offset 0x0)": create wasm contract failed [!injective!labs/[email protected]/x/wasm/keeper/keeper.go:182] With gas wanted: '50000000' and gas used: '2555596' -> wasm file is string
const buffer = Buffer.from(wasm, 'base64');
const wasmUint8array = new Uint8Array(buffer);
const msg = MsgStoreCode.fromJSON({
sender: walletAccount,
wasmBytes: wasmUint8array,
});
which fixed the issue.
this happens because
export function fromUtf8(str: Uint8Array | string): Uint8Array {
if (typeof str !== 'string') {
return str
}
return new TextEncoder().encode(str)
}
TextEncoder only accept utf-8 as input. So we need to specify whether the string is base64 or utf-8, or return Uint8array depending on encoded type.
This error happens when I try to store code with MetaMask Wallet Strategy/MsgBroadcaster
signature verification failed: unable to verify signer signature of EIP712 typed data; please verify account number (740687) and chain-id (injective-888)
Of coure the reason for this is because the client side signed data and chain's data is different.
{"types":{"EIP712Domain":[{"name":"name","type":"string"},{"name":"version","type":"string"},{"name":"chainId","type":"uint256"},{"name":"verifyingContract","type":"address"},{"name":"salt","type":"string"}],"Tx":[{"name":"context","type":"string"},{"name":"msgs","type":"string"}]},"primaryType":"Tx","domain":{"name":"Injective Web3","version":"1.0.0","chainId":"0xaa36a7","verifyingContract":"0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC","salt":"0"},"message":{"context":"{\"account_number\":740687,\"chain_id\":\"injective-888\",\"fee\":{\"amount\":[{\"denom\":\"inj\",\"amount\":\"327120160000000\"}],\"gas\":2044501},\"memo\":\"\",\"sequence\":28,\"timeout_height\":41175676}","msgs":"[{\"@type\":\"/cosmwasm.wasm.v1.MsgStoreCode\",\"sender\":\"inj1j407ampkleyrhz2cv4nuksmwc3naj2n95jqljz\",\"wasm_byte_code\":{\"0\":65,\"1\":71,\"2\":70,\"3\":122,\"4\":98,\"5\":81,\"6\":69,\"7\":65,\"8\":65,\"9\":65,\"10\":65,\"11\":66,\"12\":119,\"13\":103,\"14\":69,\"15\":89,\"16\":89,\"17\":65,\"18\":74,\"19\":47,\"20\":102,\"21\":119,\"22\":70,\"23\":47,\"24\":89,\"25\":65,\"26\":74,\"27\":47,\"28\":102,\"29\":119,\"30\":66,\"31\":103,\"32\":65,\"33\":51,\"34\":57,\"35\":47,\"36\":102,\"37\":119,\"38\":70,\"39\":47,\"40\":89,\"41\":65,\"42\":78,\"43\":47,\"44\":102,\"45\":51,\"46\":56,\"47\":65,\"48\":89,\"49\":65,\"50\":70,\"51\":47,\"52\".....}]"
}
}
{"types":{"EIP712Domain":[{"name":"name","type":"string"},{"name":"version","type":"string"},{"name":"chainId","type":"uint256"},{"name":"verifyingContract","type":"address"},{"name":"salt","type":"string"}],"Tx":[{"name":"context","type":"string"},{"name":"msgs","type":"string"}]},"primaryType":"Tx","domain":{"name":"Injective Web3","version":"1.0.0","chainId":"0xaa36a7","verifyingContract":"0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC","salt":"0"},"message":{"context":"{\"account_number\":740687,\"chain_id\":\"injective-888\",\"fee\":{\"amount\":[{\"denom\":\"inj\",\"amount\":\"327120160000000\"}],\"gas\":2044501},\"memo\":\"\",\"sequence\":28,\"timeout_height\":41175676}","msgs":"[{\"@type\":\"/cosmwasm.wasm.v1.MsgStoreCode\",\"sender\":\"inj1j407ampkleyrhz2cv4nuksmwc3naj2n95jqljz\",\"wasm_byte_code\":\"QUdGemJRRUFBQUFCd2dFWVlBSi9md0YvWUFKL2Z3QmdBMzkvZndGL1lBTi9mMzhBWUFGL0FHQUVmMzkvZndCZ0FYOEJmMkFGZjM5L2YzOEFZQWQvZjM5L2YzOS9BR0FHZjM5L2YzOS9BR0FBQUdBSWYzOS9mMzkvZjM4QVlBVi9mMzkvZndGL1lBZC9mMzkvZjM5L0FYOWdBMzkvZndGK1lBVi9mMzkvZmdCZ0JIOS9mMzhCZjJBRGYzOStBR0FEZm45L0FHQUdmMzkvZjM5L0FYOWdDMzkvZjM5L2YzOS9mMzkvQVg5Z0RuOS9mMzkvZjM5L2YzOS9mMzkvQVg5Z0EzNS9md0YvWUFSL2ZuNStBQUthQWc4RFpXNTJCV0ZpYjNKMEFBUURaVzUyQjJSaVgzSmxZV1FBQmdObGJuWUlaR0pmZDNKcGRHV
...}]"
}
}
I fixed this issue by hardcoding the msgs inside the eip712 data with none Uint8Array value
eip712TypedData.message.msgs = '[{\"@type\":\"/cosmwasm.wasm.v1.MsgStoreCode\",\"sender\":\"inj1j407ampkleyrhz2cv4nuksmwc3naj2n95jqljz\",\"wasm_byte_code\":\"QUdGemJRRUFBQUFCd2dFWVlBSi9md0YvWUFKL2Z3QmdBMzkvZndGL1lBTi9mMzhBWUFGL0FHQUVmMzkvZndCZ0FYOEJmMkFGZjM5L2YzOEFZQWQvZjM5L2YzOS9BR0FHZjM5L2YzOS9BR0FBQUdBSWYzOS9mMzkvZjM4QVlBVi9mMzkvZndGL1lBZC9mMzkvZjM5L0FYOWdBMzkvZndGK1lBVi9mMzkvZmdCZ0JIOS9mMzhCZjJBRGYzOStBR0FEZm45L0FHQUdmMzkvZjM5L0FYOWdDMzkvZjM5L2YzOS9mMzkvQVg5Z0RuOS9mMzkvZjM5L2YzOS9mMzkvQVg5Z0EzNS9md0YvWUFSL2ZuNStBQUthQWc4RFpXNTJCV0ZpYjNKMEFBUURaVzUyQjJSaVgzSmxZV1FBQmdObGJuWUlaR0pmZDNKcGRHV
...}]"'Ï
wasm byte code turns into Uint8Array when broadcastV2 is called and getEip712TypedDataV2 inside broadcastV2 turns the wasmByteCode into Uint8Array with fromUtf8