diff --git a/packages/sdk-ts/src/core/accounts/PrivateKey.spec.ts b/packages/sdk-ts/src/core/accounts/PrivateKey.spec.ts index bf650f11b..8f19b4a64 100644 --- a/packages/sdk-ts/src/core/accounts/PrivateKey.spec.ts +++ b/packages/sdk-ts/src/core/accounts/PrivateKey.spec.ts @@ -1,5 +1,7 @@ -import { generateArbitrarySignDoc } from '../tx/index.js' +import { verifyMessage, Wallet } from 'ethers' +import { toUtf8 } from '../../utils' import { PrivateKey } from './PrivateKey.js' +import { generateArbitrarySignDoc } from '../tx/index.js' const pk = process.env.TEST_PRIVATE_KEY as string const seedPhase = process.env.TEST_SEED_PHASE as string @@ -117,6 +119,32 @@ describe('PrivateKey', () => { // }) + it('returns true when checking a pk signature against the signer public key', async () => { + const message = 'this is a test message' + + const wallet = new Wallet(pk) + const ethersSignature = await wallet.signMessage(message) + + const privateKey = PrivateKey.fromHex(pk) + const publicKey = privateKey.toHex() + + const privKeySignatureArray = privateKey.sign( + Buffer.from(toUtf8(message), 'utf-8'), + ) + 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 () => { const privateKey = PrivateKey.fromHex(pk) diff --git a/packages/wallets/wallet-private-key/src/strategy/strategy.ts b/packages/wallets/wallet-private-key/src/strategy/strategy.ts index bf1b5b79c..993ce9210 100644 --- a/packages/wallets/wallet-private-key/src/strategy/strategy.ts +++ b/packages/wallets/wallet-private-key/src/strategy/strategy.ts @@ -204,7 +204,7 @@ export class PrivateKeyWallet try { const signature = await pk.signHashed(Buffer.from(toUtf8(data), 'utf-8')) - return `0x${Buffer.from(signature).toString('base64')}` + return signature } catch (e: unknown) { throw new MetamaskException(new Error((e as any).message), { code: UnspecifiedErrorCode,