okx / js-wallet-sdk

Multi-chain typescript signature sdk, supports bitcoin, ethereum, solana, cosmos, etc.
https://okx.github.io/js-wallet-sdk/#/
MIT License
224 stars 79 forks source link

Calling wallet.signMessage leads to an error. #125

Closed l1135677068 closed 1 month ago

l1135677068 commented 1 month ago

i follow this step https://www.okx.com/zh-hans/web3/build/docs/waas/walletapi-quickstart-multi-chain and i want to create a account, and when i call wallet.signMessage(signParams); The error occurs.

// version is 1.0.0
import { EthWallet } from "@okxweb3/coin-ethereum";
const wallet = new EthWallet()
let now = new Date();

let timestamp = now.getTime();

let timestampString = timestamp.toString();

console.log(timestampString);

let signParams = {
// The derivePrivateKey is from last step from this wallet.getDerivedPrivateKey() response
    privateKey: derivePrivateKey,
    data: timestampString
}

let signature = await wallet.signMessage(signParams);
console.log(signature);

And the error is Invalid messageType: undefined at hashMessage (http://localhost:3001/js-wallet-sdk-demo/static/js/bundle.js:42517:13)

evieyee commented 1 month ago

You can try to understand the different types of sign typed messages in Ethereum, such as: EIP-191 and EIP-712.

Here's the demo after the fix:

// version is 1.0.0
import { EthWallet } from "@okxweb3/coin-ethereum";
const wallet = new EthWallet()
let now = new Date();
let timestamp = now.getTime();
let timestampString = timestamp.toString();
const data = {
    type: eth.MessageTypes.ETH_SIGN,
    message: timestampString
};
let signParams: SignTxParams = {
    privateKey: privateKey,
    data: data
};

let signature = await wallet.signMessage(signParams)
l1135677068 commented 1 month ago

You can try to understand the different types of sign typed messages in Ethereum, such as: EIP-191 and EIP-712.

Here's the demo after the fix:

// version is 1.0.0
import { EthWallet } from "@okxweb3/coin-ethereum";
const wallet = new EthWallet()
let now = new Date();
let timestamp = now.getTime();
let timestampString = timestamp.toString();
const wallet = new eth.EthWallet()
const data = {
    type: eth.MessageTypes.ETH_SIGN,
    message: timestampString
};
let signParams: SignTxParams = {
    privateKey: privateKey,
    data: data
};

let signature = await wallet.signMessage(signParams)

Thanks for your answer, but i still have a question. where is the variable eth from ?

evieyee commented 1 month ago

You can try to understand the different types of sign typed messages in Ethereum, such as: EIP-191 and EIP-712. Here's the demo after the fix:

// version is 1.0.0
import { EthWallet } from "@okxweb3/coin-ethereum";
const wallet = new EthWallet()
let now = new Date();
let timestamp = now.getTime();
let timestampString = timestamp.toString();
const wallet = new eth.EthWallet()
const data = {
    type: eth.MessageTypes.ETH_SIGN,
    message: timestampString
};
let signParams: SignTxParams = {
    privateKey: privateKey,
    data: data
};

let signature = await wallet.signMessage(signParams)

Thanks for your answer, but i still have a question. where is the variable eth from ?

Sorry, please use the updated example:

import { EthWallet,MessageTypes } from "@okxweb3/coin-ethereum";
let wallet = new EthWallet();
let now = new Date();
let timestamp = now.getTime();
let timestampString = timestamp.toString();
console.log(timestampString);
let data = {
        type: MessageTypes.ETH_SIGN,// MessageTypes.PERSONAL_SIGN, etc...
        message: timestampString
    };
let signParams = {
    privateKey: privateKey,
    data: data
}
let signature = await wallet.signMessage(signParams);
console.log(signature);