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

Init #1113

Closed
wants to merge 13 commits into from
Prev Previous commit
fix: 程序优化
  • Loading branch information
f5i23q999d committed Dec 8, 2022
commit ea6c7fa08d0f70c91b30aab2626493f9a6de3e08
5 changes: 4 additions & 1 deletion bin/node
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ if (process.argv.indexOf('--version') !== -1
const FullNode = require('../lib/node/fullnode');

const node = new FullNode({
network : 'regtest',
file: true,
argv: true,
env: true,
Expand All @@ -31,7 +32,9 @@ const node = new FullNode({
memory: false,
workers: true,
listen: true,
loader: require
loader: require,
indexTX: true,
indexAddress: true,
});

// Temporary hack
Expand Down
File renamed without changes.
9 changes: 9 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {

network : 'regtest',
clientPort : 8332,
client_apiKey: 'bikeshed',
walletPort : 48334,
wallet_apiKey: 'bikeshed',

}
193 changes: 129 additions & 64 deletions services/api/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,27 @@
const express = require('express');
const NodeClient = require('../../../lib/client/node');
const Network = require('../../../lib/protocol/network');
const network = Network.get('regtest');

const config = require('../../../config')
const network = Network.get(config.network);
const WalletClient = require('../../../lib/client/wallet');
const KeyRing = require('../../../lib/primitives/keyring');
const WalletDB = require('../../../lib/wallet/walletdb');





const clientOptions = {
network: network.type,
port: 8332,
apiKey: 'bikeshed'
port: config.clientPort,
apiKey: config.client_apiKey
}
const client = new NodeClient(clientOptions);
const walletOptions = {
network: network.type,
port: 48334,
apiKey: 'bikeshed'
port: config.walletPort,
apiKey: config.wallet_apiKey
}

const walletClient = new WalletClient(walletOptions);

async function getReceiveAddress(pub) {
const wdb = new WalletDB({ network: 'regtest' });
await wdb.open();
const walletdb = await wdb.create({
watchOnly: true,
accountKey: pub,
witness: false
});
const account = await walletdb.getAccount(0);
const recAddr = account.toJSON().receiveAddress;
await wdb.close();
return recAddr;
}



class Router {
Expand All @@ -66,17 +50,17 @@ class Router {
.get('/tx/address/:address', this.getTxByAddress)
.get('/api/v2/xpub/:xpubkey', this.getTxs)
.get('/api/v1/estimatefee/:blocks', this.getEstimateFee)
.get('/api/v2/sendtx/:txHex',this.sendTX)
.get('/api/v2/sendtx/:txHex', this.sendTX)
}



async sendTX(req, res) {
try {
const tx = req.params.txHex;
// const result = await client.broadcast(tx);
const result = await client.execute('sendrawtransaction', [ tx ]);
console.log(result);
// const result = await client.broadcast(tx);
const result = await client.execute('sendrawtransaction', [tx]);
//console.log(result);
res.status(200).json({
result: result
});
Expand All @@ -91,8 +75,8 @@ class Router {
async getEstimateFee(req, res) {
try {
const blocks = Number(req.params.blocks);
console.log(blocks/10);
const data = await client.estimateFee(blocks/10);
//console.log(blocks / 10);
const data = await client.estimateFee(blocks / 10);
console.log(data);
res.status(200).json({
result: String(data.rate)
Expand All @@ -107,42 +91,64 @@ class Router {


async getTxs(req, res) {

try {

const xpubkey = req.params.xpubkey;
const address = await getReceiveAddress(xpubkey);
const txs = await client.getTXByAddress(address);
const txs_processed = txs.map((item) => {
const offset = req.query.offset ? Number(req.query.offset) : 0;
const limit = req.query.limit ? Number(req.query.limit) : 20;

const wallet = walletClient.wallet(xpubkey.slice(0, 40));

const txs = (await wallet.getHistory()).slice(offset, limit);
const tokens = [];

const txs_processed = [];
for (let i = 0; i < txs.length; i++) {
let valueIn = 0;
const vin = item.inputs.map((item) => {
valueIn += item.coin.value;
const item = txs[i];
const tx = await getTxByHash(item.hash);
const vin = item.inputs.map((item, index) => {
item.value = tx.vin[index].value;
if (item.path && !tokens.includes(item.address)) {
tokens.push(item.address)
}
valueIn += item.value;
return {
addresses: [item.coin.address],
value: item.coin.value,
coinbase: item.coin.coinbase
addresses: [item.address],
value: item.value,
coinbase: item.address ? false : true
}
});

const vout = item.outputs.map((item) => {
if (item.path && !tokens.includes(item.address)) {
tokens.push(item.address)
}
return {
addresses: [item.address],
value: item.value
}
})

return {
txid: item.txid,
blockTime: item.blockTime,
blockHeight: item.blockHeight,
txs_processed.push({
txid: item.hash,
blockTime: item.time,
blockHeight: item.height,
vin: vin,
vout: vout,
valueIn: valueIn

}
})

}





})
res.status(200).json({
tokens: [],
tokens: tokens,
transactions: txs_processed
});

Expand All @@ -165,35 +171,41 @@ class Router {
async getUTXO(req, res) {
try {
const xpubkey = req.params.xpubkey;
const clientinfo = await client.getInfo();
const clientinfo = await client.getInfo();
await walletClient.rescan(clientinfo.chain.height);
try {
const createWallet = await walletClient.createWallet(xpubkey.slice(0, 40), {
witness: false,
watchOnly: true,
accountKey: xpubkey
});
console.log(createWallet);
const wallet = walletClient.wallet(xpubkey.slice(0, 40));
for (let i = 0; i < 22; i++) { //初始化钱包遍历22个深度
await wallet.createAddress('default');
await wallet.createChange('default');
}
} catch (err) {
console.log(err);
// console.log('钱包已经存在');
console.log(err);
}


const wallet = walletClient.wallet(xpubkey.slice(0, 40));
console.log(await wallet.getAccount('default'));
//console.log(await wallet.getAccount('default'));
const coins = await wallet.getCoins();
console.log(coins);
//console.log(coins);
const items = [];
for (let i of coins) {
items.push({ value: i.value,
confirmations: i.height > 0 ? 1 : 0 ,
address : i.address,
height : i.height,
path: `m/44'/0'/0'/0/0`,
txid: i.hash,
vout: i.index
})
const tx = await wallet.getTX(i.hash);
const path = tx.outputs[Number(i.index)].path.derivation.replace("m/", "m/44'/0'/"); //默认使用传统地址
items.push({
value: i.value,
confirmations: i.height > 0 ? 1 : 0,
address: i.address,
height: i.height,
path: path,
txid: i.hash,
vout: i.index
})
}

res.status(200).json(items);
Expand All @@ -207,22 +219,18 @@ class Router {
}




async getTx(req, res) {
try {

const txid = req.params.tx;
const data = await client.getTX(txid);



let valueIn = 0;
const vin = data.inputs.map((item) => {
valueIn += item.coin.value;
return {
addresses: [item.coin.address],
value: item.coin.value,
coinbase: item.coin.coinbase
coinbase: item.coin.address ? false : true
}
});

Expand Down Expand Up @@ -283,4 +291,61 @@ class Router {

}


async function getTxByHash(hash) {
try {

const data = await client.getTX(hash);
let valueIn = 0;
const vin = data.inputs.map((item) => {
valueIn += item.coin.value;
return {
addresses: [item.coin.address],
value: item.coin.value,
coinbase: item.coin.address ? false : true
}
});

const vout = data.outputs.map((item) => {
return {
addresses: [item.address],
value: item.value
}
})


return {
txid: data.hash,
blockHeight: data.height,
fees: data.fee,
blockTime: data.time,
confirmations: data.confirmations,
value: 0,
valueIn: valueIn,
vin: vin,
vout: vout


}
} catch (err) {
console.log(err);
return {};
}
}


async function getReceiveAddress(pub) {
const wdb = new WalletDB({ network: 'regtest' });
await wdb.open();
const walletdb = await wdb.create({
watchOnly: true,
accountKey: pub,
witness: false
});
const account = await walletdb.getAccount(0);
const recAddr = account.toJSON().receiveAddress;
await wdb.close();
return recAddr;
}

module.exports = Router;