Skip to content

Commit

Permalink
Merge pull request okx#24 from okx/develop
Browse files Browse the repository at this point in the history
fix injective wallet sign message bug
  • Loading branch information
yanminmin authored Nov 13, 2023
2 parents b106459 + b0e6df4 commit 31e5d71
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 12 deletions.
4 changes: 4 additions & 0 deletions packages/coin-cosmos/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))


2 changes: 1 addition & 1 deletion packages/coin-cosmos/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
24 changes: 23 additions & 1 deletion packages/coin-cosmos/src/CosmosWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ import {
getMPCTransaction,
validSignedTransaction,
getMPCSignedMessage,
} from './index';
signWithStdSignDocForINJ,
SignWithSignDocForINJ,
} from './';

export interface CosmosTransferParam {
fromAddress: string
Expand Down Expand Up @@ -669,6 +671,26 @@ export class InjectiveWallet extends CosmosWallet {
pubKeyUrl(): string | undefined {
return "/injective.crypto.v1beta1.ethsecp256k1.PubKey";
}

async signMessage(param: SignTxParams): Promise<string> {
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 {
Expand Down
27 changes: 27 additions & 0 deletions packages/coin-cosmos/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
78 changes: 68 additions & 10 deletions packages/coin-cosmos/tests/luna.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down Expand Up @@ -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", () => {
Expand Down

0 comments on commit 31e5d71

Please sign in to comment.