aboutsummaryrefslogtreecommitdiffstats
path: root/lib/buildtx.js
blob: 5e0fe29f7787c0a31985c80bfa3ea0603b3b5803 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const axios = require("axios");
const bitcoin = require("bitcoinjs-lib");
const coinselect = require("coinselect");
const split = require("coinselect/split");
const api = prod ? "https://blockstream.info/api" : "http://localhost:3095";
const SATS = 100000000;
const network = prod
  ? bitcoin.networks["bitcoin"]
  : bitcoin.networks["regtest"];

module.exports = async ({ address, amount, feeRate, replaceable, user }) => {
  if (!feeRate) feeRate = (await bc.estimateSmartFee(6)).feerate * SATS;
  feeRate = Math.round(feeRate / 1000);

  const payments = await db.Payment.findAll({
    where: {
      received: true,
      account_id: user.account_id,
      network: 'bitcoin',
    } 
  });


  const addresses = [...new Set(payments.map(p => p.address))];
  const utxos = [];
  for (let i = 0; i < addresses.length; i++) {
    try {
      utxos.push(...(await axios.get(`${api}/address/${addresses[i]}/utxo`)).data);
    } catch (e) {
      l.error("problem fetching utxos", e.message);
    }
  } 

  let targets = [
    {
      address,
      value: amount
    }
  ];

  let { inputs, outputs, fee } = coinselect(utxos, targets, feeRate);

  if (!inputs || !outputs) {
    l.info("utxos", amount, feeRate, utxos.map(u => u.value));
    throw new Error("Couldn't find inputs for transaction, check your balance/fee");
  } 

  let psbt = new bitcoin.Psbt({ network });
  let total = 0;

  outputs.map(({ address, value }) => {
    total += value;
    if (!address) {
      address =  payments[payments.length - 1].address;
      change.push(address);
    } 

    psbt = psbt.addOutput({
      address,
      value
    });
  });

  for (let i = 0; i < inputs.length; i++) {
    let { txid, vout } = inputs[i];
    let nonWitnessUtxo = Buffer.from(await bc.getRawTransaction(txid), "hex");
    psbt = psbt.addInput({
      hash: txid,
      index: vout,
      nonWitnessUtxo
    });
  }

  if (replaceable) psbt.setInputSequence(0, 0xffffffff - 2);

  feeRate = Math.round(feeRate * 1000);
  psbt = psbt.toBase64();

  return { tx: psbt, feeRate };
};
0a5d6a5 ^
7c560110 ^

4352e081 ^
c06178aa ^
4352e081 ^
5f907fcf ^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
 
                   
                     

                  
                                   
                                                 
                                           

                   
                                      
                     
                       
                           
                                                           
                                 
                       
                      
                             
                               
                              

                            
                                                      
                              
                               
                            



                              
                     
                            
                       
                        
                                 
                                
                         


                                            
                               

                                       
                             
                    
                                                       
                        
                        
                       

                       

                                
                             
                                                                                



                              


                                

                                 
                                                                          
                                     
                        
                      

                      
                                     
                                   
                    

   
{
  "name": "coinos",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "babel-node index.js",
    "debug": "babel-node --inspect-brk index.js",
    "postinstall": "cp -n .env.sample .env"
  },
  "dependencies": {
    "apollo-server-express": "^1.3.2",
    "async": "2.1.1",
    "axios": "^0.18.0",
    "babel-cli": "^6.26.0",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "bcrypt": "^2.0.1",
    "bip39": "^2.2.0",
    "bitcoin-core": "^2.0.0",
    "bitcoinaverage": "^0.1.4",
    "bitcoinjs-lib": "^3.3.2",
    "bluebird": "^3.4.6",
    "body-parser": "^1.5.0",
    "bolt11": "https://github.com/asoltys/bolt11#wip",
    "browser-sync": "^2.11.0",
    "buffer-reverse": "^1.0.1",
    "compression": "^1.7.1",
    "connect-assets": "*",
    "connect-redis": "3.1.0",
    "consolidate": "0.14.1",
    "cookie-parser": "^1.3.2",
    "cors": "^2.8.4",
    "crypto-js": "^3.1.9-1",
    "dotenv": "^4.0.0",
    "express": "4.14.0",
    "express-graphql": "^0.6.12",
    "express-session": "^1.6.5",
    "graphql": "^0.12.3",
    "graphql-redis-subscriptions": "^1.4.0",
    "graphql-relay": "^0.5.4",
    "graphql-sequelize": "^5.6.1",
    "graphql-tools": "^2.18.0",
    "hogan-express": "0.5.2",
    "http-proxy-middleware": "^0.17.2",
    "jsonwebtoken": "^8.1.1",
    "less": "2.7.1",
    "lnrpc": "https://github.com/asoltys/lnrpc#master",
    "lodash": "^4.17.4",
    "moment": "^2.10.6",
    "morgan": "^1.9.0",
    "mysql": "^2.15.0",
    "mysql2": "^1.5.1",
    "node-http-proxy": "^0.2.3",
    "passport": "0.3.2",
    "passport-jwt": "^4.0.0",
    "passport-jwt.socketio": "https://github.com/erreina/passport-jwt.socketio",
    "passport-local": "1.0.0",
    "redis": "2.6.2",
    "request": "~2.75.0",
    "sendgrid": "^4.3.0",
    "sequelize": "^4.32.2",
    "sequelize-auto": "^0.4.29",
    "sequelize-cli": "^3.2.0",
    "socket.io": "^2.0.4",
    "socket.io-client": "^2.0.4",
    "socketio-jwt-auth": "https://github.com/adcentury/socketio-jwt-auth",
    "sql-template-strings": "^2.2.2",
    "twilio": "^2.11.1",
    "zeromq": "^4.6.0"
  },
  "devDependencies": {
    "babel-preset-es2015": "^6.24.1",
    "eslint-plugin-html": "^5.0.0",
    "pm2": "^2.10.4"
  }
}