Skip to content

Commit

Permalink
fix(messageToHash): support messages longer than 252 chars
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Apr 25, 2022
1 parent 22ee323 commit b4aa456
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"swagger-client": "^3.18.4",
"tweetnacl": "^1.0.3",
"tweetnacl-auth": "^1.0.1",
"varuint-bitcoin": "^1.1.2",
"websocket": "^1.0.34"
},
"repository": {
Expand Down
5 changes: 2 additions & 3 deletions src/utils/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import nacl, { SignKeyPair } from 'tweetnacl'
import aesjs from 'aes-js'
import { blake2b, Data } from 'blakejs'
import { encode as varuintEncode } from 'varuint-bitcoin'

import { str2buf } from './bytes'
import { encode, decode, sha256hash } from './encoder'
import { NotImplementedError } from './errors'

export { sha256hash }

Expand Down Expand Up @@ -206,8 +206,7 @@ export function verify (
export function messageToHash (message: string): Buffer {
const p = Buffer.from('aeternity Signed Message:\n', 'utf8')
const msg = Buffer.from(message, 'utf8')
if (msg.length >= 0xFD) throw new NotImplementedError('Message too long')
return hash(Buffer.concat([Buffer.from([p.length]), p, Buffer.from([msg.length]), msg]))
return hash(Buffer.concat([varuintEncode(p.length), p, varuintEncode(msg.length), msg]))
}

export function signMessage (message: string, privateKey: string | Buffer): Uint8Array {
Expand Down
6 changes: 6 additions & 0 deletions test/unit/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ describe('crypto', () => {
const messageNonASCIISignatureAsHex = 'faa1bdb8a88c529be904036382705ed207bbdde00ece3bdb541f5986d57aebe7babe315a4d95f5882165c28bf41f6149430509ded1cc7dcd9b134e0e1d73cd0b'
const messageNonASCIISignature = Buffer.from(messageNonASCIISignatureAsHex, 'hex')

const longMessage = 'test'.repeat(256)
const longMessageHash = Buffer.from('J9bibOHrlicf0tYQxe1lW69LdDAxETwPmrafKjjQwvs=', 'base64')

it('calculates a hash of a long message', () =>
expect(Crypto.messageToHash(longMessage)).to.eql(longMessageHash))

describe('sign', () => {
it('should produce correct signature of message', () => {
const s = Crypto.signMessage(message, privateKey)
Expand Down

0 comments on commit b4aa456

Please sign in to comment.