Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit 553485f

Browse files
committed
Merge branch 'getdelta-trezor/fix/stellar-upper-zero-byte'
2 parents 8bcee5d + 0309a19 commit 553485f

3 files changed

Lines changed: 32 additions & 42 deletions

File tree

src/crypto/utils.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,8 @@ const addressType = {
1818
WITNESS_UNKNOWN: 'p2w-unknown',
1919
};
2020

21-
function numberToHex(number) {
22-
var hex = Math.round(number).toString(16)
23-
if (hex.length === 1) {
24-
hex = '0' + hex
25-
}
26-
return hex
21+
function numberToHex(number, sizeInBytes) {
22+
return Math.round(number).toString(16).padStart(sizeInBytes * 2, '0');
2723
}
2824

2925
function isHexChar(c) {
@@ -91,10 +87,11 @@ function hexStr2byteArray(str) {
9187
}
9288

9389
module.exports = {
90+
numberToHex,
9491
toHex: function (arrayOfBytes) {
9592
var hex = '';
9693
for (var i = 0; i < arrayOfBytes.length; i++) {
97-
hex += numberToHex(arrayOfBytes[i]);
94+
hex += numberToHex(arrayOfBytes[i], 1);
9895
}
9996
return hex;
10097
},

src/stellar_validator.js

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,40 @@
11
const { addressType } = require('../src/crypto/utils');
2-
var baseX = require('base-x');
3-
var crc = require('crc');
4-
var cryptoUtils = require('./crypto/utils');
2+
const baseX = require('base-x');
3+
const crc = require('crc');
4+
const cryptoUtils = require('./crypto/utils');
55

6-
var ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
6+
const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
77

8-
var base32 = baseX(ALPHABET);
9-
var regexp = new RegExp('^[' + ALPHABET + ']{56}$');
10-
var ed25519PublicKeyVersionByte = (6 << 3);
8+
const base32 = baseX(ALPHABET);
9+
const regexp = new RegExp('^[' + ALPHABET + ']{56}$');
10+
const ed25519PublicKeyVersionByte = (6 << 3);
1111

12-
function swap16(number) {
13-
var lower = number & 0xFF;
14-
var upper = (number >> 8) & 0xFF;
12+
function swap16(number) {
13+
const lower = number & 0xFF;
14+
const upper = (number >> 8) & 0xFF;
1515
return (lower << 8) | upper;
1616
}
1717

18-
function numberToHex(number) {
19-
var hex = number.toString(16);
20-
if(hex.length % 2 === 1) {
21-
hex = '0' + hex;
22-
}
23-
return hex;
24-
}
25-
26-
module.exports = {
18+
module.exports = {
2719
isValidAddress: function (address) {
2820
if (regexp.test(address)) {
2921
return this.verifyChecksum(address);
3022
}
3123

32-
return false;
24+
return false;
3325
},
3426

3527
verifyChecksum: function (address) {
36-
// based on https://github.com/stellar/js-stellar-base/blob/master/src/strkey.js#L126
37-
var bytes = base32.decode(address);
28+
// based on https://github.com/stellar/js-stellar-base/blob/master/src/strkey.js
29+
const bytes = base32.decode(address);
3830
if (bytes[0] !== ed25519PublicKeyVersionByte) {
3931
return false;
4032
}
4133

42-
var computedChecksum = numberToHex(swap16(crc.crc16xmodem(bytes.slice(0, -2))));
43-
var checksum = cryptoUtils.toHex(bytes.slice(-2));
44-
45-
return computedChecksum === checksum
34+
const payload = bytes.slice(0, -2);
35+
const checksum = cryptoUtils.toHex(bytes.slice(-2));
36+
const computedChecksum = cryptoUtils.numberToHex(swap16(crc.crc16xmodem(payload)), 2);
37+
return computedChecksum === checksum;
4638
},
4739

4840
getAddressType: function (address, currency, networkType) {

test/wallet_address_validator.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ describe('WAValidator.validate()', function () {
8888

8989
it('should return true for correct bitcoincash addresses', function () {
9090
valid('bitcoincash:qq4v32mtagxac29my6gwj6fd4tmqg8rysu23dax807', 'bch');
91-
valid('bitcoincash:qp3wjpa3tjlj042z2wv7hahsldgwhwy0rq9sywjpyy', 'bch');
92-
valid('qp3wjpa3tjlj042z2wv7hahsldgwhwy0rq9sywjpyy', 'bch');
93-
valid('bitcoincash:qp3wjpa3tjlj042z2wv7hahsldgwhwy0rq9sywjpyy', 'bch', 'testnet');
94-
valid('qp3wjpa3tjlj042z2wv7hahsldgwhwy0rq9sywjpyy', 'bch', 'testnet');
91+
valid('bitcoincash:qp3wjpa3tjlj042z2wv7hahsldgwhwy0rq9sywjpyy', 'bch');
92+
valid('qp3wjpa3tjlj042z2wv7hahsldgwhwy0rq9sywjpyy', 'bch');
93+
valid('bitcoincash:qp3wjpa3tjlj042z2wv7hahsldgwhwy0rq9sywjpyy', 'bch', 'testnet');
94+
valid('qp3wjpa3tjlj042z2wv7hahsldgwhwy0rq9sywjpyy', 'bch', 'testnet');
9595
});
9696

9797
it('should match the expected BCH address type', function () {
@@ -110,7 +110,7 @@ describe('WAValidator.validate()', function () {
110110

111111
// p2sh addresses
112112
valid('MUWheVyCBf3Fm3WNNXvotQ3Gj8NTSZCBVe', 'litecoin');
113-
113+
114114
valid('2MxKEf2su6FGAUfCEAHreGFQvEYrfYNHvL7', 'litecoin', 'testnet');
115115
valid('QW2SvwjaJU8LD6GSmtm1PHnBG2xPuxwZFy', 'litecoin', 'testnet');
116116
valid('QjpzxpbLp5pCGsCczMbfh1uhC3P89QZavY', 'litecoin', 'testnet');
@@ -435,7 +435,7 @@ describe('WAValidator.validate()', function () {
435435
valid('4swhHtxKapQbj3TZEipgtp7NQzcRWDYqCxXYoPQWjGyHmhxS1w1TjUEszCQT1sQucGwmPQMYdv1FYs3d51KgoubviPBf', 'cardano');
436436
valid('addr1qxnv5u3vrx2t37h3u27qd5ukgcjmrl4f8mu9f5sza3h20cxsfjh80un9kvlggfcdw8fp5kqp9tztqnee9msd0qsafhdsyqclvk', 'cardano');
437437
valid('ADDR1QXNV5U3VRX2T37H3U27QD5UKGCJMRL4F8MU9F5SZA3H20CXSFJH80UN9KVLGGFCDW8FP5KQP9TZTQNEE9MSD0QSAFHDSYQCLVK', 'cardano');
438-
valid('addr1qxclz0u9guazk70l9vv3xf67wx3psx3dekasvy43xfvz56qcs6f7ssw2x0fcesudyj8h224rnzkae2lqlnw8f3353t3sjggfx0', 'cardano');
438+
valid('addr1qxclz0u9guazk70l9vv3xf67wx3psx3dekasvy43xfvz56qcs6f7ssw2x0fcesudyj8h224rnzkae2lqlnw8f3353t3sjggfx0', 'cardano');
439439
});
440440

441441
it('should match the expected Cardano address type', function () {
@@ -811,6 +811,7 @@ describe('WAValidator.validate()', function () {
811811
valid('GACXQEAXYBEZLBMQ2XETOBRO4P66FZAJENDHOQRYPUIXZIIXLKMZEXBJ', 'stellar');
812812
valid('GDD3XRXU3G4DXHVRUDH7LJM4CD4PDZTVP4QHOO4Q6DELKXUATR657OZV', 'stellar');
813813
valid('GDTYVCTAUQVPKEDZIBWEJGKBQHB4UGGXI2SXXUEW7LXMD4B7MK37CWLJ', 'stellar');
814+
valid('GDC5UWE3G6Z4KYOTET5NOCRIVBKWH7MOCSPZPHF4GHQ6XUDD27ACOACD', 'stellar');
814815
});
815816

816817
it('should match the expected stellar address type', function () {
@@ -908,8 +909,8 @@ describe('WAValidator.validate()', function () {
908909
invalid('3NJZLcZEEYBpxYEUGewU4knsQRn1WM5Fkt', 'bitcoincash');
909910
invalid('2MxKEf2su6FGAUfCEAHreGFQvEYrfYNHvL7', 'bitcoincash', 'testnet');
910911
// bitcoincash
911-
invalid('bitcoincash:qp3wjpa3tjlj042z2wv7hahsldgwhwy0rq9sywjpyya', 'bch');
912-
invalid('qp3wjpa3tjlj042z2wv7hahsldgwhwy0rq9sywjpyya', 'bch');
912+
invalid('bitcoincash:qp3wjpa3tjlj042z2wv7hahsldgwhwy0rq9sywjpyya', 'bch');
913+
invalid('qp3wjpa3tjlj042z2wv7hahsldgwhwy0rq9sywjpyya', 'bch');
913914
});
914915

915916
it('should return false for incorrect litecoin addresses', function () {
@@ -1242,7 +1243,7 @@ describe('WAValidator.validate()', function () {
12421243
invalid('xrb_1111111112111111111111111111111111111111111111111111hifc8npp', 'bnb');
12431244
invalid('nano_111111111111111111111111111111111111111111111111111hifc8npp', 'bnb');
12441245
});
1245-
1246+
12461247
it('should return false for incorrect xtz(tezos) address', function () {
12471248
commonTests('xtz');
12481249
invalid('SBGWKM3CD4IL47QN6X54N6Y33T3JDNVI6AIJ6CD5IM47HG3IG4O36XCU', 'xtz');

0 commit comments

Comments
 (0)