|
| 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