Skip to content

Commit

Permalink
chore: add test case for issue
Browse files Browse the repository at this point in the history
  • Loading branch information
billyjacoby committed Nov 8, 2024
1 parent 566e858 commit fc20dc9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
38 changes: 23 additions & 15 deletions packages/sdk-ts/src/core/accounts/PrivateKey.spec.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 () => {
Expand Down
13 changes: 7 additions & 6 deletions packages/sdk-ts/src/core/accounts/PrivateKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
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]),
)
}

/**
Expand Down Expand Up @@ -365,9 +368,7 @@ export class PrivateKey {
}

const decodedExtension =
InjectiveTypesV1Beta1TxExt.ExtensionOptionsWeb3Tx.decode(
extension.value,
)
InjectiveTypesV1Beta1TxExt.ExtensionOptionsWeb3Tx.decode(extension.value)

const ethereumChainId = Number(
decodedExtension.typedDataChainID,
Expand Down

0 comments on commit fc20dc9

Please sign in to comment.