From 8478577679c4544b24f4fa2e684152cad7ed96fc Mon Sep 17 00:00:00 2001 From: billyjacoby Date: Thu, 31 Oct 2024 12:36:59 -0400 Subject: [PATCH 1/3] 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 c723b31b846516c5753fade8decad5f0e729e06e Mon Sep 17 00:00:00 2001 From: Billy Jacoby Date: Thu, 31 Oct 2024 13:40:58 -0400 Subject: [PATCH 2/3] 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 3709a9f79a8653e4448921720fdd6aa34ee972ec Mon Sep 17 00:00:00 2001 From: Billy Jacoby Date: Thu, 31 Oct 2024 13:42:13 -0400 Subject: [PATCH 3/3] 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) {