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

tools/setup-dev-account.js   A

Complexity

Total Complexity 16
Complexity/F 1.45

Size

Lines of Code 116
Function Count 11

Duplication

Duplicated Lines 53
Ratio 45.69 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 0
wmc 16
c 3
b 0
f 0
nc 1
mnd 1
bc 15
fnc 11
dl 53
loc 116
rs 10
bpm 1.3636
cpm 1.4544
noi 4

5 Functions

Rating   Name   Duplication   Size   Complexity  
A setup-dev-account.js ➔ createDiscoveryTestWallet 0 6 1
A setup-dev-account.js ➔ createTransactionTestWallet 0 3 1
A setup-dev-account.js ➔ createTransactionTestWallet(ꞌunittest-transactionꞌ) 0 15 2
A setup-dev-account.js ➔ createTransactionTestWallet(ꞌunittest-transaction-swꞌ) 0 15 2
A setup-dev-account.js ➔ _createTestWallet 53 53 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
var _ = require('lodash');
2
var blocktrail = require('../');
3
var crypto = require('crypto');
4
var async = require('async');
5
var bitcoin = require('bitcoinjs-lib');
6
var bip39 = require("bip39");
7
8
/**
9
 * @type APIClient
10
 */
11
var client = blocktrail.BlocktrailSDK({
12
    apiKey : process.env.BLOCKTRAIL_SDK_APIKEY || "EXAMPLE_BLOCKTRAIL_SDK_NODEJS_APIKEY",
13
    apiSecret : process.env.BLOCKTRAIL_SDK_APISECRET || "EXAMPLE_BLOCKTRAIL_SDK_NODEJS_APISECRET",
14
    testnet : true
15
});
16
17
var TRANSACTION_TEST_WALLET_PRIMARY_MNEMONIC = "give pause forget seed dance crawl situate hole keen",
18
    TRANSACTION_TEST_WALLET_BACKUP_MNEMONIC = "give pause forget seed dance crawl situate hole give",
19
    TRANSACTION_TEST_WALLET_PASSWORD = "password";
20
21 View Code Duplication
var _createTestWallet = function(identifier, passphrase, primaryMnemonic, backupMnemonic, segwit, cb) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
22
    var keyIndex = 9999;
23
    var network = client.testnet ? bitcoin.networks.testnet : bitcoin.networks.bitcoin;
24
25
    var primarySeed = bip39.mnemonicToSeed(primaryMnemonic, passphrase);
26
    var primaryPrivateKey = bitcoin.HDNode.fromSeedBuffer(primarySeed, network);
27
28
    var backupSeed = bip39.mnemonicToSeed(backupMnemonic, "");
29
    var backupPrivateKey = bitcoin.HDNode.fromSeedBuffer(backupSeed, network);
30
    var backupPublicKey = backupPrivateKey.neutered();
31
32
    var checksum = primaryPrivateKey.getAddress();
33
    var primaryPublicKey = primaryPrivateKey.deriveHardened(keyIndex).neutered();
34
35
    client.storeNewWalletV1(
36
        identifier,
37
        [primaryPublicKey.toBase58(), "M/" + keyIndex + "'"],
38
        [backupPublicKey.toBase58(), "M"],
39
        primaryMnemonic,
40
        checksum,
41
        keyIndex,
42
        segwit
43
    ).then(function(result) {
44
        var blocktrailPublicKeys = _.mapValues(result.blocktrail_public_keys, function(blocktrailPublicKey) {
45
            return bitcoin.HDNode.fromBase58(blocktrailPublicKey[0], network);
46
        });
47
48
        var wallet = new blocktrail.Wallet(
49
            client,
50
            identifier,
51
            blocktrail.Wallet.WALLET_VERSION_V1,
52
            primaryMnemonic,
53
            null,
54
            null,
55
            {keyIndex: primaryPublicKey},
56
            backupPublicKey,
57
            blocktrailPublicKeys,
58
            keyIndex,
59
            result.chain || 0,
60
            result.segwit || 0,
61
            client.testnet,
62
            checksum
63
        );
64
65
        wallet.unlock({
66
            passphrase: passphrase
67
        }, function(err) {
68
            cb(err, wallet);
69
        });
70
    }, function(err) {
71
        cb(err);
72
    });
73
};
74
75
var createDiscoveryTestWallet = function(identifier, passphrase, cb) {
76
    var primaryMnemonic = "give pause forget seed dance crawl situate hole kingdom";
77
    var backupMnemonic = "give pause forget seed dance crawl situate hole course";
78
79
    return _createTestWallet(identifier, passphrase, primaryMnemonic, backupMnemonic, cb);
80
};
81
82
var createTransactionTestWallet = function(identifier, segwit, cb) {
83
    return _createTestWallet(identifier, TRANSACTION_TEST_WALLET_PASSWORD, TRANSACTION_TEST_WALLET_PRIMARY_MNEMONIC, TRANSACTION_TEST_WALLET_BACKUP_MNEMONIC, segwit, cb);
84
};
85
86
createTransactionTestWallet("unittest-transaction", false, function(err, wallet) {
87
    if (err) {
88
        console.log(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...
89
        return;
90
    }
91
92
    wallet.doDiscovery(50, function(err, result) {
93
        if (err) {
94
            console.log(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...
95
            return;
96
        }
97
98
        console.log(result);
99
    });
100
});
101
102
createTransactionTestWallet("unittest-transaction-sw", true, function(err, wallet) {
103
    if (err) {
104
        console.log(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...
105
        return;
106
    }
107
108
    wallet.doDiscovery(50, function(err, result) {
109
        if (err) {
110
            console.log(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...
111
            return;
112
        }
113
114
        console.log(result);
115
    });
116
});
117
118