ripple-binary-codec 1.3.1 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/types/currency.js +10 -7
- package/dist/types/currency.js.map +1 -1
- package/package.json +2 -3
- package/test/hash.test.js +15 -4
- package/test/tx-encode-decode.test.js +22 -0
package/dist/types/currency.js
CHANGED
|
@@ -18,8 +18,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
18
18
|
exports.Currency = void 0;
|
|
19
19
|
var hash_160_1 = require("./hash-160");
|
|
20
20
|
var buffer_1 = require("buffer/");
|
|
21
|
+
var XRP_HEX_REGEX = /^0{40}$/;
|
|
21
22
|
var ISO_REGEX = /^[A-Z0-9]{3}$/;
|
|
22
23
|
var HEX_REGEX = /^[A-F0-9]{40}$/;
|
|
24
|
+
// eslint-disable-next-line no-control-regex
|
|
25
|
+
var STANDARD_FORMAT_HEX_REGEX = /^0{24}[\x00-\x7F]{6}0{10}$/;
|
|
23
26
|
/**
|
|
24
27
|
* Convert an ISO code to a currency bytes representation
|
|
25
28
|
*/
|
|
@@ -40,7 +43,7 @@ function isIsoCode(iso) {
|
|
|
40
43
|
function isoCodeFromHex(code) {
|
|
41
44
|
var iso = code.toString();
|
|
42
45
|
if (iso === 'XRP') {
|
|
43
|
-
|
|
46
|
+
return null;
|
|
44
47
|
}
|
|
45
48
|
if (isIsoCode(iso)) {
|
|
46
49
|
return iso;
|
|
@@ -89,15 +92,15 @@ var Currency = /** @class */ (function (_super) {
|
|
|
89
92
|
__extends(Currency, _super);
|
|
90
93
|
function Currency(byteBuf) {
|
|
91
94
|
var _this = _super.call(this, byteBuf !== null && byteBuf !== void 0 ? byteBuf : Currency.XRP.bytes) || this;
|
|
92
|
-
var
|
|
93
|
-
if (
|
|
94
|
-
_this._iso = null;
|
|
95
|
-
}
|
|
96
|
-
else if (/^0*$/.test(_this.bytes.toString('hex'))) {
|
|
95
|
+
var hex = _this.bytes.toString('hex');
|
|
96
|
+
if (XRP_HEX_REGEX.test(hex)) {
|
|
97
97
|
_this._iso = 'XRP';
|
|
98
98
|
}
|
|
99
|
+
else if (STANDARD_FORMAT_HEX_REGEX.test(hex)) {
|
|
100
|
+
_this._iso = isoCodeFromHex(_this.bytes.slice(12, 15));
|
|
101
|
+
}
|
|
99
102
|
else {
|
|
100
|
-
_this._iso =
|
|
103
|
+
_this._iso = null;
|
|
101
104
|
}
|
|
102
105
|
return _this;
|
|
103
106
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"currency.js","sourceRoot":"","sources":["../../src/types/currency.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,uCAAoC;AACpC,kCAAgC;AAEhC,IAAM,SAAS,GAAG,eAAe,CAAA;AACjC,IAAM,SAAS,GAAG,gBAAgB,CAAA;
|
|
1
|
+
{"version":3,"file":"currency.js","sourceRoot":"","sources":["../../src/types/currency.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,uCAAoC;AACpC,kCAAgC;AAEhC,IAAM,aAAa,GAAG,SAAS,CAAA;AAC/B,IAAM,SAAS,GAAG,eAAe,CAAA;AACjC,IAAM,SAAS,GAAG,gBAAgB,CAAA;AAClC,4CAA4C;AAC5C,IAAM,yBAAyB,GAAG,4BAA4B,CAAA;AAE9D;;GAEG;AACH,SAAS,UAAU,CAAC,GAAW;IAC7B,IAAM,KAAK,GAAG,eAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;IAC9B,IAAI,GAAG,KAAK,KAAK,EAAE;QACjB,IAAM,QAAQ,GAAG,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,UAAC,CAAC,IAAK,OAAA,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,EAAf,CAAe,CAAC,CAAA;QAC1D,KAAK,CAAC,GAAG,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAA;KACxB;IACD,OAAO,KAAK,CAAA;AACd,CAA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AM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAA;IAsDtD,eAAC;CAAA,AAvDD,CAAuB,kBAAO,GAuD7B;AAEQ,4BAAQ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ripple-binary-codec",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.2",
|
|
4
4
|
"description": "XRP Ledger binary codec",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist/*",
|
|
@@ -41,6 +41,5 @@
|
|
|
41
41
|
"prettier": "@xrplf/prettier-config",
|
|
42
42
|
"engines": {
|
|
43
43
|
"node": ">=10.22.0"
|
|
44
|
-
}
|
|
45
|
-
"gitHead": "e4c7923bf3ff1195b47ce045e31b647c1a3021c1"
|
|
44
|
+
}
|
|
46
45
|
}
|
package/test/hash.test.js
CHANGED
|
@@ -50,10 +50,9 @@ describe('Hash256', function () {
|
|
|
50
50
|
})
|
|
51
51
|
|
|
52
52
|
describe('Currency', function () {
|
|
53
|
-
test('
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
).toThrow()
|
|
53
|
+
test('Decoding allows dodgy XRP without throwing', function () {
|
|
54
|
+
const currencyCode = '0000000000000000000000005852500000000000'
|
|
55
|
+
expect(Currency.from(currencyCode).toJSON()).toBe(currencyCode)
|
|
57
56
|
})
|
|
58
57
|
test('Currency with lowercase letters decode to hex', () => {
|
|
59
58
|
expect(Currency.from('xRp').toJSON()).toBe(
|
|
@@ -70,6 +69,18 @@ describe('Currency', function () {
|
|
|
70
69
|
expect(Currency.from('USD').toJSON()).toBe('USD')
|
|
71
70
|
})
|
|
72
71
|
|
|
72
|
+
test('Currency codes with no contiguous zeroes in first 96 type code & reserved bits', function () {
|
|
73
|
+
expect(
|
|
74
|
+
Currency.from('0000000023410000000000005852520000000000').iso(),
|
|
75
|
+
).toBe(null)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
test('Currency codes with no contiguous zeroes in last 40 reserved bits', function () {
|
|
79
|
+
expect(
|
|
80
|
+
Currency.from('0000000000000000000000005852527570656500').iso(),
|
|
81
|
+
).toBe(null)
|
|
82
|
+
})
|
|
83
|
+
|
|
73
84
|
test('can be constructed from a Buffer', function () {
|
|
74
85
|
const xrp = new Currency(Buffer.alloc(20))
|
|
75
86
|
expect(xrp.iso()).toBe('XRP')
|
|
@@ -50,6 +50,28 @@ describe('encoding and decoding tx_json', function () {
|
|
|
50
50
|
const decoded = decode(encoded)
|
|
51
51
|
expect(my_tx).toEqual(decoded)
|
|
52
52
|
})
|
|
53
|
+
test('can decode a transaction with an issued currency that evaluates to XRP', function () {
|
|
54
|
+
// Encoding is done prior, because this is disallowed during encoding with client libraries to avoid scam XRP tokens.
|
|
55
|
+
const expectedTx = {
|
|
56
|
+
TransactionType: 'TrustSet',
|
|
57
|
+
Flags: 0,
|
|
58
|
+
Sequence: 19,
|
|
59
|
+
LimitAmount: {
|
|
60
|
+
value: '200',
|
|
61
|
+
currency: '0000000000000000000000005852500000000000',
|
|
62
|
+
issuer: 'r9hEDb4xBGRfBCcX3E4FirDWQBAYtpxC8K',
|
|
63
|
+
},
|
|
64
|
+
Fee: '10',
|
|
65
|
+
SigningPubKey:
|
|
66
|
+
'023076CBB7A61837F1A23D4A3DD7CE810B694992EB0959AB9D6F4BB6FED6F8CC26',
|
|
67
|
+
TxnSignature:
|
|
68
|
+
'304502202D0CD77D8E765E3783C309CD663723B18406B7950A348A6F301492916A990FC70221008A76D586111205304F10ADEFDFDDAF804EF202D8CD1E492DC6E1AA8030EA1844',
|
|
69
|
+
Account: 'rPtfQWdcdhuL9eNeNv5YfmekSX3K7vJHbG',
|
|
70
|
+
}
|
|
71
|
+
const encoded = encode(expectedTx)
|
|
72
|
+
const decoded = decode(encoded)
|
|
73
|
+
expect(expectedTx).toEqual(decoded)
|
|
74
|
+
})
|
|
53
75
|
test('throws when Amount is invalid', function () {
|
|
54
76
|
const my_tx = Object.assign({}, tx_json, {
|
|
55
77
|
Amount: '1000.001',
|