Skip to content

Commit

Permalink
refactor(crypto)!: rename messageToBinary to messageToHash adding has…
Browse files Browse the repository at this point in the history
…hing
  • Loading branch information
davidyuk committed Jun 16, 2021
1 parent 34288cb commit df37004
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/account/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import stampit from '@stamp/it'
import { required } from '@stamp/required'
import { hash, messageToBinary, decodeBase64Check, assertedType, verifyMessage as verifyMessageCrypto } from '../utils/crypto'
import { messageToHash, decodeBase64Check, assertedType, verifyMessage as verifyMessageCrypto } from '../utils/crypto'
import { buildTx, buildTxHash } from '../tx/builder'
import { decode } from '../tx/builder/helpers'
import { TX_TYPE } from '../tx/builder/schema'
Expand Down Expand Up @@ -78,7 +78,7 @@ async function signTransaction (tx, opt) {
* @return {String} Signature
*/
async function signMessage (message, opt = { returnHex: false }) {
const sig = await this.sign(hash(messageToBinary(message)), opt)
const sig = await this.sign(messageToHash(message), opt)
return opt.returnHex ? Buffer.from(sig).toString('hex') : sig
}

Expand Down
8 changes: 4 additions & 4 deletions src/utils/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,19 +328,19 @@ export function prepareTx (signature, data) {
return [Buffer.from([11]), Buffer.from([1]), [Buffer.from(signature)], data]
}

export function messageToBinary (message) {
export function messageToHash (message) {
const p = Buffer.from('aeternity Signed Message:\n', 'utf8')
const msg = Buffer.from(message, 'utf8')
if (msg.length >= 0xFD) throw new Error('message too long')
return Buffer.concat([Buffer.from([p.length]), p, Buffer.from([msg.length]), msg])
return hash(Buffer.concat([Buffer.from([p.length]), p, Buffer.from([msg.length]), msg]))
}

export function signMessage (message, privateKey) {
return sign(hash(messageToBinary(message)), privateKey)
return sign(messageToHash(message), privateKey)
}

export function verifyMessage (str, signature, publicKey) {
return verify(hash(messageToBinary(str)), signature, publicKey)
return verify(messageToHash(str), signature, publicKey)
}

/**
Expand Down
5 changes: 2 additions & 3 deletions test/integration/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { DRY_RUN_ACCOUNT } from '../../src/tx/builder/schema'
import * as R from 'ramda'
import { randomName } from '../utils'
import { decodeEvents, readType, SOPHIA_TYPES } from '../../src/contract/aci/transformation'
import { hash, messageToBinary } from '../../src/utils/crypto'
import { messageToHash } from '../../src/utils/crypto'
import { getFunctionACI } from '../../src/contract/aci/helpers'

const identityContract = `
Expand Down Expand Up @@ -295,8 +295,7 @@ describe('Contract', function () {
return contract.contractDeploy(code.bytecode, identityContract).should.eventually.have.property('address')
})
it('Verify message in Sophia', async () => {
const msg = messageToBinary('Hello')
const msgHash = hash(msg)
const msgHash = messageToHash('Hello')
const signature = await contract.sign(msgHash)
const signContract = await contract.getContractInstance(signSource)
await signContract.deploy()
Expand Down

0 comments on commit df37004

Please sign in to comment.