Last active
August 3, 2021 13:35
-
-
Save fbslo/8441fff0364c4a0f8aa4a2ac69ce9619 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const dhive = require('@hiveio/dhive'); | |
const { cryptoUtils, Signature } = dhive | |
const dhiveClient = new dhive.Client(['https://api.hive.blog'], { | |
chainId: 'beeab0de00000000000000000000000000000000000000000000000000000000', | |
}) | |
async function test(){ | |
let transaction = { | |
"ref_block_num":45115, | |
"ref_block_prefix":1373329981, | |
"expiration":"2021-08-03T12:43:45", | |
"operations":[ | |
["custom_json", | |
{"required_auths":[],"required_posting_auths":["yabapmatt"],"id":"matt-sm_find_match","json":"{\"match_type\":\"Practice\",\"app\":\"matt-steemmonsters/0.6.3\",\"n\":0}"}] | |
], | |
"extensions":[], | |
"signatures":["1f3eb2cce6b609259a867646198e75a72adafa54434c55f6f54420ec66331b81d5765ea0e8ba5ee4cfbefdda7e45cc5551e51142e2854cc2f3953fd4e14865d9a1"] | |
} | |
let signature = transaction.signatures[0] | |
//transaction must be JSON object | |
let publicKey = await recoverKeyFromSignature(transaction, signature) | |
console.log(publicKey.toString(), publicKey == 'STM5HCerA3vwtxkejz2PsGWzEkXDrCV46J8bvLX3WGBHdBnN17vnL') | |
} | |
test() | |
async function recoverKeyFromSignature(transaction, signature){ | |
try { | |
let msg = { | |
expiration: transaction.expiration, | |
extensions: transaction.extensions, | |
operations: transaction.operations, | |
ref_block_num: transaction.ref_block_num, | |
ref_block_prefix: transaction.ref_block_prefix | |
}; | |
//create signature object from string | |
let sig = Signature.fromString(signature); | |
let digest = cryptoUtils.transactionDigest(msg); | |
// Recover public key | |
let key = (new Signature(sig.data, sig.recovery)).recover(digest); | |
return key; | |
} catch (e) { | |
console.log(`Error: ${e}`) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment