Comparing version 1.4.0 to 1.5.0
@@ -10,5 +10,2 @@ #!/usr/bin/env node | ||
let Dashcore = require("@dashevo/dashcore-lib"); | ||
let Transaction = Dashcore.Transaction; | ||
let dashsightBaseUrl = | ||
@@ -21,8 +18,83 @@ process.env.DASHSIGHT_BASE_URL || "https://insight.dash.org/insight-api"; | ||
insightBaseUrl: dashsightBaseUrl, | ||
dashsightBaseUrl: dashsightBaseUrl, | ||
dashsightBaseUrl: "", // not needed here | ||
dashsocketBaseUrl: "", // not needed here | ||
}); | ||
let Pub = require("./_wif-to-addr.js"); | ||
let Base58Check = require("@dashincubator/base58check").Base58Check; | ||
let b58c = Base58Check.create({ | ||
pubKeyHashVersion: "4c", | ||
privateKeyVersion: "cc", | ||
}); | ||
let DashTx = require("dashtx"); | ||
let RIPEMD160 = require("@dashincubator/ripemd160"); | ||
let Secp256k1 = require("@dashincubator/secp256k1"); | ||
//@ts-ignore | ||
let Crypto = exports.crypto || require("../shims/crypto-node.js"); | ||
/** | ||
* @param {import('dashtx').TxSignOpts} opts | ||
*/ | ||
async function signTx({ privateKey, hash }) { | ||
let sigOpts = { canonical: true }; | ||
let sigBuf = await Secp256k1.sign(hash, privateKey, sigOpts); | ||
return sigBuf; | ||
} | ||
/** | ||
* @param {import('dashtx').TxPrivateKey} privBuf | ||
*/ | ||
function toPublicKey(privBuf) { | ||
let isCompressed = true; | ||
let pubBuf = Secp256k1.getPublicKey(privBuf, isCompressed); | ||
return pubBuf; | ||
} | ||
/** | ||
* @param {import('dashtx').TxPublicKey} pubBuf | ||
*/ | ||
async function hashPublicKey(pubBuf) { | ||
let sha = await Crypto.subtle.digest("SHA-256", pubBuf); | ||
let shaU8 = new Uint8Array(sha); | ||
let ripemd = RIPEMD160.create(); | ||
let hash = ripemd.update(shaU8); | ||
let pkh = hash.digest("hex"); | ||
return pkh; | ||
} | ||
/** | ||
* @param {String} wif | ||
* @returns {Promise<Uint8Array>} | ||
*/ | ||
async function wifToPrivateKey(wif) { | ||
let parts = await b58c.verify(wif); | ||
let privBuf = Buffer.from(parts.privateKey, "hex"); | ||
return privBuf; | ||
} | ||
/** | ||
* @param {String} wif | ||
* @returns {Promise<String>} | ||
*/ | ||
async function wifToAddr(wif) { | ||
let parts = await b58c.verify(wif); | ||
let privBuf = Buffer.from(parts.privateKey, "hex"); | ||
let isCompressed = true; | ||
let pubBuf = Secp256k1.getPublicKey(privBuf, isCompressed); | ||
let pubKeyHash = await hashPublicKey(pubBuf); | ||
let addr = await b58c.encode({ | ||
version: "4c", | ||
pubKeyHash: pubKeyHash, | ||
}); | ||
return addr; | ||
} | ||
/** | ||
* @param {String} addr - base58check | ||
* @returns {Promise<String>} - pubKeyHash as hex | ||
*/ | ||
async function addrToPubKeyHash(addr) { | ||
let parts = await b58c.verify(addr); | ||
return parts.pubKeyHash; | ||
} | ||
async function main() { | ||
@@ -32,3 +104,3 @@ let addrs = process.argv.slice(2); | ||
if (2 !== addrs.length) { | ||
console.error(`Usage: create-tx <wif> <payaddr>`); | ||
console.error(`Usage: create-tx <wif> <pay-addr>`); | ||
process.exit(1); | ||
@@ -39,3 +111,3 @@ return; | ||
let wifpath = addrs[0]; | ||
let payaddr = addrs[1]; | ||
let payAddr = addrs[1]; | ||
@@ -47,9 +119,10 @@ console.info(); | ||
let source = await Pub.wifToAddr(wif); | ||
let source = await wifToAddr(wif); | ||
console.info(`Source: ${source}`); | ||
console.info(`Destination: ${payaddr}`); | ||
console.info(`Destination: ${payAddr}`); | ||
console.info(); | ||
let utxos = await dashsight.getUtxos(source); | ||
let hasUtxos = utxos.length > 0; | ||
let insightUtxos = await dashsight.getUtxos(source); | ||
let coreUtxos = Dashsight.toCoreUtxos(insightUtxos); | ||
let hasUtxos = coreUtxos.length > 0; | ||
if (!hasUtxos) { | ||
@@ -61,17 +134,11 @@ console.error(`'${source}' is completely spent`); | ||
let utxo = utxos[0]; | ||
let duffs = toDuffs(utxo.satoshis); | ||
let dash = toDash(utxo.satoshis); | ||
let coreUtxo = coreUtxos[0]; | ||
let key = await wifToPrivateKey(wif); | ||
let duffs = toDuffs(coreUtxo.satoshis); | ||
let dash = toDash(coreUtxo.satoshis); | ||
console.info(`First Unspent Tx: ${dash} (${duffs})`); | ||
let coreUtxo = { | ||
txId: utxo.txid, | ||
outputIndex: utxo.vout, | ||
address: utxo.address, | ||
script: utxo.scriptPubKey, | ||
satoshis: utxo.satoshis, | ||
}; | ||
// max bytes for single tx with both bigint pads is 193 | ||
let fee = 193; | ||
let fee = 191; | ||
let feeDash = toDash(fee); | ||
@@ -81,16 +148,40 @@ let feeDuffs = toDuffs(fee); | ||
let amount = coreUtxo.satoshis - fee; | ||
let amountDash = toDash(amount); | ||
let amountDuffs = toDuffs(amount); | ||
let units = coreUtxo.satoshis - fee; | ||
let amountDash = toDash(units); | ||
let amountDuffs = toDuffs(units); | ||
console.info(`Payment Amount: ${amountDash} (${amountDuffs})`); | ||
//@ts-ignore - no input required, actually | ||
let tx = new Transaction() | ||
//@ts-ignore - allows single value or array | ||
.from([coreUtxo]); | ||
tx.to(payaddr, amount); | ||
tx.fee(fee); | ||
tx.sign(wif); | ||
let keys = [key]; | ||
let inputs = [coreUtxo]; | ||
let pubKeyHash = await addrToPubKeyHash(payAddr); | ||
let outputs = [{ pubKeyHash, units }]; | ||
let txInfo = { | ||
version: 3, // (will be) optional | ||
inputs: inputs, | ||
outputs: outputs, | ||
// TODO any sort of minimum fee guessing? | ||
locktime: 0, // optional | ||
}; | ||
let txHex = tx.toString(); | ||
let dashTx = DashTx.create({ | ||
version: 3, | ||
//@ts-ignore | ||
sign: signTx, | ||
//@ts-ignore | ||
getPrivateKey: async function (txInput, i) { | ||
let privKey = keys[i]; | ||
return privKey; | ||
}, | ||
//@ts-ignore | ||
getPublicKey: async function (txInput, i) { | ||
let privKey = keys[i]; | ||
let pubKey = toPublicKey(privKey); | ||
return pubKey; | ||
}, | ||
}); | ||
//@ts-ignore | ||
let txInfoSigned = await dashTx.hashAndSignAll(txInfo, keys); | ||
let txHex = txInfoSigned.transaction.toString(); | ||
console.info(); | ||
@@ -97,0 +188,0 @@ console.info(txHex); |
@@ -11,8 +11,8 @@ (function (exports) { | ||
*/ | ||
const defaultOpts = { | ||
let defaultOpts = { | ||
headers: { | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json' | ||
Accept: "application/json", | ||
"Content-Type": "application/json", | ||
}, | ||
} | ||
}; | ||
@@ -165,3 +165,4 @@ const DUFFS = 100000000; | ||
let nextResp = await Dashsight.fetch( | ||
`${insightBaseUrl}/txs?address=${addr}&pageNum=${cursor}`); | ||
`${insightBaseUrl}/txs?address=${addr}&pageNum=${cursor}`, | ||
); | ||
nextResp = await nextResp.json(); | ||
@@ -187,3 +188,3 @@ // Note: this could still be wrong, but I don't think we have | ||
rawtx: hexTx, | ||
} | ||
}, | ||
}); | ||
@@ -208,9 +209,8 @@ if (!txResp.ok) { | ||
/** @type {ToCoreUtxo} */ | ||
insight.toCoreUtxo = Dashsight.toCoreUtxo; | ||
/** @type {ToCoreUtxos} */ | ||
insight.toCoreUtxos = function (insightUtxos) { | ||
let coreUtxos = insightUtxos.map(insight.toCoreUtxo); | ||
insight.toCoreUtxos = Dashsight.toCoreUtxos; | ||
return coreUtxos; | ||
}; | ||
/** | ||
@@ -294,5 +294,5 @@ * Handles UTXOs that have NO MORE THAN ONE page of transactions | ||
Dashsight.fetch = async function dashfetch(url, opts) { | ||
opts = Object.assign({}, defaultOpts, opts) | ||
opts = Object.assign({}, defaultOpts, opts); | ||
if (opts.body) { | ||
opts.body = JSON.stringify(opts.body) | ||
opts.body = JSON.stringify(opts.body); | ||
} | ||
@@ -311,4 +311,22 @@ | ||
throw err; | ||
} | ||
}; | ||
/** @type {ToCoreUtxo} */ | ||
Dashsight.toCoreUtxo = function (utxo) { | ||
return { | ||
txId: utxo.txid, | ||
outputIndex: utxo.vout, | ||
address: utxo.address, | ||
script: utxo.scriptPubKey, | ||
satoshis: utxo.satoshis, | ||
}; | ||
}; | ||
/** @type {ToCoreUtxos} */ | ||
Dashsight.toCoreUtxos = function (insightUtxos) { | ||
let coreUtxos = insightUtxos.map(Dashsight.toCoreUtxo); | ||
return coreUtxos; | ||
}; | ||
if ("undefined" !== typeof module) { | ||
@@ -318,4 +336,5 @@ module.exports.Dashsight = Dashsight; | ||
module.exports.Dashfetch = Dashsight.fetch; | ||
module.exports.toCoreUtxo = Dashsight.toCoreUtxo; | ||
module.exports.toCoreUtxos = Dashsight.toCoreUtxos; | ||
} | ||
})(("undefined" !== typeof module && module.exports) || window); |
@@ -204,3 +204,3 @@ (function (exports) { | ||
let pingTimeout | ||
let pingTimeout; | ||
let ws = await Eio3.connectWs(session.sid); | ||
@@ -275,3 +275,3 @@ wsc._ws = ws; | ||
ws.removeEventListener("message", _onMessage); | ||
clearTimeout(ws._pingTimeout) | ||
clearTimeout(ws._pingTimeout); | ||
if (debug) { | ||
@@ -278,0 +278,0 @@ console.debug("WebSocket Close"); |
{ | ||
"name": "dashsight", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "SDK for Dash's flavor of the Insight API", | ||
"main": "index.js", | ||
"browser": { | ||
"./ws/index.js": "./dashsocket.js" | ||
"./ws/index.js": "./dashsocket.js", | ||
"crypto": false | ||
}, | ||
@@ -55,11 +56,10 @@ "bin": { | ||
"dependencies": { | ||
"@root/request": "^1.9.2" | ||
"dashtx": "^0.9.0-3" | ||
}, | ||
"devDependencies": { | ||
"@dashevo/dashcore-lib": "^0.19.44", | ||
"@root/base58check": "^1.0.0", | ||
"@types/tough-cookie": "^4.0.2", | ||
"ripemd160": "^2.0.2", | ||
"secp256k1": "^4.0.3" | ||
"@dashincubator/base58check": "^1.3.1", | ||
"@dashincubator/ripemd160": "^2.3.0", | ||
"@dashincubator/secp256k1": "^1.7.1-1", | ||
"@types/tough-cookie": "^4.0.2" | ||
} | ||
} |
@@ -21,3 +21,4 @@ # [dashsight.js](https://github.com/dashhive/dashsight.js) | ||
let dashsightBaseUrl = | ||
process.env.DASHSIGHT_BASE_URL || "https://dashsight.dashincubator.dev/insight-api"; | ||
process.env.DASHSIGHT_BASE_URL || | ||
"https://dashsight.dashincubator.dev/insight-api"; | ||
let dashsocketBaseUrl = | ||
@@ -263,3 +264,4 @@ process.env.DASHSOCKET_BASE_URL || "https://insight.dash.org/socket.io"; | ||
Gets all unspent transaction outputs (the usable "coins") for the given address, | ||
including all information needed by `dashcore-lib.Transaction`. | ||
including all information needed by `DashTx#hashAndSignAll()` (and | ||
`dashcore-lib.Transaction`). | ||
@@ -302,5 +304,41 @@ ```js | ||
```js | ||
let Dashcore = require("@dashevo/dashcore-lib"); | ||
let Transaction = Dashcore.Transaction; | ||
"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 = [ | ||
@@ -320,14 +358,15 @@ { | ||
let privateKeys = [ | ||
// keys that correspond to the available utxos | ||
"YOUR_KEY_HERE", | ||
]; | ||
let txInfo = { | ||
inputs: coreUtxos, | ||
outputs: payments, | ||
}; | ||
let tx = new Transaction(); | ||
tx.from(coreUtxos); | ||
tx.to(payments); | ||
tx.change(changeAddr); | ||
tx.sign(privateKeys); | ||
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.serialize(); | ||
let txHex = tx.transaction; | ||
@@ -334,0 +373,0 @@ let result = await dashsight.instantSend(txHex); |
@@ -99,4 +99,3 @@ "use strict"; | ||
let cookies = await cookieStore.get(subUrl); | ||
let subResp = await fetch(subUrl, | ||
{ | ||
let subResp = await fetch(subUrl, { | ||
//agent: httpAgent, | ||
@@ -103,0 +102,0 @@ method: "POST", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
71050
4
2104
391
+ Addeddashtx@^0.9.0-3
+ Addeddashtx@0.9.0(transitive)
- Removed@root/request@^1.9.2
- Removed@root/request@1.9.2(transitive)