Passed
Pull Request — master (#1)
by thomas
01:07
created

index.js ➔ fromWords   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
'use strict';
2
3
var BigInteger = require('bigi');
4
var ALPHABET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l';
5
6
// pre-compute lookup table
7
var SEPARATOR = ':';
8
var CSLEN = 8;
9
var ALPHABET_MAP = {};
10
for (var z = 0; z < ALPHABET.length; z++) {
11
    var x = ALPHABET.charAt(z);
12
13
    if (ALPHABET_MAP[x] !== undefined) throw new TypeError(x + ' is ambiguous');
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
14
    ALPHABET_MAP[x] = z;
15
}
16
17
function polymodStep(pre) {
18
    var b = pre.shiftRight(35);
19
    var mask = BigInteger.fromHex('07ffffffff');
20
21
    var v = pre.and(mask).shiftLeft(new BigInteger('5'));
22
23
    if (b.and(new BigInteger('1')).intValue() > 0) {
24
        v = v.xor(BigInteger.fromHex('98f2bc8e61'));
25
    }
26
    if (b.and(new BigInteger('2')).intValue()) {
27
        v = v.xor(BigInteger.fromHex('79b76d99e2'));
28
    }
29
    if (b.and(new BigInteger('4')).intValue()) {
30
        v = v.xor(BigInteger.fromHex('f33e5fb3c4'));
31
    }
32
    if (b.and(new BigInteger('8')).intValue()) {
33
        v = v.xor(BigInteger.fromHex('ae2eabe2a8'));
34
    }
35
    if (b.and(new BigInteger('16')).intValue()) {
36
        v = v.xor(BigInteger.fromHex('1e4f43e470'));
37
    }
38
39
    return v;
40
}
41
42
function prefixChk(prefix) {
43
    var chk = new BigInteger('1');
44
    for (var i = 0; i < prefix.length; ++i) {
45
        var c = prefix.charCodeAt(i);
46
47
        var mixwith = new BigInteger('' + (c & 0x1f));
48
        chk = polymodStep(chk).xor(mixwith);
49
    }
50
51
    chk = polymodStep(chk);
52
    return chk;
53
}
54
55
function encode(prefix, words) {
56
    // too long?
57
    if (prefix.length + 7 + words.length > 90) throw new TypeError('Exceeds Bech32 maximum length');
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
58
    prefix = prefix.toLowerCase();
59
60
    // determine chk mod
61
    var chk = prefixChk(prefix);
62
    var result = prefix + SEPARATOR;
63
    for (var i = 0; i < words.length; ++i) {
64
        var _x = words[i];
65
        if (_x >>> 5 !== 0) throw new Error('Non 5-bit word');
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
66
        chk = polymodStep(chk).xor(new BigInteger('' + _x));
67
        result += ALPHABET.charAt(_x);
68
    }
69
70
    for (var _i = 0; _i < CSLEN; ++_i) {
71
        chk = polymodStep(chk);
72
    }
73
    chk = chk.xor(new BigInteger('1'));
74
    for (var _i2 = 0; _i2 < CSLEN; ++_i2) {
75
        var pos = 5 * (CSLEN - 1 - _i2);
76
        var v2 = chk.shiftRight(new BigInteger('' + pos)).and(BigInteger.fromHex('1f'));
77
        result += ALPHABET.charAt(v2.toString(10));
78
    }
79
80
    return result;
81
}
82
83
function decode(str) {
84
    if (str.length < 8) throw new TypeError(str + ' too short');
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
85
    if (str.length > 90) throw new TypeError(str + ' too long');
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
86
87
    // don't allow mixed case
88
    var lowered = str.toLowerCase();
89
    var uppered = str.toUpperCase();
90
    if (str !== lowered && str !== uppered) throw new Error('Mixed-case string ' + str);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
91
    str = lowered;
92
93
    var split = str.lastIndexOf(SEPARATOR);
94
    if (split === -1) throw new Error('No separator character for ' + str);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
95
    if (split === 0) throw new Error('Missing prefix for ' + str);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
96
97
    var prefix = str.slice(0, split);
98
    var wordChars = str.slice(split + 1);
99
    if (wordChars.length < 6) throw new Error('Data too short');
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
100
101
    var chk = prefixChk(prefix);
102
    var words = [];
103
    for (var i = 0; i < wordChars.length; ++i) {
104
        var c = wordChars.charAt(i);
105
        var v = ALPHABET_MAP[c];
106
        if (v === undefined) throw new Error('Unknown character ' + c);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
107
        chk = polymodStep(chk).xor(new BigInteger('' + v));
108
        // not in the checksum?
109
        if (i + CSLEN >= wordChars.length) continue;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
110
        words.push(v);
111
    }
112
113
    if (chk.toString(10) !== '1') throw new Error('Invalid checksum for ' + str);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
114
    return { prefix: prefix, words: words };
115
}
116
117
function convert(data, inBits, outBits, pad) {
118
    var value = 0;
119
    var bits = 0;
120
    var maxV = (1 << outBits) - 1;
121
122
    var result = [];
123
    for (var i = 0; i < data.length; ++i) {
124
        value = value << inBits | data[i];
125
        bits += inBits;
126
127
        while (bits >= outBits) {
128
            bits -= outBits;
129
            result.push(value >>> bits & maxV);
130
        }
131
    }
132
133
    if (pad) {
134
        if (bits > 0) {
135
            result.push(value << outBits - bits & maxV);
136
        }
137
    } else {
138
        if (bits >= inBits) throw new Error('Excess padding');
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
139
        if (value << outBits - bits & maxV) throw new Error('Non-zero padding');
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...
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
140
    }
141
142
    return result;
143
}
144
145
function toWords(bytes) {
146
    return convert(bytes, 8, 5, true);
147
}
148
149
function fromWords(words) {
150
    return convert(words, 5, 8, false);
151
}
152
153
module.exports = { decode: decode, encode: encode, toWords: toWords, fromWords: fromWords };