diff --git a/packages/coin-cosmos/CHANGELOG.md b/packages/coin-cosmos/CHANGELOG.md index ef37eac..af23db6 100644 --- a/packages/coin-cosmos/CHANGELOG.md +++ b/packages/coin-cosmos/CHANGELOG.md @@ -18,4 +18,8 @@ All notable changes to this project will be documented in this file. - **coin-cosmos:** add cosmos common wallet ([17](https://github.com/okx/js-wallet-sdk/pull/17)) +# [1.0.4](https://github.com/okx/js-wallet-sdk) (2023-11-13) + +- **coin-cosmos:** fix injective wallet sign message bug ([24](https://github.com/okx/js-wallet-sdk/pull/24)) + diff --git a/packages/coin-cosmos/package.json b/packages/coin-cosmos/package.json index 2301f06..c2936ad 100644 --- a/packages/coin-cosmos/package.json +++ b/packages/coin-cosmos/package.json @@ -1,6 +1,6 @@ { "name": "@okxweb3/coin-cosmos", - "version": "1.0.3", + "version": "1.0.4", "description": "", "main": "./dist/index.js", "types": "./dist/index.d.ts", diff --git a/packages/coin-cosmos/src/CosmosWallet.ts b/packages/coin-cosmos/src/CosmosWallet.ts index 2378f01..ce47201 100644 --- a/packages/coin-cosmos/src/CosmosWallet.ts +++ b/packages/coin-cosmos/src/CosmosWallet.ts @@ -42,7 +42,9 @@ import { getMPCTransaction, validSignedTransaction, getMPCSignedMessage, -} from './index'; + signWithStdSignDocForINJ, + SignWithSignDocForINJ, +} from './'; export interface CosmosTransferParam { fromAddress: string @@ -669,6 +671,26 @@ export class InjectiveWallet extends CosmosWallet { pubKeyUrl(): string | undefined { return "/injective.crypto.v1beta1.ethsecp256k1.PubKey"; } + + async signMessage(param: SignTxParams): Promise { + try { + const ethSign = this.supportEthSign() + let privateKey; + if (param.privateKey) { + privateKey = base.fromHex(param.privateKey); + } + const message = param.data as SignMessageData + if (message.type == "amino") { + const result = await signWithStdSignDocForINJ(privateKey as Buffer, message.data, ethSign) + return Promise.resolve(result); + } else { + const result = await SignWithSignDocForINJ(privateKey as Buffer, message.data, ethSign) + return Promise.resolve(result); + } + } catch (e) { + return Promise.reject(SignMsgError); + } + } } export class CelestiaWallet extends CosmosWallet { diff --git a/packages/coin-cosmos/src/index.ts b/packages/coin-cosmos/src/index.ts index f216b03..d12911d 100644 --- a/packages/coin-cosmos/src/index.ts +++ b/packages/coin-cosmos/src/index.ts @@ -186,6 +186,33 @@ export interface SignDocMessage { accountNumber: string; } +export async function SignWithSignDocForINJ(privateKey: Uint8Array, message: string, useEthSecp256k1: boolean) { + const m: SignDocMessage = JSON.parse(message) + const signDoc = makeSignDoc(base.fromHex(m.body), base.fromHex(m.authInfo), m.chainId, parseInt(m.accountNumber)); + if (!privateKey) { + const signDocBytes = makeSignBytes(signDoc); + const messageHash = useEthSecp256k1 ? base.keccak256(signDocBytes) : base.sha256(signDocBytes); + return Promise.resolve(base.toHex(messageHash)); + } + const publicKey = private2Public(privateKey, true) + const signDocBytes = makeSignBytes(signDoc); + const messageHash = useEthSecp256k1 ? base.keccak256(signDocBytes) : base.sha256(signDocBytes); + const { signature } = signUtil.secp256k1.sign(Buffer.from(messageHash), privateKey) + return Promise.resolve(encodeSecp256k1Signature(publicKey, signature, false)) +} + +export async function signWithStdSignDocForINJ(privateKey: Uint8Array, message: string, useEthSecp256k1: boolean) { + const m: amino.StdSignDoc = JSON.parse(message) + const signDocBytes = amino.serializeSignDoc(m) + const messageHash = useEthSecp256k1 ? base.keccak256(signDocBytes) : base.sha256(signDocBytes); + if (!privateKey) { + return Promise.resolve(base.toHex(messageHash)); + } + const { signature } = signUtil.secp256k1.sign(Buffer.from(messageHash), privateKey) + const publicKey = private2Public(privateKey, true) + return Promise.resolve(encodeSecp256k1Signature(publicKey, signature, false)) +} + export async function SignWithSignDoc(privateKey: Uint8Array, message: string, useEthSecp256k1: boolean) { const m: SignDocMessage = JSON.parse(message) const signDoc = makeSignDoc(base.fromHex(m.body), base.fromHex(m.authInfo), m.chainId, parseInt(m.accountNumber)); diff --git a/packages/coin-cosmos/tests/luna.test.ts b/packages/coin-cosmos/tests/luna.test.ts index 5c3e730..a83dd52 100644 --- a/packages/coin-cosmos/tests/luna.test.ts +++ b/packages/coin-cosmos/tests/luna.test.ts @@ -1,18 +1,20 @@ -import { base} from '@okxweb3/crypto-lib'; +import {base} from '@okxweb3/crypto-lib'; import { - getNewAddress, - validateAddress, - sendToken, amount2Coin, - amount2StdFee, amount2Coins, - sendIBCToken, - sendAminoMessage, - sendMessages, - GammRegistry, + amount2StdFee, + CommonCosmosWallet, GammAminoConverters, + GammRegistry, + getNewAddress, + InjectiveWallet, SeiWallet, - CommonCosmosWallet + sendAminoMessage, + sendIBCToken, + sendMessages, + sendToken, + SignMessageData, + validateAddress } from '../src'; describe("luna", () => { @@ -259,6 +261,62 @@ describe("injective", () => { ) console.info(v) }); + + test("injective-signmessage", async () => { + const privateKey = "ebc42dae1245fad403bd18f59f7283dc18724d2fc843b61e01224b9789057347" + const wallet = new InjectiveWallet() + const message = "{\n" + + " \"accountNumber\": \"6666\",\n" + + " \"chainId\": \"injective-1\",\n" + + " \"body\": \"0a00\",\n" + + " \"authInfo\": \"0a0912040a02087f188a3412110a0f0a03696e6a12083433393939393939\"\n" + + "}"; + const data: SignMessageData = {type: "signDoc", data: message} + const v = await wallet.signMessage({privateKey, data}) + console.info(v) + }); + + test("injective-signmessage-2", async () => { + const privateKey = "ebc42dae1245fad403bd18f59f7283dc18724d2fc843b61e01224b9789057347" + const wallet = new InjectiveWallet() + const message = "{\n" + + " \"account_number\": \"833360\",\n" + + " \"chain_id\": \"injective-1\",\n" + + " \"fee\": {\n" + + " \"amount\": [\n" + + " {\n" + + " \"amount\": \"100000000000000000\",\n" + + " \"denom\": \"inj\"\n" + + " }\n" + + " ],\n" + + " \"gas\": \"174651\"\n" + + " },\n" + + " \"memo\": \"\",\n" + + " \"msgs\": [\n" + + " {\n" + + " \"type\": \"sei/poolmanager/swap-exact-amount-in\",\n" + + " \"value\": {\n" + + " \"routes\": [\n" + + " {\n" + + " \"pool_id\": \"1\",\n" + + " \"token_out_denom\": \"ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2\"\n" + + " }\n" + + " ],\n" + + " \"sender\": \"inj1ywqe8057srngat8rtz95tkx0ffl2urarkegcc8\",\n" + + " \"token_in\": {\n" + + " \"amount\": \"10000\",\n" + + " \"denom\": \"inj\"\n" + + " },\n" + + " \"token_out_min_amount\": \"545\"\n" + + " }\n" + + " }\n" + + " ],\n" + + " \"sequence\": \"20\"\n" + + "}"; + const data: SignMessageData = {type: "amino", data: message} + const v = await wallet.signMessage({privateKey, data}) + console.info(v) + }); }); describe("common_wallet", () => {