Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example: How to create a txHex with DashTx + DashKeys #27

Open
coolaj86 opened this issue Oct 27, 2023 · 0 comments
Open

Example: How to create a txHex with DashTx + DashKeys #27

coolaj86 opened this issue Oct 27, 2023 · 0 comments

Comments

@coolaj86
Copy link
Member

DashSight is exclusively focused on interacting with an indexing and broadcasting web service.

In order to create the txHex you need to send via instantSend(txHex), you'll need to construct a transaction, sign it, and probably private key encoding/decoding along the way.

See the DashTx for Examples

Here's an example of that using Secp256k1 and DashTx:
(and you can use DashKeys for WIFs and such)

"use strict";

let DashTx = require("dashtx");

let dashTx = DashTx.create({
  version: 3,
  sign: signTx,
  toPublicKey: toPublicKey,
  addrToPubKeyHash: addrToPubKeyHash,
});

async function signTx({ privateKey, hash }) {
  let sigOpts = { canonical: true };
  let sigBuf = await Secp256k1.sign(hash, privateKey, sigOpts);
  return sigBuf;
}

async function toPublicKey(privKeyBuf) {
  let Secp256k1 = require("@dashincubator/secp256k1");
  let isCompressed = true;
  let pubBuf = Secp256k1.getPublicKey(privKeyBuf, isCompressed);
  return pubBuf;
}

async function addrToPubKeyHash(addr) {
  let Base58Check = require("@dashincubator/base58check").Base58Check;
  let b58c = Base58Check.create({
    pubKeyHashVersion: "4c",
    privateKeyVersion: "cc",
  });
  let parts = await b58c.verify(addr);
  return parts.pubKeyHash;
}

// keys that correspond to the available utxos
let privateKeys = {
  XmCyQ6qARLWXap74QubFMunngoiiA1QgCL: "YOUR_PRIVATE_KEY_HERE",
};

let coreUtxos = [
  {
    address: "XmCyQ6qARLWXap74QubFMunngoiiA1QgCL",
    outputIndex: 0,
    satoshis: 99809,
    script: "76a91473640d816ff4161d8c881da78983903bf9eba2d988ac",
    txId: "f92e66edc9c8da41de71073ef08d62c56f8752a3f4e29ced6c515e0b1c074a38",
  },
];

let payments = [
  { address: `Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx`, satoshis: 10000000 },
];

let txInfo = {
  inputs: coreUtxos,
  outputs: payments,
};

let keys = coreUtxos.map(function (utxo) {
  let privHex = privateKeys[utxo.address];
  let privBuf = Tx.utils.hexToU8(privHex);
  return privBuf;
});
let tx = dashTx.hashAndSignAll(txInfo, keys);

let txHex = tx.transaction;

let result = await dashsight.instantSend(txHex);

console.log(result);

Example transaction hex:

(inspect at https://live.blockcypher.com/dash/decodetx/)

030000000187ab81e88e2c19ca354f33f14d5b43b60d171ac851eb97dddd271b510cadbdb0000000
006b483045022100ec38c77b9f285d4c9aeeba36c1fac51bb88f7443185caf7eec21b170cc5d4062
0220098dcb5d90cb5f4ddc75ef54e2b2d1dbf220eb6fc28eed61c43192c0a420802c012103a6da86
f51829979a3c9f05251d9400d153111655526c6c25f8f82aba38b8a745ffffffff01188501000000
00001976a9149a00c2072c0209688cc6de5cc557af03e4f41b6388ac00000000

Example output:

{ "txid": "0f90cf5e03e8b8f8c4468f60fc8328cfcd5617fc2163f485fabfd227c692bf93" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant