Completed
Pull Request — master (#3)
by thomas
01:17
created

src/index.js   A

Complexity

Total Complexity 21
Complexity/F 3.5

Size

Lines of Code 102
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
c 2
b 0
f 0
nc 1
dl 0
loc 102
ccs 53
cts 53
cp 1
crap 0
rs 10
wmc 21
mnd 2
bc 16
fnc 6
bpm 2.6666
cpm 3.5
noi 4

6 Functions

Rating   Name   Duplication   Size   Complexity  
A index.js ➔ createVersion 0 7 4
A index.js ➔ encodePayload 0 8 1
A index.js ➔ encode 0 14 3
A index.js ➔ decode 0 18 3
A index.js ➔ checkMap 0 10 3
B index.js ➔ decodeVersion 0 18 7
1
'use strict'
2 1
let base32 = require('./base32')
3
4 1
let P2SH = "scripthash";
5 1
let P2PKH = "pubkeyhash";
6
7 1
let hashBitMap = {};
8 1
hashBitMap[160] = 0;
9 1
hashBitMap[192] = 1;
10 1
hashBitMap[224] = 2;
11 1
hashBitMap[256] = 3;
12 1
hashBitMap[320] = 4;
13 1
hashBitMap[384] = 5;
14 1
hashBitMap[448] = 6;
15 1
hashBitMap[512] = 7;
16
17 1
let versionBitMap = {};
18 1
versionBitMap[P2PKH] = 0;
19 1
versionBitMap[P2SH] = 1;
20
21
function checkMap(mapObj, value) {
22 21
    let keys = Object.keys(mapObj);
23 21
    for (let i = 0; i < keys.length; i++) {
24 30
        if (mapObj[keys[i]] === value) {
25 20
            return keys[i];
26
        }
27
    }
28
29 1
    return null;
30
}
31
32
function createVersion(scriptType, hashLengthBits) {
33 9
    if ((scriptType === P2PKH || scriptType === P2SH) && hashLengthBits !== 160) {
34 1
        throw new Error("Invalid hash length for scriptType");
35
    }
36
37 8
    return (versionBitMap[scriptType] << 3) | hashBitMap[hashLengthBits];
38
}
39
40
function encodePayload(scriptType, hash) {
41 9
    let hashLength = hash.length
42 9
    let version = createVersion(scriptType, hashLength * 8)
43 8
    let payload = Buffer.allocUnsafe(1 + hash.length)
0 ignored issues
show
Bug introduced by
The variable Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
44 8
    payload.writeUInt8(version, 0)
45 8
    hash.copy(payload, 1)
46 8
    return payload
47
}
48
49
function decodeVersion (version) {
50 12
    let last = (version >>> 7);
51 12
    if (last & 1 || last > 0) {
0 ignored issues
show
introduced by
You have used a bitwise operator & in a condition. Did you maybe want to use the logical operator &&
Loading history...
52 1
        throw new Error("Invalid version, most significant bit is reserved");
53
    }
54
55 11
    let scriptType = checkMap(versionBitMap, (version >> 3) & 0x0f)
56 11
    if (scriptType === null) {
57 1
        throw new Error("Invalid script type");
58
    }
59
    // all possible values return
60 10
    let hashSize = parseInt(checkMap(hashBitMap, version & 0x07), 10);
61 10
    if ((scriptType === P2PKH || scriptType === P2SH) && hashSize !== 160) {
62 1
        throw new Error("Mismatch between script type and hash length");
63
    }
64
65 9
    return {scriptType, hashSize}
66
}
67
68
function encode (prefix, scriptType, hash) {
69 11
    if (!(hash instanceof Buffer)) {
0 ignored issues
show
Bug introduced by
The variable Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
70 1
        throw new Error("Hash should be passed as a Buffer")
71
    }
72
73 10
    if (!(scriptType in versionBitMap)) {
74 1
        throw new Error("Unsupported script type")
75
    }
76
77 9
    return base32.encode(
78
        prefix,
79
        base32.toWords(encodePayload(scriptType, hash))
80
    )
81
}
82
83
function decode(address) {
84 13
    let result = base32.decode(address)
85 13
    let data = base32.fromWords(result.words)
86 13
    if (data.length < 1) {
87 1
        throw new Error("Empty payload in address")
88
    }
89
90 12
    let versionInfo = decodeVersion(data[0])
91 9
    if (1 + (versionInfo.hashSize / 8) !== data.length) {
92 1
        throw new Error('Hash length does not match version')
93
    }
94
95 8
    return {
96
        version: versionInfo.scriptType,
97
        prefix: result.prefix,
98
        hash: Buffer.from(data.slice(1))
0 ignored issues
show
Bug introduced by
The variable Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
99
    }
100
}
101
102
module.exports = { decode: decode, encode: encode };
103