Skip to content

Commit 4a62457

Browse files
committed
rollback changes for token length 10
1 parent d60fb3b commit 4a62457

File tree

3 files changed

+101
-9
lines changed

3 files changed

+101
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "chainsql-binary-codec",
3-
"version": "0.1.31",
3+
"version": "0.1.33",
44
"description": "chainsql binary codec",
55
"files": [
66
"distrib/npm/*",

src/types/currency.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@ 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,10}$/;
5+
const ISO_REGEX = /^[A-Z0-9]{3}$/;
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[9] = isoBytes.length;
13-
bytes.set(isoBytes, 10);
12+
bytes.set(isoBytes, 12);
1413
}
1514
return bytes;
1615
}
1716

1817
function isISOCode(val) {
19-
return ISO_REGEX.test(val);
18+
return val.length === 3; // ISO_REGEX.test(val);
2019
}
2120

2221
function isHex(val) {
@@ -39,7 +38,7 @@ function bytesFromRepr(val) {
3938
if (isValidRepr(val)) {
4039
// We assume at this point that we have an object with a length, either 3,
4140
// 20 or 40.
42-
return val.length >= 3 ? isoToBytes(val) : val;
41+
return val.length === 3 ? isoToBytes(val) : val;
4342
}
4443
throw new Error(`Unsupported Currency repr: ${val}`);
4544
}
@@ -67,12 +66,11 @@ const Currency = makeClass({
6766
let onlyISO = true;
6867

6968
const bytes = this._bytes;
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);
69+
const code = slice(this._bytes, 12, 15, Array);
7270
const iso = code.map(c => String.fromCharCode(c)).join('');
7371

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

src/types/currencyNew.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
const _ = require('lodash');
2+
const makeClass = require('../utils/make-class');
3+
const {slice} = require('../utils/bytes-utils');
4+
const {Hash160} = require('./hash-160');
5+
const ISO_REGEX = /^[A-Z0-9]{3,10}$/;
6+
const HEX_REGEX = /^[A-F0-9]{40}$/;
7+
8+
function isoToBytes(iso) {
9+
const bytes = new Uint8Array(20);
10+
if (iso !== 'ZXC') {
11+
const isoBytes = iso.split('').map(c => c.charCodeAt(0));
12+
bytes[9] = isoBytes.length;
13+
bytes.set(isoBytes, 10);
14+
}
15+
return bytes;
16+
}
17+
18+
function isISOCode(val) {
19+
return ISO_REGEX.test(val);
20+
}
21+
22+
function isHex(val) {
23+
return HEX_REGEX.test(val);
24+
}
25+
26+
function isStringRepr(val) {
27+
return _.isString(val) && (isISOCode(val) || isHex(val));
28+
}
29+
30+
function isBytesArray(val) {
31+
return val.length === 20;
32+
}
33+
34+
function isValidRepr(val) {
35+
return isStringRepr(val) || isBytesArray(val);
36+
}
37+
38+
function bytesFromRepr(val) {
39+
if (isValidRepr(val)) {
40+
// We assume at this point that we have an object with a length, either 3,
41+
// 20 or 40.
42+
return val.length >= 3 ? isoToBytes(val) : val;
43+
}
44+
throw new Error(`Unsupported Currency repr: ${val}`);
45+
}
46+
47+
const $uper = Hash160.prototype;
48+
const Currency = makeClass({
49+
inherits: Hash160,
50+
getters: ['isNative', 'iso'],
51+
statics: {
52+
init() {
53+
this.ZXC = new this(new Uint8Array(20));
54+
},
55+
from(val) {
56+
return val instanceof this ? val : new this(bytesFromRepr(val));
57+
}
58+
},
59+
Currency(bytes) {
60+
Hash160.call(this, bytes);
61+
this.classify();
62+
},
63+
classify() {
64+
// We only have a non null iso() property available if the currency can be
65+
// losslessly represented by the 3 letter iso code. If none is available a
66+
// hex encoding of the full 20 bytes is the canonical representation.
67+
let onlyISO = true;
68+
69+
const bytes = this._bytes;
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);
72+
const iso = code.map(c => String.fromCharCode(c)).join('');
73+
74+
for (let i = bytes.length - 1; i >= 0; i--) {
75+
if (bytes[i] !== 0 && !(i>=9 && i<10+length)) {
76+
onlyISO = false;
77+
break;
78+
}
79+
}
80+
const lossLessISO = onlyISO && iso !== 'ZXC' && ISO_REGEX.test(iso);
81+
this._isNative = onlyISO && _.isEqual(code, [0, 0, 0]);
82+
this._iso = this._isNative ? 'ZXC' : lossLessISO ? iso : null;
83+
},
84+
toJSON() {
85+
if (this.iso()) {
86+
return this.iso();
87+
}
88+
return $uper.toJSON.call(this);
89+
}
90+
});
91+
92+
module.exports = {
93+
Currency
94+
};

0 commit comments

Comments
 (0)