Skip to content

Commit 062c97f

Browse files
committed
support coin length to 10
1 parent 3fce2a0 commit 062c97f

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/types/currency.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@ const _ = require('lodash');
22
const makeClass = require('../utils/make-class');
33
const {slice} = require('../utils/bytes-utils');
44
const {Hash160} = require('./hash-160');
5-
const ISO_REGEX = /^[A-Z0-9]{3}$/;
5+
const ISO_REGEX = /^[A-Z0-9]{3,10}$/;
66
const HEX_REGEX = /^[A-F0-9]{40}$/;
77

88
function isoToBytes(iso) {
99
const bytes = new Uint8Array(20);
1010
if (iso !== 'ZXC') {
1111
const isoBytes = iso.split('').map(c => c.charCodeAt(0));
12-
bytes.set(isoBytes, 12);
12+
bytes[9] = isoBytes.length;
13+
bytes.set(isoBytes, 10);
1314
}
1415
return bytes;
1516
}
1617

1718
function isISOCode(val) {
18-
return val.length === 3; // ISO_REGEX.test(val);
19+
return ISO_REGEX.test(val);
1920
}
2021

2122
function isHex(val) {
@@ -38,7 +39,7 @@ function bytesFromRepr(val) {
3839
if (isValidRepr(val)) {
3940
// We assume at this point that we have an object with a length, either 3,
4041
// 20 or 40.
41-
return val.length === 3 ? isoToBytes(val) : val;
42+
return val.length >= 3 ? isoToBytes(val) : val;
4243
}
4344
throw new Error(`Unsupported Currency repr: ${val}`);
4445
}
@@ -66,11 +67,12 @@ const Currency = makeClass({
6667
let onlyISO = true;
6768

6869
const bytes = this._bytes;
69-
const code = slice(this._bytes, 12, 15, Array);
70+
const length = bytes[9]? bytes[9]:3;
71+
const code = bytes[9]? slice(this._bytes, 10, 10+length, Array) : slice(this._bytes, 12, 12+length, Array);
7072
const iso = code.map(c => String.fromCharCode(c)).join('');
7173

7274
for (let i = bytes.length - 1; i >= 0; i--) {
73-
if (bytes[i] !== 0 && !(i === 12 || i === 13 || i === 14)) {
75+
if (bytes[i] !== 0 && !(i>=9 && i<10+length)) {
7476
onlyISO = false;
7577
break;
7678
}

0 commit comments

Comments
 (0)