Skip to content
This repository has been archived by the owner on Jun 17, 2021. It is now read-only.

Commit

Permalink
Enforce Buffer input type for hashPersonalMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
cgewecke committed Apr 28, 2020
1 parent 391dfc1 commit e221440
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as secp256k1 from 'secp256k1'
import * as BN from 'bn.js'
import { toBuffer, setLengthLeft, bufferToHex } from './bytes'
import { keccak } from './hash'
import { assertIsBuffer } from './helpers'

export interface ECDSASignature {
v: number
Expand Down Expand Up @@ -134,6 +135,7 @@ export const isValidSignature = function(
* used to produce the signature.
*/
export const hashPersonalMessage = function(message: Buffer): Buffer {
assertIsBuffer(message)
const prefix = Buffer.from(
`\u0019Ethereum Signed Message:\n${message.length.toString()}`,
'utf-8',
Expand Down
7 changes: 7 additions & 0 deletions test/signature.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ describe('hashPersonalMessage', function() {
Buffer.from('8144a6fa26be252b86456491fbcd43c1de7e022241845ffea1c3df066f7cfede', 'hex'),
)
})
it('should throw if input is not a buffer', function() {
try {
hashPersonalMessage((<unknown>[0, 1, 2, 3, 4]) as Buffer)
} catch (err) {
assert(err.message.includes('This method only supports Buffer'))
}
})
})

describe('isValidSignature', function() {
Expand Down

0 comments on commit e221440

Please sign in to comment.