Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Compatible with OneKeyConnect.starcoinSignMessage
  • Loading branch information
wk3368 committed Nov 11, 2021
1 parent 37ee6df commit aed2b05
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
8 changes: 5 additions & 3 deletions src/utils/signed-message.spec.ts
@@ -1,20 +1,22 @@
import { arrayify } from '@ethersproject/bytes';
import { addHexPrefix, stripHexPrefix } from 'ethereumjs-util';
import { encodeSignedMessage, recoverSignedMessageAddress } from "./signed-message";

const chainId = 254
const exampleMessage = 'Example `personal_sign` message 中文'
// const exampleMessage = 'helloworld'
const msgBytes = new Uint8Array(Buffer.from(exampleMessage, 'utf8'))
// const msgBytes = new Uint8Array(Buffer.from(exampleMessage, 'utf8'))
// const msgHex = Buffer.from(exampleMessage, 'utf8').toString('hex')
// console.log({ msgHex })
// console.log(Buffer.from(stripHexPrefix(msgHex), 'hex').toString('utf8'))

test('encode SignedMessage: simple-keyring', async () => {
// simple-keyring
const publicKey = '0x32ed52d319694aebc5b52e00836e2f7c7d2c7c7791270ede450d21dbc90cbfa1'
const privateKey = '0x587737ebefb4961d377a3ab2f9ceb37b1fa96eb862dfaf954a4a1a99535dfec0'
const address = '0xd7f20befd34b9f1ab8aeae98b82a5a51'
const privateKeyBytes = arrayify(privateKey)
const signedMessageHex = await encodeSignedMessage(msgBytes, privateKeyBytes, chainId);
const signedMessageHex = await encodeSignedMessage(exampleMessage, privateKeyBytes, chainId);
console.log({ chainId, exampleMessage, privateKey, publicKey, address, signedMessageHex })

try {
Expand All @@ -33,7 +35,7 @@ test('encode SignedMessage: hd-keyring', async () => {
const privateKey = '0xda82fa47266c40c84d76e20b0a278d1b27ae4a14c9c318e54457722d739371b0'
const address = '0xfa0d5060eb2622e26b4dc307a481db0c'
const privateKeyBytes = arrayify(privateKey)
const signedMessageHex = await encodeSignedMessage(msgBytes, privateKeyBytes, chainId);
const signedMessageHex = await encodeSignedMessage(exampleMessage, privateKeyBytes, chainId);
console.log({ chainId, exampleMessage, privateKey, publicKey, address, signedMessageHex })

try {
Expand Down
18 changes: 11 additions & 7 deletions src/utils/signed-message.ts
Expand Up @@ -18,7 +18,7 @@ export function encodeTransactionAuthenticatorEd25519(signatureBytes: bytes, pub
return authenticatorEd25519;
}

function getEd25519SignMsgBytes(
export function getEd25519SignMsgBytes(
signingMessage: SigningMessage,
): bytes {
const hasher = createSigningMessageHasher();
Expand All @@ -41,11 +41,14 @@ function getEd25519SignMsgBytes(
}

// simulate OneKeyConnect.starcoinSignMessage with the same response payload
async function signMessage(signingMessageBytes: bytes, privateKeyHex: string): Promise<Record<string, string>> {
export async function signMessage(msg: string, privateKeyHex: string): Promise<Record<string, string>> {
const msgBytes = new Uint8Array(Buffer.from(msg, 'utf8'))
const signingMessage = new SigningMessage(msgBytes);
const signingMessageBytes = getEd25519SignMsgBytes(signingMessage)
const publicKeyHex = await <string><unknown>ed.getPublicKey(stripHexPrefix(privateKeyHex));
const signatureBytes = await ed.sign(signingMessageBytes, stripHexPrefix(privateKeyHex))
const signatureHex = hexlify(signatureBytes)
return Promise.resolve({ publicKeyHex, signatureHex })
return Promise.resolve({ publicKey: publicKeyHex, signature: signatureHex })
}

export async function generateSignedMessage(signingMessage: SigningMessage, id: uint8, publicKeyHex: string, signatureHex: string): Promise<string> {
Expand All @@ -62,11 +65,12 @@ export async function generateSignedMessage(signingMessage: SigningMessage, id:
return Promise.resolve(signedMessageHex);
}

export async function encodeSignedMessage(msgBytes: bytes, privateKeyBytes: bytes, chainId: uint8): Promise<string> {
export async function encodeSignedMessage(msg: string, privateKeyBytes: bytes, chainId: uint8): Promise<string> {
const msgBytes = new Uint8Array(Buffer.from(msg, 'utf8'))
const signingMessage = new SigningMessage(msgBytes);
const signingMessageBytes = getEd25519SignMsgBytes(signingMessage)
const { publicKeyHex, signatureHex } = await signMessage(signingMessageBytes, hexlify(privateKeyBytes))
const signedMessageHex = await generateSignedMessage(signingMessage, chainId, publicKeyHex, signatureHex)
const { publicKey, signature } = await signMessage(msg, hexlify(privateKeyBytes))

const signedMessageHex = await generateSignedMessage(signingMessage, chainId, publicKey, signature)
return Promise.resolve(signedMessageHex);
}

Expand Down

0 comments on commit aed2b05

Please sign in to comment.