Skip to content
This repository was archived by the owner on Aug 2, 2022. It is now read-only.

Fix broken key recovery for r1 signatures#731

Closed
jnordberg wants to merge 3 commits intoEOSIO:developfrom
jnordberg:recovery_fix
Closed

Fix broken key recovery for r1 signatures#731
jnordberg wants to merge 3 commits intoEOSIO:developfrom
jnordberg:recovery_fix

Conversation

@jnordberg
Copy link
Copy Markdown
Contributor

The recovery param was not extracted correctly causing all pubkey recovery attempts to fail or output the wrong pubkey for secp256r1 signatures.

Test case:

import {Signature} from 'eosjs/dist/Signature'
import {createHash} from 'crypto'

function sha256(buffer: Buffer) {
    let hash = createHash('sha256')
    hash.update(buffer)
    return hash.digest()
}

// real txn on chain, eos mainnet blk 123,803,753
let expectedId = '801048dfa7c17bf53dd80fca4c194901dadcba7399da8e5a7c66e71e9f57a13b'
let expectedPub = 'PUB_R1_5ie6pYwskXQY7VqbsRovPnfJweqPaaT97zdgYrAxuXUBdqadM6'
let packedTrx = '3165d55e13179b502978000000000100a6823403ea3055000000572d3ccdcd01104d76cca58c653400000000a8ed323225104d76cca58c653480b1915e5d268dca010000000000000004454f530000000004f09f8ea000'
let sigStr = 'SIG_R1_KHdJK7c5yuC7ouDywJ2U9Jm47RxY81P6PzF9U7N6iaQeeZY6mVUxR6sQTHTG4zaw4wvUCMxNdvNvQRj8TqtxyqYwEhQecm'

let txId = sha256(Buffer.from(packedTrx, 'hex')).toString('hex')

console.log('expected tx id', expectedId)
console.log('  actual tx id', txId)

let messageHex = 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906' + packedTrx + '0000000000000000000000000000000000000000000000000000000000000000'

let sig = Signature.fromString(sigStr)
let pub = sig.recover(messageHex, true, 'hex').toString()

console.log('expected pub', expectedPub)
console.log('  actual pub', pub)

@jlamarr22 jlamarr22 mentioned this pull request Jun 2, 2020
@jlamarr22
Copy link
Copy Markdown
Contributor

@jnordberg Thank you for the submission. These fixes look good. Since I want to be sure to add testing around this updated functionality (and have another code fix), I'm going to open another PR instead of merging this in, but will make sure you receive credit for it.

I'll be tracking the work #732

@jlamarr22 jlamarr22 closed this Jun 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants