From 99da278ae2d5e812763cea84a94f3fdf9e88e0ed Mon Sep 17 00:00:00 2001 From: billyjacoby Date: Thu, 31 Oct 2024 12:36:59 -0400 Subject: [PATCH 1/4] fix: return correct signature --- packages/sdk-ts/src/core/accounts/PrivateKey.ts | 13 ++++++------- .../wallet-private-key/src/strategy/strategy.ts | 7 ++++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/sdk-ts/src/core/accounts/PrivateKey.ts b/packages/sdk-ts/src/core/accounts/PrivateKey.ts index 267a28bbf..8aab78294 100644 --- a/packages/sdk-ts/src/core/accounts/PrivateKey.ts +++ b/packages/sdk-ts/src/core/accounts/PrivateKey.ts @@ -176,15 +176,12 @@ export class PrivateKey { * @param {string} messageHashedBytes: the message that will be signed, a Buffer made of bytes * @returns {Uint8Array} a signature of this private key over the given message */ - signHashed(messageHashedBytes: Buffer): Uint8Array { + async signHashed(messageHashedBytes: Buffer): Promise { const { wallet } = this - const signature = wallet.signingKey.sign(messageHashedBytes) - const splitSignature = BytesUtils.splitSignature(signature) + const signature = await wallet.signMessage(messageHashedBytes) - return BytesUtils.arrayify( - BytesUtils.concat([splitSignature.r, splitSignature.s]), - ) + return signature } /** @@ -368,7 +365,9 @@ export class PrivateKey { } const decodedExtension = - InjectiveTypesV1Beta1TxExt.ExtensionOptionsWeb3Tx.decode(extension.value) + InjectiveTypesV1Beta1TxExt.ExtensionOptionsWeb3Tx.decode( + extension.value, + ) const ethereumChainId = Number( decodedExtension.typedDataChainID, diff --git a/packages/wallets/wallet-private-key/src/strategy/strategy.ts b/packages/wallets/wallet-private-key/src/strategy/strategy.ts index bf1b5b79c..2d1898096 100644 --- a/packages/wallets/wallet-private-key/src/strategy/strategy.ts +++ b/packages/wallets/wallet-private-key/src/strategy/strategy.ts @@ -9,7 +9,7 @@ import { DirectSignResponse, PrivateKey as PrivateKeySigner, getInjectiveSignerAddress, -} from '@injectivelabs/sdk-ts' +} from '@injectivelabs/sdk-ts/src/index' import { ErrorType, WalletException, @@ -202,9 +202,10 @@ export class PrivateKeyWallet } try { - const signature = await pk.signHashed(Buffer.from(toUtf8(data), 'utf-8')) + const bufferUTF = Buffer.from(toUtf8(data), 'utf-8') + const signature = await pk.signHashed(bufferUTF) - return `0x${Buffer.from(signature).toString('base64')}` + return signature } catch (e: unknown) { throw new MetamaskException(new Error((e as any).message), { code: UnspecifiedErrorCode, From 671209aba5ca4bc55e04d105d60de504823fa1c2 Mon Sep 17 00:00:00 2001 From: Billy Jacoby Date: Thu, 31 Oct 2024 13:40:58 -0400 Subject: [PATCH 2/4] typo: fix dev import --- packages/wallets/wallet-private-key/src/strategy/strategy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/wallets/wallet-private-key/src/strategy/strategy.ts b/packages/wallets/wallet-private-key/src/strategy/strategy.ts index 2d1898096..15b179868 100644 --- a/packages/wallets/wallet-private-key/src/strategy/strategy.ts +++ b/packages/wallets/wallet-private-key/src/strategy/strategy.ts @@ -9,7 +9,7 @@ import { DirectSignResponse, PrivateKey as PrivateKeySigner, getInjectiveSignerAddress, -} from '@injectivelabs/sdk-ts/src/index' +} from '@injectivelabs/sdk-ts' import { ErrorType, WalletException, From 566e8583cf58c973c72b417cdee6f95d02855de2 Mon Sep 17 00:00:00 2001 From: Billy Jacoby Date: Thu, 31 Oct 2024 13:42:13 -0400 Subject: [PATCH 3/4] chore: add initial test case --- .../src/core/accounts/PrivateKey.spec.ts | 20 +++++++++++++++++++ .../src/strategy/strategy.ts | 3 +-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/sdk-ts/src/core/accounts/PrivateKey.spec.ts b/packages/sdk-ts/src/core/accounts/PrivateKey.spec.ts index 6072a8c79..38e646abc 100644 --- a/packages/sdk-ts/src/core/accounts/PrivateKey.spec.ts +++ b/packages/sdk-ts/src/core/accounts/PrivateKey.spec.ts @@ -1,3 +1,4 @@ +import { toUtf8 } from '../..' import { generateArbitrarySignDoc } from '../tx' import { PrivateKey } from './PrivateKey' @@ -113,6 +114,25 @@ describe('PrivateKey', () => { ).toBe(true) }) + it('returns the correct signature for signing an arbitrary message', () => { + // const pubKey = '0x697e62225Dd22A5afcAa82901089b2151DAEB706' + const message = 'this is a test message' + + const verifiedSignature = + '0xfd0879e35cec78b87ae7e647ebb743093ea15edcb88eb388806adaaff5f645527dab0cfac030b23d702206d6e0840a7bae5d2239518ba20b73c6636f75f150401b' + + const privateKey = PrivateKey.fromHex(pk) + + const privKeySignature = privateKey.sign( + Buffer.from(toUtf8(message), 'utf-8'), + ) + + expect(Buffer.from(privKeySignature).toString('hex')).toEqual( + verifiedSignature, + ) + // + }) + it('returns true when verifying signature for a public key and a cosmos message', () => { // }) diff --git a/packages/wallets/wallet-private-key/src/strategy/strategy.ts b/packages/wallets/wallet-private-key/src/strategy/strategy.ts index 15b179868..993ce9210 100644 --- a/packages/wallets/wallet-private-key/src/strategy/strategy.ts +++ b/packages/wallets/wallet-private-key/src/strategy/strategy.ts @@ -202,8 +202,7 @@ export class PrivateKeyWallet } try { - const bufferUTF = Buffer.from(toUtf8(data), 'utf-8') - const signature = await pk.signHashed(bufferUTF) + const signature = await pk.signHashed(Buffer.from(toUtf8(data), 'utf-8')) return signature } catch (e: unknown) { From fc20dc9b0873067f94b1c6fa716817160fabbf15 Mon Sep 17 00:00:00 2001 From: billyjacoby Date: Fri, 8 Nov 2024 09:43:37 -0500 Subject: [PATCH 4/4] chore: add test case for issue --- .../src/core/accounts/PrivateKey.spec.ts | 38 +++++++++++-------- .../sdk-ts/src/core/accounts/PrivateKey.ts | 13 ++++--- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/packages/sdk-ts/src/core/accounts/PrivateKey.spec.ts b/packages/sdk-ts/src/core/accounts/PrivateKey.spec.ts index 38e646abc..98f44fd51 100644 --- a/packages/sdk-ts/src/core/accounts/PrivateKey.spec.ts +++ b/packages/sdk-ts/src/core/accounts/PrivateKey.spec.ts @@ -1,6 +1,7 @@ -import { toUtf8 } from '../..' +import { verifyMessage, Wallet } from 'ethers' import { generateArbitrarySignDoc } from '../tx' import { PrivateKey } from './PrivateKey' +import { toUtf8 } from '../../utils' const pk = process.env.TEST_PRIVATE_KEY as string const seedPhase = process.env.TEST_SEED_PHASE as string @@ -114,27 +115,34 @@ describe('PrivateKey', () => { ).toBe(true) }) - it('returns the correct signature for signing an arbitrary message', () => { - // const pubKey = '0x697e62225Dd22A5afcAa82901089b2151DAEB706' + it('returns true when verifying signature for a public key and a cosmos message', () => { + // + }) + + it('returns true when checking a pk signature against the signer public key', async () => { const message = 'this is a test message' - const verifiedSignature = - '0xfd0879e35cec78b87ae7e647ebb743093ea15edcb88eb388806adaaff5f645527dab0cfac030b23d702206d6e0840a7bae5d2239518ba20b73c6636f75f150401b' + const wallet = new Wallet(pk) + const ethersSignature = await wallet.signMessage(message) const privateKey = PrivateKey.fromHex(pk) + const publicKey = privateKey.toHex() - const privKeySignature = privateKey.sign( + const privKeySignatureArray = privateKey.sign( Buffer.from(toUtf8(message), 'utf-8'), ) - - expect(Buffer.from(privKeySignature).toString('hex')).toEqual( - verifiedSignature, - ) - // - }) - - it('returns true when verifying signature for a public key and a cosmos message', () => { - // + const privKeySignature = `0x${Buffer.from(privKeySignatureArray).toString( + 'hex', + )}` + + const ethersVerifiedSigner = verifyMessage(message, ethersSignature) + const ethersSignatureVerifiedCorrectly = ethersVerifiedSigner === publicKey + expect(ethersSignatureVerifiedCorrectly).toBe(true) + + const privKeyVerifiedSigner = verifyMessage(message, privKeySignature) + const privKeySignatureVerifiedCorrectly = + privKeyVerifiedSigner === publicKey + expect(privKeySignatureVerifiedCorrectly).toBe(true) }) it('returns true when verifying arbitrary message', async () => { diff --git a/packages/sdk-ts/src/core/accounts/PrivateKey.ts b/packages/sdk-ts/src/core/accounts/PrivateKey.ts index 8aab78294..267a28bbf 100644 --- a/packages/sdk-ts/src/core/accounts/PrivateKey.ts +++ b/packages/sdk-ts/src/core/accounts/PrivateKey.ts @@ -176,12 +176,15 @@ export class PrivateKey { * @param {string} messageHashedBytes: the message that will be signed, a Buffer made of bytes * @returns {Uint8Array} a signature of this private key over the given message */ - async signHashed(messageHashedBytes: Buffer): Promise { + signHashed(messageHashedBytes: Buffer): Uint8Array { const { wallet } = this - const signature = await wallet.signMessage(messageHashedBytes) + const signature = wallet.signingKey.sign(messageHashedBytes) + const splitSignature = BytesUtils.splitSignature(signature) - return signature + return BytesUtils.arrayify( + BytesUtils.concat([splitSignature.r, splitSignature.s]), + ) } /** @@ -365,9 +368,7 @@ export class PrivateKey { } const decodedExtension = - InjectiveTypesV1Beta1TxExt.ExtensionOptionsWeb3Tx.decode( - extension.value, - ) + InjectiveTypesV1Beta1TxExt.ExtensionOptionsWeb3Tx.decode(extension.value) const ethereumChainId = Number( decodedExtension.typedDataChainID,