Skip to content

Commit

Permalink
Merge pull request #510 from InjectiveLabs/fix/pk-sign-arbitraty
Browse files Browse the repository at this point in the history
fix: return correct signature
  • Loading branch information
ThomasRalee authored Nov 29, 2024
2 parents 1b19d62 + c85814e commit b88b459
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
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

0 comments on commit b88b459

Please sign in to comment.