_getSender.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* Import modules. */
  2. import { encodeAddress } from '@nexajs/address'
  3. import {
  4. ripemd160,
  5. sha256,
  6. } from '@nexajs/crypto'
  7. import { getTransaction } from '@nexajs/rostrum'
  8. import {
  9. encodeDataPush,
  10. OP,
  11. } from '@nexajs/script'
  12. import {
  13. binToHex,
  14. hexToBin,
  15. } from '@nexajs/utils'
  16. export default async (_unspent) => {
  17. // const pos = _unspent.pos
  18. const txid = _unspent.txid
  19. const tx = await getTransaction(txid)
  20. // console.log('TX', tx)
  21. const inputs = tx?.vin
  22. // console.log('INPUTS', inputs)
  23. const hex = inputs[0]?.scriptSig.hex
  24. // console.log('HEX', hex)
  25. const publicKey = hexToBin(hex.slice(4, 70))
  26. // console.log('PUBLIC KEY', binToHex(publicKey))
  27. /* Hash the public key hash according to the P2PKH/P2PKT scheme. */
  28. const scriptPushPubKey = encodeDataPush(publicKey)
  29. // console.log('SCRIPT PUSH PUBLIC KEY', scriptPushPubKey);
  30. const publicKeyHash = ripemd160(sha256(scriptPushPubKey))
  31. // console.log('PUBLIC KEY HASH (hex)', binToHex(publicKeyHash))
  32. const scriptPubKey = new Uint8Array([
  33. OP.ZERO,
  34. OP.ONE,
  35. ...encodeDataPush(publicKeyHash),
  36. ])
  37. // console.info('\n Script Public Key:', binToHex(scriptPubKey))
  38. const address = encodeAddress(
  39. 'nexa',
  40. 'TEMPLATE',
  41. scriptPubKey,
  42. )
  43. // console.info('ADDRESS', address)
  44. /* Set sender. */
  45. const sender = {
  46. address,
  47. inputs,
  48. }
  49. /* Return sender. */
  50. return sender
  51. }