Skip to content

Commit

Permalink
add PublicSigningKey.fromSignedEthereumMessage fn
Browse files Browse the repository at this point in the history
  • Loading branch information
yusefnapora committed Jan 18, 2017
1 parent 3edfe39 commit 36a5ca1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/peer/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ class PublicSigningKey {
}
}

static fromSignedEthereumMessage (message: Buffer, signature: Buffer, ethereumAddress: Buffer | string): PublicSigningKey {
const pubKey = recoverEthereumPubKey(message, signature)
const addrForSig = ethereumUtils.pubToAddress(pubKey, true)
const expectedAddr = ethereumUtils.toBuffer(ethereumAddress)
if (!expectedAddr.equals(addrForSig)) {
throw new Error(`Public key for signature does not match expected ethereum address ${expectedAddr.toString('hex')}`)
}
const p2pKey = new Secp256k1PublicKey(pubKey)
return new PublicSigningKey(p2pKey)
}

get isSecp256k1 () {
return (this._key instanceof Secp256k1PublicKey)
}
Expand Down
7 changes: 7 additions & 0 deletions test/signature_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,11 @@ describe('Signature verification', () => {
assert(valid, 'signature verification failed')
})
})

it('recovers a PublicSigningKey from a signed ethereum message and account address', () => {
const {message, signature, ethAccount, ethPubKey} = ETH_MESSAGE_FIXTURES
const pubKey = PublicSigningKey.fromSignedEthereumMessage(message, signature, ethAccount)
const keyFromHex = PublicSigningKey.fromBytes(ethPubKey)
assert.deepEqual(pubKey.bytes, keyFromHex.bytes, 'public key recovery failed')
})
})

0 comments on commit 36a5ca1

Please sign in to comment.