Completed
Push — master ( a4251b...3c3114 )
by Ruben de
01:08 queued 15s
created

client.createNewWallet   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
nop 3
1
/* jshint -W101 */
2
var blocktrail = require('../'); // require('blocktrail-sdk') when trying example from in your own project
3
var bitcoin = require('bitcoinjs-lib');
4
5
var client = blocktrail.BlocktrailSDK({
6
    apiKey : "YOUR_APIKEY_HERE",
7
    apiSecret : "YOUR_APISECRET_HERE",
8
    testnet : true
9
});
10
11
/*
12
 * this example is for when you're storing the primary private and backup public key yourself
13
 */
14
var primaryPrivateKey = bitcoin.HDNode.fromBase58("tprv8ZgxMBicQKsPdMD2AYgpezVQZNi5kxsRJDpQWc5E9mxp747KgzekJbCkvhqv6sBTDErTjkWqZdY14rLP1YL3cJawEtEp2dufHxPhr1YUoeS", bitcoin.networks.testnet);
15
var backupPublicKey = bitcoin.HDNode.fromBase58("tpubD6NzVbkrYhZ4Y6Ny2VF2o5wkBGuZLQAsGPn88Y4JzKZH9siB85txQyYq3sDjRBFwnE1YhdthmHWWAurJu7EetmdeJH9M5jz3Chk7Ymw2oyf", bitcoin.networks.testnet);
16
17
var sendTransaction = function(wallet) {
18
    wallet.getNewAddress(function(err, address, path) {
19
        if (err) {
20
            return console.log("getNewAddress ERR", err);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
21
        }
22
23
        console.log('new address', address, path);
24
25
        var pay = {};
26
        pay[address] = blocktrail.toSatoshi(0.001);
27
28
        wallet.pay(pay, function(err, result) {
29
            if (err) {
30
                return console.log("pay ERR", err);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
31
            }
32
33
            console.log('transaction', result);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
34
        });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
35
    });
36
};
37
38
var action = 'default';
39
40
if (action === 'create') {
41
    client.createNewWallet({
42
        identifier: "example-wallet",
43
        keyIndex: 9999,
44
        primaryPrivateKey: primaryPrivateKey,
45
        backupPublicKey: backupPublicKey
46
    }, function(err, wallet, primaryMnemonic, backupMnemonic, blocktrailPubKeys) {
47
            if (err) {
48
                return console.log("createNewWallet ERR", err);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
49
            }
50
51
            console.log('primary mnemonic', primaryMnemonic);
52
            console.log('backup mnemonic', backupMnemonic);
53
            console.log('blocktrail pubkeys', blocktrailPubKeys);
54
55
            wallet.getNewAddress(function(err, address, path) {
56
                if (err) {
57
                    return console.log("getNewAddress ERR", err);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
58
                }
59
60
                console.log('new address', address, path);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
61
            });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
62
        }
63
    );
64
} else {
65
    client.initWallet({
66
        identifier: "example-wallet",
67
        keyIndex: 9999,
68
        primaryPrivateKey: primaryPrivateKey,
69
        primaryMnemonic: false
70
    }, function(err, wallet) {
71
            if (err) {
72
                return console.log('initWallet ERR', err);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
73
            }
74
75
            wallet.getBalance(function(err, confirmed, unconfirmed) {
76
                if (err) {
77
                    return console.log("getBalance ERR", err);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
78
                }
79
80
                console.log('confirmed balance', confirmed);
81
                console.log('unconfirmed balance', unconfirmed);
82
83
                sendTransaction(wallet);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
84
            });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
85
        }
86
    );
87
}
88