Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4bba55d

Browse files
committedDec 4, 2014
Revert "[FEATURE] improve memo support"
This reverts commit 89adcf4.
1 parent b4cabad commit 4bba55d

File tree

5 files changed

+71
-514
lines changed

5 files changed

+71
-514
lines changed
 

‎src/js/ripple/binformat.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
* Data type map.
33
*
44
* Mapping of type ids to data types. The type id is specified by the high
5-
*
6-
* For reference, see rippled's definition:
7-
* https://github.com/ripple/rippled/blob/develop/src/ripple/data/protocol/SField.cpp
85
*/
96
var TYPES_MAP = exports.types = [
107
void(0),
@@ -378,7 +375,7 @@ exports.ledger = {
378375
['Balance', REQUIRED],
379376
['LowLimit', REQUIRED],
380377
['HighLimit', REQUIRED]])
381-
};
378+
}
382379

383380
exports.metadata = [
384381
[ 'TransactionIndex' , REQUIRED ],

‎src/js/ripple/serializedtypes.js

+32-150
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ var Currency = amount.Currency;
2424
// Shortcuts
2525
var hex = sjcl.codec.hex;
2626
var bytes = sjcl.codec.bytes;
27-
var utf8 = sjcl.codec.utf8String;
2827

2928
var BigInteger = utils.jsbn.BigInteger;
3029

@@ -53,7 +52,7 @@ function isBigInteger(val) {
5352
return val instanceof BigInteger;
5453
};
5554

56-
function serializeHex(so, hexData, noLength) {
55+
function serialize_hex(so, hexData, noLength) {
5756
var byteData = bytes.fromBits(hex.toBits(hexData));
5857
if (!noLength) {
5958
SerializedType.serialize_varint(so, byteData.length);
@@ -64,18 +63,10 @@ function serializeHex(so, hexData, noLength) {
6463
/**
6564
* parses bytes as hex
6665
*/
67-
function convertByteArrayToHex (byte_array) {
66+
function convert_bytes_to_hex (byte_array) {
6867
return sjcl.codec.hex.fromBits(sjcl.codec.bytes.toBits(byte_array)).toUpperCase();
6968
};
7069

71-
function convertStringToHex(string) {
72-
return hex.fromBits(utf8.toBits(string)).toUpperCase();
73-
}
74-
75-
function convertHexToString(hexString) {
76-
return utf8.fromBits(hex.toBits(hexString));
77-
}
78-
7970
SerializedType.serialize_varint = function (so, val) {
8071
if (val < 0) {
8172
throw new Error('Variable integers are unsigned.');
@@ -124,7 +115,7 @@ SerializedType.prototype.parse_varint = function (so) {
124115
*
125116
* The result is appended to the serialized object ('so').
126117
*/
127-
function convertIntegerToByteArray(val, bytes) {
118+
function append_byte_array(so, val, bytes) {
128119
if (!isNumber(val)) {
129120
throw new Error('Value is not a number', bytes);
130121
}
@@ -139,7 +130,7 @@ function convertIntegerToByteArray(val, bytes) {
139130
newBytes.unshift(val >>> (i * 8) & 0xff);
140131
}
141132

142-
return newBytes;
133+
so.append(newBytes);
143134
};
144135

145136
// Convert a certain number of bytes from the serialized object ('so') into an integer.
@@ -161,7 +152,7 @@ function readAndSum(so, bytes) {
161152

162153
var STInt8 = exports.Int8 = new SerializedType({
163154
serialize: function (so, val) {
164-
so.append(convertIntegerToByteArray(val, 1));
155+
append_byte_array(so, val, 1);
165156
},
166157
parse: function (so) {
167158
return readAndSum(so, 1);
@@ -172,7 +163,7 @@ STInt8.id = 16;
172163

173164
var STInt16 = exports.Int16 = new SerializedType({
174165
serialize: function (so, val) {
175-
so.append(convertIntegerToByteArray(val, 2));
166+
append_byte_array(so, val, 2);
176167
},
177168
parse: function (so) {
178169
return readAndSum(so, 2);
@@ -183,7 +174,7 @@ STInt16.id = 1;
183174

184175
var STInt32 = exports.Int32 = new SerializedType({
185176
serialize: function (so, val) {
186-
so.append(convertIntegerToByteArray(val, 4));
177+
append_byte_array(so, val, 4);
187178
},
188179
parse: function (so) {
189180
return readAndSum(so, 4);
@@ -226,7 +217,7 @@ var STInt64 = exports.Int64 = new SerializedType({
226217
hex = '0' + hex;
227218
}
228219

229-
serializeHex(so, hex, true); //noLength = true
220+
serialize_hex(so, hex, true); //noLength = true
230221
},
231222
parse: function (so) {
232223
var bytes = so.read(8);
@@ -246,7 +237,7 @@ var STHash128 = exports.Hash128 = new SerializedType({
246237
if (!hash.is_valid()) {
247238
throw new Error('Invalid Hash128');
248239
}
249-
serializeHex(so, hash.to_hex(), true); //noLength = true
240+
serialize_hex(so, hash.to_hex(), true); //noLength = true
250241
},
251242
parse: function (so) {
252243
return UInt128.from_bytes(so.read(16));
@@ -261,7 +252,7 @@ var STHash256 = exports.Hash256 = new SerializedType({
261252
if (!hash.is_valid()) {
262253
throw new Error('Invalid Hash256');
263254
}
264-
serializeHex(so, hash.to_hex(), true); //noLength = true
255+
serialize_hex(so, hash.to_hex(), true); //noLength = true
265256
},
266257
parse: function (so) {
267258
return UInt256.from_bytes(so.read(32));
@@ -276,7 +267,7 @@ var STHash160 = exports.Hash160 = new SerializedType({
276267
if (!hash.is_valid()) {
277268
throw new Error('Invalid Hash160');
278269
}
279-
serializeHex(so, hash.to_hex(), true); //noLength = true
270+
serialize_hex(so, hash.to_hex(), true); //noLength = true
280271
},
281272
parse: function (so) {
282273
return UInt160.from_bytes(so.read(20));
@@ -303,7 +294,7 @@ var STCurrency = new SerializedType({
303294
// UInt160 value and consider it valid. But it doesn't, so for the
304295
// deserialization to be usable, we need to allow invalid results for now.
305296
//if (!currency.is_valid()) {
306-
// throw new Error('Invalid currency: '+convertByteArrayToHex(bytes));
297+
// throw new Error('Invalid currency: '+convert_bytes_to_hex(bytes));
307298
//}
308299
return currency;
309300
}
@@ -418,16 +409,15 @@ STAmount.id = 6;
418409

419410
var STVL = exports.VariableLength = exports.VL = new SerializedType({
420411
serialize: function (so, val) {
421-
422412
if (typeof val === 'string') {
423-
serializeHex(so, val);
413+
serialize_hex(so, val);
424414
} else {
425415
throw new Error('Unknown datatype.');
426416
}
427417
},
428418
parse: function (so) {
429419
var len = this.parse_varint(so);
430-
return convertByteArrayToHex(so.read(len));
420+
return convert_bytes_to_hex(so.read(len));
431421
}
432422
});
433423

@@ -439,7 +429,7 @@ var STAccount = exports.Account = new SerializedType({
439429
if (!account.is_valid()) {
440430
throw new Error('Invalid account!');
441431
}
442-
serializeHex(so, account.to_hex());
432+
serialize_hex(so, account.to_hex());
443433
},
444434
parse: function (so) {
445435
var len = this.parse_varint(so);
@@ -451,6 +441,7 @@ var STAccount = exports.Account = new SerializedType({
451441
var result = UInt160.from_bytes(so.read(len));
452442
result.set_version(Base.VER_ACCOUNT_ID);
453443

444+
//console.log('PARSED 160:', result.to_json());
454445
if (false && !result.is_valid()) {
455446
throw new Error('Invalid Account');
456447
}
@@ -602,104 +593,6 @@ var STVector256 = exports.Vector256 = new SerializedType({
602593

603594
STVector256.id = 19;
604595

605-
// Internal
606-
var STMemo = exports.STMemo = new SerializedType({
607-
serialize: function(so, val, no_marker) {
608-
609-
var keys = [];
610-
611-
Object.keys(val).forEach(function (key) {
612-
// Ignore lowercase field names - they're non-serializable fields by
613-
// convention.
614-
if (key[0] === key[0].toLowerCase()) {
615-
return;
616-
}
617-
618-
if (typeof binformat.fieldsInverseMap[key] === 'undefined') {
619-
throw new Error('JSON contains unknown field: "' + key + '"');
620-
}
621-
622-
keys.push(key);
623-
});
624-
625-
// Sort fields
626-
keys = sort_fields(keys);
627-
628-
// store that we're dealing with json
629-
var isJson = val.MemoFormat === 'json';
630-
631-
for (var i=0; i<keys.length; i++) {
632-
var key = keys[i];
633-
switch (key) {
634-
635-
// MemoType and MemoFormat are always ASCII strings
636-
case 'MemoType':
637-
case 'MemoFormat':
638-
val[key] = convertStringToHex(val[key]);
639-
break;
640-
641-
// MemoData can be a JSON object, otherwise it's a string
642-
case 'MemoData':
643-
if (typeof val[key] !== 'string') {
644-
if (isJson) {
645-
try {
646-
val[key] = convertStringToHex(JSON.stringify(val[key]));
647-
} catch (e) {
648-
throw new Error('MemoFormat json with invalid JSON in MemoData field');
649-
}
650-
} else {
651-
throw new Error('MemoData can only be a JSON object with a valid json MemoFormat');
652-
}
653-
} else if (isString(val[key])) {
654-
val[key] = convertStringToHex(val[key]);
655-
}
656-
break;
657-
}
658-
659-
serialize(so, key, val[key]);
660-
}
661-
662-
if (!no_marker) {
663-
//Object ending marker
664-
STInt8.serialize(so, 0xe1);
665-
}
666-
667-
},
668-
parse: function(so) {
669-
var output = {};
670-
while (so.peek(1)[0] !== 0xe1) {
671-
var keyval = parse(so);
672-
output[keyval[0]] = keyval[1];
673-
}
674-
675-
if (output['MemoType'] !== void(0)) {
676-
output['parsed_memo_type'] = convertHexToString(output['MemoType']);
677-
}
678-
679-
if (output['MemoFormat'] !== void(0)) {
680-
output['parsed_memo_format'] = convertHexToString(output['MemoFormat']);
681-
}
682-
683-
if (output['MemoData'] !== void(0)) {
684-
685-
// see if we can parse JSON
686-
if (output['parsed_memo_format'] === 'json') {
687-
try {
688-
output['parsed_memo_data'] = JSON.parse(convertHexToString(output['MemoData']));
689-
} catch(e) {
690-
// fail, which is fine, we just won't add the memo_data field
691-
}
692-
} else if(output['parsed_memo_format'] === 'text') {
693-
output['parsed_memo_data'] = convertHexToString(output['MemoData']);
694-
}
695-
}
696-
697-
so.read(1);
698-
return output;
699-
}
700-
701-
});
702-
703596
exports.serialize = exports.serialize_whatever = serialize;
704597

705598
function serialize(so, field_name, value) {
@@ -729,15 +622,9 @@ function serialize(so, field_name, value) {
729622
STInt8.serialize(so, field_bits);
730623
}
731624

732-
// Get the serializer class (ST...)
733-
var serialized_object_type;
734-
if (field_name === 'Memo' && typeof value === 'object') {
735-
// for Memo we override the default behavior with our STMemo serializer
736-
serialized_object_type = exports.STMemo;
737-
} else {
738-
// for a field based on the type bits.
739-
serialized_object_type = exports[binformat.types[type_bits]];
740-
}
625+
// Get the serializer class (ST...) for a field based on the type bits.
626+
var serialized_object_type = exports[binformat.types[type_bits]];
627+
//do something with val[keys] and val[keys[i]];
741628

742629
try {
743630
serialized_object_type.serialize(so, value);
@@ -758,21 +645,18 @@ function parse(so) {
758645
type_bits = so.read(1)[0];
759646
}
760647

648+
// Get the parser class (ST...) for a field based on the type bits.
649+
var type = exports[binformat.types[type_bits]];
650+
651+
assert(type, 'Unknown type - header byte is 0x' + tag_byte.toString(16));
761652

762653
var field_bits = tag_byte & 0x0f;
763654
var field_name = (field_bits === 0)
764-
? field_name = binformat.fields[type_bits][so.read(1)[0]]
765-
: field_name = binformat.fields[type_bits][field_bits];
655+
? field_name = binformat.fields[type_bits][so.read(1)[0]]
656+
: field_name = binformat.fields[type_bits][field_bits];
766657

767658
assert(field_name, 'Unknown field - header byte is 0x' + tag_byte.toString(16));
768659

769-
// Get the parser class (ST...) for a field based on the type bits.
770-
var type = (field_name === 'Memo')
771-
? exports.STMemo
772-
: exports[binformat.types[type_bits]];
773-
774-
assert(type, 'Unknown type - header byte is 0x' + tag_byte.toString(16));
775-
776660
return [ field_name, type.parse(so) ]; //key, value
777661
};
778662

@@ -794,20 +678,18 @@ function sort_fields(keys) {
794678

795679
var STObject = exports.Object = new SerializedType({
796680
serialize: function (so, val, no_marker) {
797-
var keys = [];
681+
var keys = Object.keys(val);
798682

799-
Object.keys(val).forEach(function (key) {
800-
// Ignore lowercase field names - they're non-serializable fields by
801-
// convention.
802-
if (key[0] === key[0].toLowerCase()) {
803-
return;
804-
}
683+
// Ignore lowercase field names - they're non-serializable fields by
684+
// convention.
685+
keys = keys.filter(function (key) {
686+
return key[0] !== key[0].toLowerCase();
687+
});
805688

689+
keys.forEach(function (key) {
806690
if (typeof binformat.fieldsInverseMap[key] === 'undefined') {
807691
throw new Error('JSON contains unknown field: "' + key + '"');
808692
}
809-
810-
keys.push(key);
811693
});
812694

813695
// Sort fields

‎src/js/ripple/transaction.js

+19-33
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,6 @@ Transaction.prototype.consts = {
181181
tecCLAIMED: 100
182182
};
183183

184-
Transaction.prototype.ascii_regex = /^[\x00-\x7F]*$/;
185-
186184
Transaction.from_json = function(j) {
187185
return (new Transaction()).parseJson(j);
188186
};
@@ -622,52 +620,40 @@ Transaction.prototype.setFlags = function(flags) {
622620
};
623621

624622
/**
625-
* Add a Memo to transaction.
623+
* Add a Memo to transaction. Memos can be used as key-value,
624+
* using the MemoType as a key
626625
*
627-
* @param {String} memoType - describes what the data represents, needs to be valid ASCII
628-
* @param {String} memoFormat - describes what format the data is in, MIME type, needs to be valid ASCII
629-
* @param {String} memoData - data for the memo, can be any JS object. Any object other than string will be stringified (JSON) for transport
626+
* @param {String} type
627+
* @param {String} data
630628
*/
631629

632-
Transaction.prototype.addMemo = function(memoType, memoFormat, memoData) {
633-
634-
if (typeof memoType === 'object') {
635-
var opts = memoType;
636-
memoType = opts.memoType;
637-
memoFormat = opts.memoFormat;
638-
memoData = opts.memoData;
639-
}
640-
641-
if (!/(undefined|string)/.test(typeof memoType)) {
630+
Transaction.prototype.addMemo = function(type, data) {
631+
if (!/(undefined|string)/.test(typeof type)) {
642632
throw new Error('MemoType must be a string');
643-
} else if (!this.ascii_regex.test(memoType)) {
644-
throw new Error('MemoType must be valid ASCII');
645633
}
646634

647-
if (!/(undefined|string)/.test(typeof memoFormat)) {
648-
throw new Error('MemoFormat must be a string');
649-
} else if (!this.ascii_regex.test(memoFormat)) {
650-
throw new Error('MemoFormat must be valid ASCII');
635+
if (!/(undefined|string)/.test(typeof data)) {
636+
throw new Error('MemoData must be a string');
651637
}
652638

653-
var memo = {};
639+
function toHex(str) {
640+
return sjcl.codec.hex.fromBits(sjcl.codec.utf8String.toBits(str));
641+
};
642+
643+
var memo = { };
654644

655-
if (memoType) {
656-
if (Transaction.MEMO_TYPES[memoType]) {
645+
if (type) {
646+
if (Transaction.MEMO_TYPES[type]) {
657647
//XXX Maybe in the future we want a schema validator for
658648
//memo types
659-
memo.MemoType = Transaction.MEMO_TYPES[memoType];
649+
memo.MemoType = Transaction.MEMO_TYPES[type];
660650
} else {
661-
memo.MemoType = memoType;
651+
memo.MemoType = toHex(type);
662652
}
663653
}
664654

665-
if (memoFormat) {
666-
memo.MemoFormat = memoFormat;
667-
}
668-
669-
if (memoData) {
670-
memo.MemoData = memoData;
655+
if (data) {
656+
memo.MemoData = toHex(data);
671657
}
672658

673659
this.tx_json.Memos = (this.tx_json.Memos || []).concat({ Memo: memo });

‎test/serializedobject-test.js

+2-189
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ describe('Serialized object', function() {
3535
assert.deepEqual(input_json, output_json);
3636
});
3737
});
38-
3938
describe('#from_json', function() {
4039
it('understands TransactionType as a Number', function() {
4140
var input_json = {
@@ -53,7 +52,6 @@ describe('Serialized object', function() {
5352
assert.equal(0, input_json.TransactionType);
5453
assert.equal("Payment", output_json.TransactionType);
5554
});
56-
5755
it('understands LedgerEntryType as a Number', function() {
5856
var input_json = {
5957
// no, non required fields
@@ -67,7 +65,6 @@ describe('Serialized object', function() {
6765
assert.equal(100, input_json.LedgerEntryType);
6866
assert.equal("DirectoryNode", output_json.LedgerEntryType);
6967
});
70-
7168
describe('Format validation', function() {
7269
// Peercover actually had a problem submitting transactions without a `Fee`
7370
// and rippled was only informing of "transaction is invalid"
@@ -83,198 +80,14 @@ describe('Serialized object', function() {
8380
};
8481
assert.throws (
8582
function() {
86-
SerializedObject.from_json(input_json);
83+
var output_json = SerializedObject.from_json(input_json);
8784
},
8885
/Payment is missing fields: \["Fee"\]/
8986
);
9087
});
9188
});
9289

93-
describe('Memos', function() {
94-
95-
var input_json;
96-
97-
beforeEach(function() {
98-
input_json = {
99-
"Flags": 2147483648,
100-
"TransactionType": "Payment",
101-
"Account": "rhXzSyt1q9J8uiFXpK3qSugAAPJKXLtnrF",
102-
"Amount": "1",
103-
"Destination": "radqi6ppXFxVhJdjzaATRBxdrPcVTf1Ung",
104-
"Sequence": 281,
105-
"SigningPubKey": "03D642E6457B8AB4D140E2C66EB4C484FAFB1BF267CB578EC4815FE6CD06379C51",
106-
"Fee": "12000",
107-
"LastLedgerSequence": 10074214,
108-
"TxnSignature": "304402201180636F2CE215CE97A29CD302618FAE60D63EBFC8903DE17A356E857A449C430220290F4A54F9DE4AC79034C8BEA5F1F8757F7505F1A6FF04D2E19B6D62E867256B"
109-
};
110-
});
111-
112-
it('should serialize and parse - full memo, all strings text/plain ', function() {
113-
input_json.Memos = [
114-
{
115-
"Memo": {
116-
"MemoType": "test",
117-
"MemoFormat": "text",
118-
"MemoData": "some data"
119-
}
120-
}
121-
];
122-
123-
var so = SerializedObject.from_json(input_json).to_json();
124-
input_json.Memos[0].Memo.parsed_memo_type = 'test';
125-
input_json.Memos[0].Memo.parsed_memo_format = 'text';
126-
input_json.Memos[0].Memo.parsed_memo_data = 'some data';
127-
128-
assert.deepEqual(so, input_json);
129-
});
130-
131-
it('should serialize and parse - full memo, all strings, invalid MemoFormat', function() {
132-
input_json.Memos = [
133-
{
134-
"Memo": {
135-
"MemoType": "test",
136-
"MemoFormat": "application/json",
137-
"MemoData": "some data"
138-
}
139-
}
140-
];
141-
142-
var so = SerializedObject.from_json(input_json).to_json();
143-
input_json.Memos[0].Memo.parsed_memo_type = 'test';
144-
input_json.Memos[0].Memo.parsed_memo_format = 'application/json';
145-
assert.deepEqual(so, input_json);
146-
assert.strictEqual(input_json.Memos[0].Memo.parsed_memo_data, void(0));
147-
});
148-
149-
it('should throw an error - full memo, json data, invalid MemoFormat', function() {
150-
input_json.Memos = [
151-
{
152-
"Memo": {
153-
"MemoType": "test",
154-
"MemoFormat": "text",
155-
"MemoData": {
156-
"string" : "some_string",
157-
"boolean" : true
158-
}
159-
}
160-
}
161-
];
162-
163-
assert.throws(function() {
164-
SerializedObject.from_json(input_json);
165-
}, /^Error: MemoData can only be a JSON object with a valid json MemoFormat \(Memo\) \(Memos\)/);
166-
});
167-
168-
it('should serialize and parse - full memo, json data, valid MemoFormat', function() {
169-
input_json.Memos = [
170-
{
171-
"Memo": {
172-
"MemoType": "test",
173-
"MemoFormat": "json",
174-
"ignored" : "ignored",
175-
"MemoData": {
176-
"string" : "some_string",
177-
"boolean" : true
178-
}
179-
}
180-
}
181-
];
182-
183-
var so = SerializedObject.from_json(input_json).to_json();
184-
delete input_json.Memos[0].Memo.ignored;
185-
input_json.Memos[0].Memo.parsed_memo_type = 'test';
186-
input_json.Memos[0].Memo.parsed_memo_format = 'json';
187-
input_json.Memos[0].Memo.parsed_memo_data = {
188-
"string" : "some_string",
189-
"boolean" : true
190-
};
191-
192-
assert.deepEqual(so, input_json);
193-
});
194-
195-
it('should serialize and parse - full memo, json data, valid MemoFormat', function() {
196-
input_json.Memos = [
197-
{
198-
"Memo": {
199-
"MemoType": "test",
200-
"MemoFormat": "json",
201-
"MemoData": {
202-
"string" : "some_string",
203-
"boolean" : true
204-
}
205-
}
206-
}
207-
];
208-
209-
var so = SerializedObject.from_json(input_json).to_json();
210-
input_json.Memos[0].Memo.parsed_memo_type = 'test';
211-
input_json.Memos[0].Memo.parsed_memo_format = 'json';
212-
input_json.Memos[0].Memo.parsed_memo_data = {
213-
"string" : "some_string",
214-
"boolean" : true
215-
};
216-
217-
assert.deepEqual(so, input_json);
218-
});
219-
220-
it('should serialize and parse - full memo, json data, valid MemoFormat', function() {
221-
input_json.Memos = [
222-
{
223-
"Memo": {
224-
"MemoType": "test",
225-
"MemoFormat": "json",
226-
"MemoData": 3
227-
}
228-
}
229-
];
230-
231-
var so = SerializedObject.from_json(input_json).to_json();
232-
input_json.Memos[0].Memo.parsed_memo_type = 'test';
233-
input_json.Memos[0].Memo.parsed_memo_format = 'json';
234-
input_json.Memos[0].Memo.parsed_memo_data = 3;
235-
assert.deepEqual(so, input_json);
236-
});
237-
238-
it('should serialize and parse - full memo, json data, valid MemoFormat', function() {
239-
input_json.Memos = [
240-
{
241-
"Memo": {
242-
"MemoType": "test",
243-
"MemoFormat": "json",
244-
"MemoData": 3
245-
}
246-
}
247-
];
248-
249-
var so = SerializedObject.from_json(input_json).to_json();
250-
input_json.Memos[0].Memo.parsed_memo_type = 'test';
251-
input_json.Memos[0].Memo.parsed_memo_format = 'json';
252-
input_json.Memos[0].Memo.parsed_memo_data = 3;
253-
assert.deepEqual(so, input_json);
254-
});
255-
256-
it('should throw an error - invalid Memo field', function() {
257-
input_json.Memos = [
258-
{
259-
"Memo": {
260-
"MemoType": "test",
261-
"MemoParty": "json",
262-
"MemoData": 3
263-
}
264-
}
265-
];
266-
267-
assert.throws(function() {
268-
SerializedObject.from_json(input_json);
269-
}, /^Error: JSON contains unknown field: "MemoParty" \(Memo\) \(Memos\)/);
270-
});
271-
272-
273-
});
274-
275-
});
276-
277-
90+
})
27891
});
27992

28093

‎test/transaction-test.js

+17-138
Original file line numberDiff line numberDiff line change
@@ -1052,84 +1052,26 @@ describe('Transaction', function() {
10521052
var transaction = new Transaction();
10531053
transaction.tx_json.TransactionType = 'Payment';
10541054

1055-
var memoType = 'message';
1056-
var memoFormat = 'application/json';
1057-
var memoData = {
1058-
string: 'value',
1059-
bool: true,
1060-
integer: 1
1061-
};
1062-
1063-
transaction.addMemo(memoType, memoFormat, memoData);
1064-
1065-
var expected = [
1066-
{
1067-
Memo:
1068-
{
1069-
MemoType: memoType,
1070-
MemoFormat: memoFormat,
1071-
MemoData: memoData
1072-
}
1073-
}
1074-
];
1075-
1076-
assert.deepEqual(transaction.tx_json.Memos, expected);
1077-
});
1078-
1079-
it('Add Memo - by object', function() {
1080-
var transaction = new Transaction();
1081-
transaction.tx_json.TransactionType = 'Payment';
1082-
1083-
var memo = {
1084-
memoType: 'type',
1085-
memoData: 'data'
1086-
};
1087-
1088-
transaction.addMemo(memo);
1089-
1090-
var expected = [
1091-
{
1092-
Memo: {
1093-
MemoType: memo.memoType,
1094-
MemoData: memo.memoData
1095-
}
1096-
}
1097-
];
1098-
1099-
assert.deepEqual(transaction.tx_json.Memos, expected);
1100-
});
1101-
1102-
it('Add Memos', function() {
1103-
var transaction = new Transaction();
1104-
transaction.tx_json.TransactionType = 'Payment';
1105-
1106-
transaction.addMemo('testkey', void(0), 'testvalue');
1107-
transaction.addMemo('testkey2', void(0), 'testvalue2');
1108-
transaction.addMemo('testkey3', 'text/html');
1109-
transaction.addMemo(void(0), void(0), 'testvalue4');
1110-
transaction.addMemo('testkey4', 'text/html', '<html>');
1055+
transaction.addMemo('testkey', 'testvalue');
1056+
transaction.addMemo('testkey2', 'testvalue2');
1057+
transaction.addMemo('testkey3');
1058+
transaction.addMemo(void(0), 'testvalue4');
11111059

11121060
var expected = [
11131061
{ Memo: {
1114-
MemoType: 'testkey',
1115-
MemoData: 'testvalue'
1062+
MemoType: new Buffer('testkey').toString('hex'),
1063+
MemoData: new Buffer('testvalue').toString('hex')
11161064
}},
11171065
{ Memo: {
1118-
MemoType: 'testkey2',
1119-
MemoData: 'testvalue2'
1066+
MemoType: new Buffer('testkey2').toString('hex'),
1067+
MemoData: new Buffer('testvalue2').toString('hex')
11201068
}},
11211069
{ Memo: {
1122-
MemoType: 'testkey3',
1123-
MemoFormat: 'text/html'
1070+
MemoType: new Buffer('testkey3').toString('hex')
11241071
}},
11251072
{ Memo: {
1126-
MemoData: 'testvalue4'
1127-
}},
1128-
{ Memo: {
1129-
MemoType: 'testkey4',
1130-
MemoFormat: 'text/html',
1131-
MemoData: '<html>'
1132-
}}
1073+
MemoData: new Buffer('testvalue4').toString('hex')
1074+
} }
11331075
];
11341076

11351077
assert.deepEqual(transaction.tx_json.Memos, expected);
@@ -1144,76 +1086,13 @@ describe('Transaction', function() {
11441086
}, /^Error: MemoType must be a string$/);
11451087
});
11461088

1147-
it('Add Memo - invalid ASCII MemoType', function() {
1148-
var transaction = new Transaction();
1149-
transaction.tx_json.TransactionType = 'Payment';
1150-
1151-
assert.throws(function() {
1152-
transaction.addMemo('한국어');
1153-
}, /^Error: MemoType must be valid ASCII$/);
1154-
});
1155-
1156-
it('Add Memo - invalid MemoFormat', function() {
1157-
var transaction = new Transaction();
1158-
transaction.tx_json.TransactionType = 'Payment';
1159-
1160-
assert.throws(function() {
1161-
transaction.addMemo(void(0), 1);
1162-
}, /^Error: MemoFormat must be a string$/);
1163-
});
1164-
1165-
it('Add Memo - invalid ASCII MemoFormat', function() {
1089+
it('Add Memo - invalid MemoData', function() {
11661090
var transaction = new Transaction();
11671091
transaction.tx_json.TransactionType = 'Payment';
11681092

11691093
assert.throws(function() {
1170-
transaction.addMemo(void(0), 'России');
1171-
}, /^Error: MemoFormat must be valid ASCII$/);
1172-
});
1173-
1174-
it('Add Memo - MemoData string', function() {
1175-
var transaction = new Transaction();
1176-
transaction.tx_json.TransactionType = 'Payment';
1177-
1178-
transaction.addMemo({memoData:'some_string'});
1179-
1180-
assert.deepEqual(transaction.tx_json.Memos, [
1181-
{
1182-
Memo: {
1183-
MemoData: 'some_string'
1184-
}
1185-
}
1186-
]);
1187-
});
1188-
1189-
it('Add Memo - MemoData complex object', function() {
1190-
var transaction = new Transaction();
1191-
transaction.tx_json.TransactionType = 'Payment';
1192-
1193-
var memo = {
1194-
memoData: {
1195-
string: 'string',
1196-
int: 1,
1197-
array: [
1198-
{
1199-
string: 'string'
1200-
}
1201-
],
1202-
object: {
1203-
string: 'string'
1204-
}
1205-
}
1206-
};
1207-
1208-
transaction.addMemo(memo);
1209-
1210-
assert.deepEqual(transaction.tx_json.Memos, [
1211-
{
1212-
Memo: {
1213-
MemoData: memo.memoData
1214-
}
1215-
}
1216-
]);
1094+
transaction.addMemo('key', 1);
1095+
}, /^Error: MemoData must be a string$/);
12171096
});
12181097

12191098
it('Construct AccountSet transaction', function() {
@@ -1390,7 +1269,7 @@ describe('Transaction', function() {
13901269
var bid = '1/USD/rsLEU1TPdCJPPysqhWYw9jD97xtG5WqSJm';
13911270
var ask = '1/EUR/rsLEU1TPdCJPPysqhWYw9jD97xtG5WqSJm';
13921271
assert.throws(function() {
1393-
new Transaction().offerCreate('xrsLEU1TPdCJPPysqhWYw9jD97xtG5WqSJm', bid, ask);
1272+
var transaction = new Transaction().offerCreate('xrsLEU1TPdCJPPysqhWYw9jD97xtG5WqSJm', bid, ask);
13941273
});
13951274
});
13961275

@@ -1423,13 +1302,13 @@ describe('Transaction', function() {
14231302

14241303
it('Construct SetRegularKey transaction - invalid account', function() {
14251304
assert.throws(function() {
1426-
new Transaction().setRegularKey('xrsLEU1TPdCJPPysqhWYw9jD97xtG5WqSJm', 'r36xtKNKR43SeXnGn7kN4r4JdQzcrkqpWe');
1305+
var transaction = new Transaction().setRegularKey('xrsLEU1TPdCJPPysqhWYw9jD97xtG5WqSJm', 'r36xtKNKR43SeXnGn7kN4r4JdQzcrkqpWe');
14271306
});
14281307
});
14291308

14301309
it('Construct SetRegularKey transaction - invalid regularKey', function() {
14311310
assert.throws(function() {
1432-
new Transaction().setRegularKey('rsLEU1TPdCJPPysqhWYw9jD97xtG5WqSJm', 'xr36xtKNKR43SeXnGn7kN4r4JdQzcrkqpWe');
1311+
var transaction = new Transaction().setRegularKey('rsLEU1TPdCJPPysqhWYw9jD97xtG5WqSJm', 'xr36xtKNKR43SeXnGn7kN4r4JdQzcrkqpWe');
14331312
});
14341313
});
14351314

0 commit comments

Comments
 (0)
Please sign in to comment.