Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return correct signature #510

Merged
merged 5 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion packages/sdk-ts/src/core/accounts/PrivateKey.spec.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down