@@ -2,20 +2,21 @@ const _ = require('lodash');
2
2
const makeClass = require ( '../utils/make-class' ) ;
3
3
const { slice} = require ( '../utils/bytes-utils' ) ;
4
4
const { Hash160} = require ( './hash-160' ) ;
5
- const ISO_REGEX = / ^ [ A - Z 0 - 9 ] { 3 } $ / ;
5
+ const ISO_REGEX = / ^ [ A - Z 0 - 9 ] { 3 , 10 } $ / ;
6
6
const HEX_REGEX = / ^ [ A - F 0 - 9 ] { 40 } $ / ;
7
7
8
8
function isoToBytes ( iso ) {
9
9
const bytes = new Uint8Array ( 20 ) ;
10
10
if ( iso !== 'ZXC' ) {
11
11
const isoBytes = iso . split ( '' ) . map ( c => c . charCodeAt ( 0 ) ) ;
12
- bytes . set ( isoBytes , 12 ) ;
12
+ bytes [ 9 ] = isoBytes . length ;
13
+ bytes . set ( isoBytes , 10 ) ;
13
14
}
14
15
return bytes ;
15
16
}
16
17
17
18
function isISOCode ( val ) {
18
- return val . length === 3 ; // ISO_REGEX.test(val);
19
+ return ISO_REGEX . test ( val ) ;
19
20
}
20
21
21
22
function isHex ( val ) {
@@ -38,7 +39,7 @@ function bytesFromRepr(val) {
38
39
if ( isValidRepr ( val ) ) {
39
40
// We assume at this point that we have an object with a length, either 3,
40
41
// 20 or 40.
41
- return val . length == = 3 ? isoToBytes ( val ) : val ;
42
+ return val . length > = 3 ? isoToBytes ( val ) : val ;
42
43
}
43
44
throw new Error ( `Unsupported Currency repr: ${ val } ` ) ;
44
45
}
@@ -66,11 +67,12 @@ const Currency = makeClass({
66
67
let onlyISO = true ;
67
68
68
69
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 ) ;
70
72
const iso = code . map ( c => String . fromCharCode ( c ) ) . join ( '' ) ;
71
73
72
74
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 ) ) {
74
76
onlyISO = false ;
75
77
break ;
76
78
}
0 commit comments