Comparing version 0.1.13 to 0.1.14
@@ -29,2 +29,13 @@ import sb from 'satoshi-bitcoin' | ||
export const myAddresses = [ | ||
{ address: "N9vdDLkxTDtFn4GgtFnepu1AHDRN7TgfQ3", changeAddress: false}, | ||
{ address: "6NqAaYuRg39JLP44eWjonmsBKtNGgiwMHV", changeAddress: false}, | ||
{ address: "1NhoejoVqQfoenoXX3nAsVNG6YmW4VLk7q", changeAddress: false}, | ||
{ address: "1Mytu3cF8cJksMuTPX62HNHR1paarXwHZx", changeAddress: false}, | ||
{ address: "16boFTTPMAdpZjgSmYfEbrQdw4VCsjDqzj", changeAddress: false}, | ||
{ address: "13DkGY5g7GwBkUKqD6xwKWeD1ruzi1TzVs", changeAddress: false}, | ||
{ address: "NHxC3bjnmYE4HGwJCL2D56KuuCCpvtHUKZ", changeAddress: false}, | ||
{ address: "N5S2eekpmJ4eFBZxCRFfSyet4vqzRGdZjT", changeAddress: false}, | ||
{ address: "bc1q467w7gh3r658u0t4matwynlvtfaw83xshw4had",changeAddress: true }, | ||
{ address: "bc1q7vtcp3gas4k54y5xtmg2dl7dw599s4wdqha78y",changeAddress: false }, | ||
{ | ||
@@ -31,0 +42,0 @@ address: "N1XBwuQkaXr45pbo8HLRZNLEkgQnade7Mk", |
@@ -19,7 +19,3 @@ export const getAddressOfInput = (input) => { | ||
console.warn('input address',address) | ||
<<<<<<< HEAD | ||
return address | ||
======= | ||
return address | ||
>>>>>>> e619c70 (minor unfinished changes) | ||
} |
<<<<<<< HEAD | ||
const myAddresses = [ | ||
{ address: "NJHArPJUknmNBL42ns6k61XApnAYzrRkow", | ||
changeAddress: false | ||
}, | ||
{ | ||
address: "Mxo7UBaf2f2kH1aLUvHWY2o7k6K3fkCj4b", | ||
changeAddress: true, | ||
}, | ||
]; | ||
======= | ||
import {myAddresses} from './constants' | ||
>>>>>>> e619c70 (minor unfinished changes) | ||
@@ -16,0 +4,0 @@ export const isOurAddress = (address) => { |
<<<<<<< HEAD | ||
const myAddresses = [ | ||
{ address: "NJHArPJUknmNBL42ns6k61XApnAYzrRkow", | ||
changeAddress: false | ||
}, | ||
{ | ||
address: "Mxo7UBaf2f2kH1aLUvHWY2o7k6K3fkCj4b", | ||
changeAddress: true, | ||
}, | ||
]; | ||
======= | ||
import {myAddresses} from './constants' | ||
>>>>>>> e619c70 (minor unfinished changes) | ||
@@ -16,0 +4,0 @@ export const isOurChangeAddress = (address) => { |
@@ -1,26 +0,1 @@ | ||
<<<<<<< HEAD | ||
import getUrl from "./getUrl"; | ||
export async function listTransactions(address, options) { | ||
let url = getUrl() + "/api/v1/listtransactions?address=" + address; | ||
if (options && options.rescan) url += "&rescan=true" //in case we are restoring a value we want to rescan the blockchhain - since it could be new server! | ||
let json | ||
try { | ||
let response | ||
let text | ||
if (typeof fetch === "function") response = await fetch(url); | ||
else { | ||
const fetch = require('node-fetch'); | ||
response = await fetch(url); | ||
} | ||
text = await response.text() | ||
json = JSON.parse(text) | ||
if (json.status !== 'success') throw "Cannot get transactions from dApp-Url " + url | ||
} catch (e) { | ||
console.log(e) | ||
// throw "Cannot get transactions from dApp-Url " + url | ||
} | ||
return json | ||
======= | ||
const bitcoin = require('bitcoinjs-lib') | ||
@@ -70,2 +45,5 @@ const ElectrumClient = require('@codewarriorr/electrum-client-js') | ||
//if this is a p2pkh | ||
let script = bitcoin.address.toOutputScript(address, network) | ||
@@ -132,4 +110,3 @@ let hash = bitcoin.crypto.sha256(script) | ||
satoshi: isOurInput ? out.value * -1 : out.value, | ||
value: isOurInput ? | ||
1e-8 * out.value * -1 : 1e-8 * out.value, | ||
value: isOurInput ? 1e-8 * out.value * -1 : 1e-8 * out.value, | ||
n: n, | ||
@@ -142,3 +119,3 @@ category: isOurInput ? "sent" : "received", | ||
} | ||
if(isOurAddress(address) && !isOurChangeAddress(address)) result.push(vout) | ||
if(isOurAddress(address) && !isOurChangeAddress(address)) result.push(vout) | ||
// if (isOurAddress(address) && isOurInput) result.push(vout) | ||
@@ -165,3 +142,2 @@ // if (isOurAddress(address) && !isOurInput) result.push(vout) //this is not our input, it's received | ||
return result; | ||
>>>>>>> e619c70 (minor unfinished changes) | ||
} |
const bitcoin = require('bitcoinjs-lib') | ||
const ElectrumClient = require('@codewarriorr/electrum-client-js') | ||
import {getElectrumClient} from "./network" | ||
import settings from "./settings" | ||
import {isOurAddress} from "./isOurAddress"; | ||
import {isOurChangeAddress} from "./isOurChangeAddress"; | ||
import { DOICHAIN,DEFAULT_NETWORK } from './network'; | ||
/** | ||
* TODO Please consolidate this Method with listTransactions which supports name_doi transactions | ||
* but this one supports bech32 addresses! | ||
* | ||
* @param {*} address | ||
* @param {*} network | ||
*/ | ||
export async function listTransactionsElectrum(address, o_options) { | ||
export async function listTransactionsElectrum(address, DOICHAIN) { | ||
let options = {} | ||
if(o_options===undefined || o_options.network===undefined) | ||
options.network=DOICHAIN | ||
else options.network = o_options.network | ||
if(o_options===undefined || o_options.settings===undefined) | ||
options.settings=settings | ||
else options.settings = o_options.settings | ||
let script = bitcoin.address.toOutputScript(address, DOICHAIN) | ||
let hash = bitcoin.crypto.sha256(script) | ||
let reversedHash = new Buffer(hash.reverse()) | ||
console.log(address, ' maps to ', reversedHash.toString('hex')) | ||
let hash = bitcoin.crypto.sha256(script) | ||
let reversedHash = Buffer.from(hash.reverse()) | ||
const client = new ElectrumClient("demo30122020.doi.works", 50002, "tls"); | ||
const client = getElectrumClient(options.settings) | ||
let result = []; | ||
try { | ||
await client.connect( | ||
@@ -34,9 +53,10 @@ "electrum-client-js", // optional client name | ||
history.forEach(async (tx) => { | ||
let i = 0 | ||
for (const tx of history){ | ||
const transaction = await client.blockchain_transaction_get( | ||
tx.tx_hash | ||
); | ||
) | ||
const decryptedTx = bitcoin.Transaction.fromHex(transaction); | ||
// console.log(decryptedTx) | ||
//check all inputs and check if the address is ours or not | ||
@@ -46,11 +66,19 @@ let isOurInput | ||
//find the address of the input tx | ||
const chunks = bitcoin.script.decompile( | ||
new Buffer(input.script, "hex") | ||
); | ||
const dec = bitcoin.script.toASM(chunks).split(" ")[1]; | ||
const address = bitcoin.payments.p2pkh({ | ||
pubkey: new Buffer(dec, "hex"), | ||
network: DOICHAIN, | ||
}).address; | ||
if(isOurAddress(address)) isOurInput=true | ||
if(!input.witness || input.witness.length===0){ //Doichain as the attribute wittness with an empty array | ||
const chunks = bitcoin.script.decompile(Buffer.from(input.script, "hex")) | ||
const dec = bitcoin.script.toASM(chunks).split(" ")[1]; | ||
const address = bitcoin.payments.p2pkh({pubkey: Buffer.from(dec, "hex"),network: options.network, | ||
}).address; | ||
if(isOurAddress(address)) isOurInput=true | ||
}else{ | ||
console.log(input.witness) | ||
let witness = [ | ||
Buffer.from(input.witness[0], 'hex'), | ||
Buffer.from(input.witness[1], 'hex') | ||
] | ||
let obj = bitcoin.payments.p2wpkh({ witness }) | ||
console.info('is segwit bech32 address',obj.address) | ||
if(isOurAddress(obj.address)) isOurInput=true | ||
} | ||
}); | ||
@@ -62,8 +90,30 @@ | ||
decryptedTx.outs.forEach(function (out, n) { | ||
const address = bitcoin.address.fromOutputScript( | ||
out.script, | ||
DOICHAIN | ||
); | ||
const vout = { | ||
let address, nameId, nameValue | ||
let vout | ||
const asm = bitcoin.script.toASM(out.script) | ||
const asmParts = asm.split(" ") | ||
//in case this is a name_op (e.g. OP_10 transaction this script will not work - no chance getting the address | ||
//we don't see any results printed even tho we expect received and sent transactions - what is the reason here | ||
if (asmParts[0] !== 'OP_10') { | ||
address = bitcoin.address.fromOutputScript(out.script, options.network) | ||
console.log('address', address) | ||
} else { | ||
const chunks = bitcoin.script.decompile(out.script) | ||
console.log(chunks) | ||
nameId = Buffer.from(chunks[1]).toString() | ||
//we had an int 0 here insead of a buffer | ||
if(chunks[2]!==0) | ||
nameValue = Buffer.from(chunks[2]).toString() | ||
else nameValue = "" | ||
console.log(network) | ||
address = bitcoin.address.toBase58Check(chunks[7], options.network.scriptHash) | ||
} | ||
console.log('name_op nameId', nameId) | ||
console.log('name_op nameValue', nameValue) | ||
console.log('name_op address', address) | ||
vout = { | ||
txid: decryptedTx.getId(), | ||
@@ -81,10 +131,15 @@ satoshi: isOurInput ? out.value * -1 : out.value, | ||
}, | ||
createdAtTime: format_time(decriptedHeader.timestamp) | ||
}; | ||
if(isOurAddress(address) && !isOurInput)result.push(vout); //this is not our input, it's received | ||
if(!isOurAddress(address)&& !isOurChangeAddress(address) && isOurInput)result.push(vout); // is our input, it's sent | ||
timestamp:decriptedHeader.timestamp, | ||
createdAtTime: format_time(decriptedHeader.timestamp), | ||
timestamp: decriptedHeader.timestamp | ||
} | ||
if(nameId) vout.nameId = nameId | ||
if(nameValue) vout.nameValue = nameValue | ||
// console.log(vout) | ||
if(isOurAddress(address) && !isOurChangeAddress(address)) result.push(vout) | ||
}); | ||
}); | ||
} | ||
const balance = await client.blockchain_scripthash_getBalance( | ||
/* const balance = await client.blockchain_scripthash_getBalance( | ||
reversedHash.toString("hex") | ||
@@ -96,5 +151,5 @@ ); | ||
); | ||
console.log(unspent); | ||
console.log(unspent); */ | ||
await client.close(); | ||
// await client.close(); | ||
} catch (err) { | ||
@@ -101,0 +156,0 @@ console.error(err); |
@@ -96,10 +96,10 @@ import settings from './settings' | ||
export const getElectrumClient = (setSettings=[]) => { | ||
export const getElectrumClient = (setSettings = []) => { | ||
settings.setSettings(setSettings) | ||
return new ElectrumClient( | ||
settings.electrumHost, | ||
settings.electrumPort, | ||
settings.electrumSSL | ||
) | ||
return new ElectrumClient( | ||
settings.electrumHost, | ||
settings.electrumPort, | ||
settings.electrumSSL | ||
) | ||
} |
var settings = { | ||
testnet: true, | ||
@@ -7,2 +8,3 @@ ssl: true, | ||
host: "doichain-testnet.le-space.de", | ||
electrumHost: "btcpay.doi.works", | ||
@@ -9,0 +11,0 @@ electrumPort: "50002", |
{ | ||
"name": "doichain", | ||
"version": "0.1.13", | ||
"version": "0.1.14", | ||
"description": "A js-Doichain library. The goal is to fully cover the Doichain protocol", | ||
@@ -40,4 +40,4 @@ "main": "index.js", | ||
"bip39": "^3.0.2", | ||
"bitcoinjs-lib": "^5.1.8", | ||
"bitcoinjs-message": "^2.1.0", | ||
"bitcoinjs-lib": "5.2.0", | ||
"bitcoinjs-message": "2.2.0", | ||
"browser-crypto": "^1.0.1", | ||
@@ -49,3 +49,3 @@ "bs58": "^4.0.1", | ||
"hdkey": "^1.1.1", | ||
"node-fetch": "^2.6.0", | ||
"node-fetch": "^2.6.1", | ||
"satoshi-bitcoin": "^1.0.4", | ||
@@ -52,0 +52,0 @@ "secp256k1": "^3.7.1" |
@@ -14,2 +14,3 @@ import chai from 'chai' | ||
}); | ||
import {generateMnemonic} from '../lib/generateMnemonic' | ||
@@ -20,3 +21,2 @@ import {validateMnemonic} from "../lib/validateMnemonic"; | ||
import {getAddress} from "../lib/getAddress" | ||
import {changeNetwork, DEFAULT_NETWORK, DOICHAIN_REGTEST, DOICHAIN_TESTNET, DOICHAIN} from "../lib/network" | ||
import {fundWallet} from "../lib/fundWallet"; | ||
@@ -40,2 +40,4 @@ import {listTransactions} from "../lib/listTransactions" | ||
import createAndSendTransaction from "../lib/createAndSendTransaction" | ||
import { listTransactionsElectrum } from '../lib/listTransactionsElectrum'; | ||
import { DOICHAIN } from '../lib/network'; | ||
@@ -58,25 +60,30 @@ const MNEMONIC = "refuse brush romance together undo document tortoise life equal trash sun ask" | ||
it.only('should get a transactions input', async function () { | ||
const ElectrumClient = require('@codewarriorr/electrum-client-js') | ||
const client = new ElectrumClient("demo30122020.doi.works", 50002, "tls"); | ||
await client.connect( | ||
"electrum-client-js", // optional client name | ||
"1.4.2" // optional protocol version | ||
) | ||
<<<<<<< HEAD | ||
const history = await client.blockchain_scripthash_getHistory( | ||
reversedHash.toString("hex") | ||
) | ||
======= | ||
>>>>>>> e619c70 (minor unfinished changes) | ||
const txid = "bed2d384dc8a304ab20f4e1282958171052fbc4265fefae7d9231b491c5216a4" | ||
const transaction = await client.blockchain_transaction_get(txid); | ||
const decryptedTx = bitcoin.Transaction.fromHex(transaction); | ||
console.log("decrypted tx", decryptedTx) | ||
const address = "NHxC3bjnmYE4HGwJCL2D56KuuCCpvtHUKZ" //NHxC3bjnmYE4HGwJCL2D56KuuCCpvtHUKZ | ||
const settings = { | ||
electrumHost: "demo30122020.doi.works", | ||
electrumPort: "50002", | ||
electrumSSL: "tls" | ||
} | ||
const options = { network: DOICHAIN, settings } | ||
let listDOITransactionElectrum = await listTransactionsElectrum(address,options) | ||
console.log("listDOITransactionElectrum", listDOITransactionElectrum) | ||
chai.assert.equal(listDOITransactionElectrum.length, 3, 'in the past we had here always 3 output '+address) | ||
//contains name_doi transactions! | ||
/* const address2 = "NHxC3bjnmYE4HGwJCL2D56KuuCCpvtHUKZ" ; | ||
const options2 = { network: DOICHAIN, settings } | ||
let listDOITransactionElectrum2 = await listTransactionsElectrum(address2,options2) | ||
console.log("listDOITransactionElectrum", listDOITransactionElectrum2) | ||
chai.assert.equal(listDOITransactionElectrum2.length, 3, 'in the past we had here always 3 outputs '+address2)*/ | ||
}) | ||
it('should log listtransactionsElectrum content', async function () { | ||
//const address = "NJHArPJUknmNBL42ns6k61XApnAYzrRkow" | ||
const address = "bc1q7vtcp3gas4k54y5xtmg2dl7dw599s4wdqha78y" | ||
let listTransactionElectrum = await listTransactions(address, DOICHAIN) | ||
console.log(listTransactionElectrum) | ||
let listBTCTransactionElectrum = await listTransactionsElectrum(address,{network:bitcoin.networks.bitcoin}) | ||
//console.log(listTransactionElectrum[0]) | ||
chai.assert.equal(listBTCTransactionElectrum.length, 1, 'in the past we had here always 1 output') | ||
//chai.assert.equal(listTransactionElectrum[0].satoshi, 16393033, 'that is the change money') | ||
chai.assert.equal(listBTCTransactionElectrum[0].satoshi, 88134, 'this is a btcpay server payment for a p2pool node') | ||
}) | ||
@@ -86,3 +93,3 @@ | ||
const mnemonic = generateMnemonic() | ||
chai.assert.equal(mnemonic.split(' ').length, 12, 'mnemonic doesnt contain 12 words') | ||
chai.assert.equal(mnemonic.split(' ').length, 12, 'mnemonic doesn´t contain 12 words') | ||
}) | ||
@@ -89,0 +96,0 @@ |
85812
1782
16
Updatedbitcoinjs-lib@5.2.0
Updatedbitcoinjs-message@2.2.0
Updatednode-fetch@^2.6.1