Completed
Pull Request — master (#78)
by Ruben de
04:05 queued 01:39
created

examples/simple_payment_api_usage.js   A

Complexity

Total Complexity 16
Complexity/F 2

Size

Lines of Code 84
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
wmc 16
c 0
b 0
f 0
nc 2
mnd 2
bc 17
fnc 8
dl 0
loc 84
rs 10
bpm 2.125
cpm 2
noi 13

3 Functions

Rating   Name   Duplication   Size   Complexity  
B client.initWallet 0 31 2
A simple_payment_api_usage.js ➔ sendTransaction 0 21 1
A client.createNewWallet 0 9 2
1
var blocktrail = require('../'); // require('blocktrail-sdk') when trying example from in your own project
2
3
var client = blocktrail.BlocktrailSDK({
4
    apiKey : process.env.BLOCKTRAIL_SDK_APIKEY || "MY_APIKEY",
5
    apiSecret : process.env.BLOCKTRAIL_SDK_APISECRET || "MY_APISECRET",
6
    testnet : true
7
});
8
9
var ALLOW_ZERO_CONF = true;
10
11
var sendTransaction = function(wallet) {
12
    wallet.getNewAddress(function(err, address, path) {
13
        if (err) {
14
            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...
15
        }
16
17
        console.log('new address', address, path);
18
19
        var pay = {};
20
        pay[address] = blocktrail.toSatoshi(0.001);
21
22
        wallet.pay(pay, null, ALLOW_ZERO_CONF, function(err, result) {
23
        // wallet.pay(pay, null, true, true, blocktrail.Wallet.FEE_STRATEGY_LOW_PRIORITY, function(err, result) {
24
            if (err) {
25
                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...
26
            }
27
28
            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...
29
        });
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...
30
    });
31
};
32
33
var action = 'default';
34
35
if (action === 'create') {
36
    client.createNewWallet({
37
        identifier: "example-wallet-sw",
38
        passphrase: "example-strong-password",
39
        keyIndex: 9999
40
    }, function(err, wallet, primaryMnemonic, backupMnemonic, blocktrailPubKeys) {
41
        if (err) {
42
            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...
43
        }
44
45
        console.log('primary mnemonic', primaryMnemonic);
46
        console.log('backup mnemonic', backupMnemonic);
47
        console.log('blocktrail pubkeys', blocktrailPubKeys);
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...
48
    });
49
} else {
50
    client.initWallet({
51
        identifier: "example-wallet-sw",
52
        readOnly: true
53
    }, function(err, wallet) {
54
        if (err) {
55
            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...
56
            throw err;
57
        }
58
59
        wallet.getBalance(function(err, confirmed, unconfirmed) {
60
            if (err) {
61
                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...
62
            }
63
64
            console.log('confirmed balance', confirmed);
65
            console.log('unconfirmed balance', unconfirmed);
66
67
            wallet.getNewAddress(function(err, address) {
68
                if (err) {
69
                    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...
70
                }
71
72
                console.log('address', address);
73
74
                wallet.unlock({passphrase: "example-strong-password"}, function(err) {
75
                    if (err) {
76
                        return console.log("unlock 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...
77
                    }
78
79
                    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...
80
                });
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...
81
            });
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...
82
        });
83
    });
84
}
85