Add serialization of XRP as an IOU if explicitly asked for.

Unfortunately, there are some transactions in the history that use this format,
so we need to support it in order to be able to verify historic ledgers.
This commit is contained in:
Stefan Thomas
2013-11-08 16:36:37 -08:00
parent 1355dc135f
commit 34ea528ade
2 changed files with 14 additions and 3 deletions

View File

@@ -278,14 +278,14 @@ STHash160.id = 17;
// Internal
var STCurrency = new SerializedType({
serialize: function (so, val) {
serialize: function (so, val, xrp_as_ascii) {
var currency = val.to_json().toUpperCase();
if (!isCurrencyString(currency)) {
throw new Error('Tried to serialize invalid/unimplemented currency type.');
}
if (currency === 'XRP') {
if (currency === 'XRP' && !xrp_as_ascii) {
serialize_hex(so, UInt160.HEX_ZERO, true);
} else {
var currencyCode = currency.toUpperCase();
@@ -361,7 +361,7 @@ var STAmount = exports.Amount = new SerializedType({
if (!amount.is_native()) {
// Currency (160-bit hash)
var currency = amount.currency();
STCurrency.serialize(so, currency);
STCurrency.serialize(so, currency, true);
// Issuer (160-bit hash)
so.append(amount.issuer().to_bytes());

View File

@@ -494,6 +494,17 @@ describe('Serialized types', function() {
types.Amount.serialize(so, '-1/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
assert.strictEqual(so.to_hex(), '94838D7EA4C680000000000000000000000000005553440000000000B5F762798A53D543A014CAF8B297CFF8F2F937E8');
});
it('Serialize 15/XRP/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', function () {
// This actually appears in the ledger, so we need to be able to serialize
// Transaction #A2AD66C93C7B7277CD5AEB718A4E82D88C7099129948BC66A394EE38B34657A9
var so = new SerializedObject();
types.Amount.serialize(so, {
"value":"1000",
"currency":"XRP",
"issuer":"rM1oqKtfh1zgjdAgbFmaRm3btfGBX25xVo"
});
assert.strictEqual(so.to_hex(), 'D5438D7EA4C680000000000000000000000000005852500000000000E4FE687C90257D3D2D694C8531CDEECBE84F3367');
});
it('Parse 1 XRP', function () {
var so = new SerializedObject('4000000000000001');
assert.strictEqual(types.Amount.parse(so).to_json(), '1');