Code Duplication    Length = 85-91 lines in 2 locations

examples/opreturn_payment_api_usage.js 1 location

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

examples/wallet_force_fee.js 1 location

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