mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-16 18:35:50 +00:00
Move field type maps to binformat.js.
This commit is contained in:
@@ -1,3 +1,199 @@
|
||||
/**
|
||||
* Data type map.
|
||||
*
|
||||
* Mapping of type ids to data types. The type id is specified by the high
|
||||
*/
|
||||
var TYPES_MAP = exports.types = [
|
||||
void(0),
|
||||
|
||||
// Common
|
||||
'Int16', // 1
|
||||
'Int32', // 2
|
||||
'Int64', // 3
|
||||
'Hash128', // 4
|
||||
'Hash256', // 5
|
||||
'Amount', // 6
|
||||
'VL', // 7
|
||||
'Account', // 8
|
||||
|
||||
// 9-13 reserved
|
||||
void(0), // 9
|
||||
void(0), // 10
|
||||
void(0), // 11
|
||||
void(0), // 12
|
||||
void(0), // 13
|
||||
|
||||
'Object', // 14
|
||||
'Array', // 15
|
||||
|
||||
// Uncommon
|
||||
'Int8', // 16
|
||||
'Hash160', // 17
|
||||
'PathSet', // 18
|
||||
'Vector256' // 19
|
||||
];
|
||||
|
||||
/**
|
||||
* Field type map.
|
||||
*
|
||||
* Mapping of field type id to field type name.
|
||||
*/
|
||||
|
||||
var FIELDS_MAP = exports.fields = {
|
||||
// Common types
|
||||
1: { // Int16
|
||||
1: 'LedgerEntryType',
|
||||
2: 'TransactionType'
|
||||
},
|
||||
2: { // Int32
|
||||
2: 'Flags',
|
||||
3: 'SourceTag',
|
||||
4: 'Sequence',
|
||||
5: 'PreviousTxnLgrSeq',
|
||||
6: 'LedgerSequence',
|
||||
7: 'CloseTime',
|
||||
8: 'ParentCloseTime',
|
||||
9: 'SigningTime',
|
||||
10: 'Expiration',
|
||||
11: 'TransferRate',
|
||||
12: 'WalletSize',
|
||||
13: 'OwnerCount',
|
||||
14: 'DestinationTag',
|
||||
// Skip 15
|
||||
16: 'HighQualityIn',
|
||||
17: 'HighQualityOut',
|
||||
18: 'LowQualityIn',
|
||||
19: 'LowQualityOut',
|
||||
20: 'QualityIn',
|
||||
21: 'QualityOut',
|
||||
22: 'StampEscrow',
|
||||
23: 'BondAmount',
|
||||
24: 'LoadFee',
|
||||
25: 'OfferSequence',
|
||||
26: 'FirstLedgerSequence',
|
||||
27: 'LastLedgerSequence',
|
||||
28: 'TransactionIndex',
|
||||
29: 'OperationLimit',
|
||||
30: 'ReferenceFeeUnits',
|
||||
31: 'ReserveBase',
|
||||
32: 'ReserveIncrement',
|
||||
33: 'SetFlag',
|
||||
34: 'ClearFlag'
|
||||
},
|
||||
3: { // Int64
|
||||
1: 'IndexNext',
|
||||
2: 'IndexPrevious',
|
||||
3: 'BookNode',
|
||||
4: 'OwnerNode',
|
||||
5: 'BaseFee',
|
||||
6: 'ExchangeRate',
|
||||
7: 'LowNode',
|
||||
8: 'HighNode'
|
||||
},
|
||||
4: { // Hash128
|
||||
1: 'EmailHash'
|
||||
},
|
||||
5: { // Hash256
|
||||
1: 'LedgerHash',
|
||||
2: 'ParentHash',
|
||||
3: 'TransactionHash',
|
||||
4: 'AccountHash',
|
||||
5: 'PreviousTxnID',
|
||||
6: 'LedgerIndex',
|
||||
7: 'WalletLocator',
|
||||
8: 'RootIndex',
|
||||
16: 'BookDirectory',
|
||||
17: 'InvoiceID',
|
||||
18: 'Nickname',
|
||||
19: 'Feature'
|
||||
},
|
||||
6: { // Amount
|
||||
1: 'Amount',
|
||||
2: 'Balance',
|
||||
3: 'LimitAmount',
|
||||
4: 'TakerPays',
|
||||
5: 'TakerGets',
|
||||
6: 'LowLimit',
|
||||
7: 'HighLimit',
|
||||
8: 'Fee',
|
||||
9: 'SendMax',
|
||||
16: 'MinimumOffer',
|
||||
17: 'RippleEscrow'
|
||||
},
|
||||
7: { // VL
|
||||
1: 'PublicKey',
|
||||
2: 'MessageKey',
|
||||
3: 'SigningPubKey',
|
||||
4: 'TxnSignature',
|
||||
5: 'Generator',
|
||||
6: 'Signature',
|
||||
7: 'Domain',
|
||||
8: 'FundCode',
|
||||
9: 'RemoveCode',
|
||||
10: 'ExpireCode',
|
||||
11: 'CreateCode'
|
||||
},
|
||||
8: { // Account
|
||||
1: 'Account',
|
||||
2: 'Owner',
|
||||
3: 'Destination',
|
||||
4: 'Issuer',
|
||||
7: 'Target',
|
||||
8: 'RegularKey'
|
||||
},
|
||||
14: { // Object
|
||||
1: void(0), //end of Object
|
||||
2: 'TransactionMetaData',
|
||||
3: 'CreatedNode',
|
||||
4: 'DeletedNode',
|
||||
5: 'ModifiedNode',
|
||||
6: 'PreviousFields',
|
||||
7: 'FinalFields',
|
||||
8: 'NewFields',
|
||||
9: 'TemplateEntry'
|
||||
},
|
||||
15: { // Array
|
||||
1: void(0), //end of Array
|
||||
2: 'SigningAccounts',
|
||||
3: 'TxnSignatures',
|
||||
4: 'Signatures',
|
||||
5: 'Template',
|
||||
6: 'Necessary',
|
||||
7: 'Sufficient',
|
||||
8: 'AffectedNodes'
|
||||
},
|
||||
|
||||
// Uncommon types
|
||||
16: { // Int8
|
||||
1: 'CloseResolution',
|
||||
2: 'TemplateEntryType',
|
||||
3: 'TransactionResult'
|
||||
},
|
||||
17: { // Hash160
|
||||
1: 'TakerPaysCurrency',
|
||||
2: 'TakerPaysIssuer',
|
||||
3: 'TakerGetsCurrency',
|
||||
4: 'TakerGetsIssuer'
|
||||
},
|
||||
18: { // PathSet
|
||||
1: 'Paths'
|
||||
},
|
||||
19: { // Vector256
|
||||
1: 'Indexes',
|
||||
2: 'Hashes',
|
||||
3: 'Features'
|
||||
}
|
||||
};
|
||||
|
||||
var INVERSE_FIELDS_MAP = exports.fieldsInverseMap = { };
|
||||
|
||||
Object.keys(FIELDS_MAP).forEach(function(k1) {
|
||||
Object.keys(FIELDS_MAP[k1]).forEach(function(k2) {
|
||||
INVERSE_FIELDS_MAP[FIELDS_MAP[k1][k2]] = [ Number(k1), Number(k2) ];
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
var REQUIRED = exports.REQUIRED = 0,
|
||||
OPTIONAL = exports.OPTIONAL = 1,
|
||||
DEFAULT = exports.DEFAULT = 2;
|
||||
|
||||
@@ -402,7 +402,7 @@ var STAmount = exports.Amount = new SerializedType({
|
||||
|
||||
STAmount.id = 6;
|
||||
|
||||
var STVL = exports.VariableLength = new SerializedType({
|
||||
var STVL = exports.VariableLength = exports.VL = new SerializedType({
|
||||
serialize: function (so, val) {
|
||||
if (typeof val === 'string') {
|
||||
serialize_hex(so, val);
|
||||
@@ -586,7 +586,7 @@ function serialize(so, field_name, value) {
|
||||
//so: a byte-stream to serialize into.
|
||||
//field_name: a string for the field name ('LedgerEntryType' etc.)
|
||||
//value: the value of that field.
|
||||
var field_coordinates = INVERSE_FIELDS_MAP[field_name];
|
||||
var field_coordinates = binformat.fieldsInverseMap[field_name];
|
||||
var type_bits = field_coordinates[0];
|
||||
var field_bits = field_coordinates[1];
|
||||
var tag_byte = (type_bits < 16 ? type_bits << 4 : 0) | (field_bits < 16 ? field_bits : 0);
|
||||
@@ -609,7 +609,8 @@ function serialize(so, field_name, value) {
|
||||
STInt8.serialize(so, field_bits);
|
||||
}
|
||||
|
||||
var serialized_object_type = TYPES_MAP[type_bits];
|
||||
// Get the serializer class (ST...) for a field based on the type bits.
|
||||
var serialized_object_type = exports[binformat.types[type_bits]];
|
||||
//do something with val[keys] and val[keys[i]];
|
||||
serialized_object_type.serialize(so, value);
|
||||
}
|
||||
@@ -625,14 +626,15 @@ function parse(so) {
|
||||
type_bits = so.read(1)[0];
|
||||
}
|
||||
|
||||
var type = TYPES_MAP[type_bits];
|
||||
// Get the parser class (ST...) for a field based on the type bits.
|
||||
var type = exports[binformat.types[type_bits]];
|
||||
|
||||
assert(type, 'Unknown type - header byte is 0x' + tag_byte.toString(16));
|
||||
|
||||
var field_bits = tag_byte & 0x0f;
|
||||
var field_name = (field_bits === 0)
|
||||
? field_name = FIELDS_MAP[type_bits][so.read(1)[0]]
|
||||
: field_name = FIELDS_MAP[type_bits][field_bits];
|
||||
? field_name = binformat.fields[type_bits][so.read(1)[0]]
|
||||
: field_name = binformat.fields[type_bits][field_bits];
|
||||
|
||||
assert(field_name, 'Unknown field - header byte is 0x' + tag_byte.toString(16));
|
||||
|
||||
@@ -641,10 +643,10 @@ function parse(so) {
|
||||
|
||||
function sort_fields(keys) {
|
||||
function sort_field_compare(a, b) {
|
||||
var a_field_coordinates = INVERSE_FIELDS_MAP[a];
|
||||
var a_field_coordinates = binformat.fieldsInverseMap[a];
|
||||
var a_type_bits = a_field_coordinates[0];
|
||||
var a_field_bits = a_field_coordinates[1];
|
||||
var b_field_coordinates = INVERSE_FIELDS_MAP[b];
|
||||
var b_field_coordinates = binformat.fieldsInverseMap[b];
|
||||
var b_type_bits = b_field_coordinates[0];
|
||||
var b_field_bits = b_field_coordinates[1];
|
||||
|
||||
@@ -666,7 +668,7 @@ var STObject = exports.Object = new SerializedType({
|
||||
});
|
||||
|
||||
keys.forEach(function (key) {
|
||||
if ("undefined" === typeof INVERSE_FIELDS_MAP[key]) {
|
||||
if ("undefined" === typeof binformat.fieldsInverseMap[key]) {
|
||||
throw new Error("JSON contains unknown field: '" + key + "'");
|
||||
}
|
||||
});
|
||||
@@ -726,107 +728,3 @@ var STArray = exports.Array = new SerializedType({
|
||||
});
|
||||
|
||||
STArray.id = 15;
|
||||
|
||||
var TYPES_MAP = [
|
||||
void(0),
|
||||
|
||||
//Common:
|
||||
STInt16, //1
|
||||
STInt32, //2
|
||||
STInt64, //3
|
||||
STHash128, //4
|
||||
STHash256, //5
|
||||
STAmount, //6
|
||||
STVL, //7
|
||||
STAccount, //8
|
||||
|
||||
// 9-13 reserved
|
||||
void(0), //9
|
||||
void(0), //10
|
||||
void(0), //11
|
||||
void(0), //12
|
||||
void(0), //13
|
||||
|
||||
STObject, //14
|
||||
STArray, //15
|
||||
|
||||
//Uncommon:
|
||||
STInt8, //16
|
||||
STHash160, //17
|
||||
STPathSet, //18
|
||||
STVector256 //19
|
||||
];
|
||||
|
||||
var FIELDS_MAP = {
|
||||
//Common types
|
||||
1: { //Int16
|
||||
1: 'LedgerEntryType',
|
||||
2: 'TransactionType'
|
||||
},
|
||||
2: { //Int32
|
||||
2:'Flags', 3:'SourceTag',4:'Sequence',5:'PreviousTxnLgrSeq',6:'LedgerSequence',
|
||||
7:'CloseTime', 8:'ParentCloseTime',9:'SigningTime',10:'Expiration',11:'TransferRate',
|
||||
12:'WalletSize', 13:'OwnerCount',14:'DestinationTag',
|
||||
//Skip 15
|
||||
16:'HighQualityIn', 17:'HighQualityOut',18:'LowQualityIn',19:'LowQualityOut',
|
||||
20:'QualityIn', 21:'QualityOut',22:'StampEscrow',23:'BondAmount',24:'LoadFee',
|
||||
25:'OfferSequence', 26:'FirstLedgerSequence',27:'LastLedgerSequence',28:'TransactionIndex',
|
||||
29:'OperationLimit', 30:'ReferenceFeeUnits',31:'ReserveBase',32:'ReserveIncrement',
|
||||
33:'SetFlag', 34:'ClearFlag',
|
||||
},
|
||||
3: { // Int64
|
||||
1:'IndexNext', 2:'IndexPrevious',3:'BookNode',4:'OwnerNode',
|
||||
5:'BaseFee', 6:'ExchangeRate',7:'LowNode',8:'HighNode'
|
||||
},
|
||||
4: { //Hash128
|
||||
1:'EmailHash'
|
||||
},
|
||||
5: { //Hash256
|
||||
1:'LedgerHash', 2:'ParentHash',3:'TransactionHash',4:'AccountHash',5:'PreviousTxnID',
|
||||
6:'LedgerIndex', 7:'WalletLocator',8:'RootIndex',16:'BookDirectory',17:'InvoiceID',
|
||||
18:'Nickname', 19:'Feature'
|
||||
},
|
||||
6: { //Amount
|
||||
1:'Amount', 2:'Balance',3:'LimitAmount',4:'TakerPays',5:'TakerGets',6:'LowLimit',
|
||||
7:'HighLimit', 8:'Fee',9:'SendMax',16:'MinimumOffer',17:'RippleEscrow'
|
||||
},
|
||||
7: { //VL
|
||||
1:'PublicKey', 2:'MessageKey',3:'SigningPubKey',4:'TxnSignature',5:'Generator',
|
||||
6:'Signature', 7:'Domain',8:'FundCode',9:'RemoveCode',10:'ExpireCode',11:'CreateCode'
|
||||
},
|
||||
8: { //Account
|
||||
1:'Account', 2:'Owner',3:'Destination',4:'Issuer',7:'Target',8:'RegularKey'
|
||||
},
|
||||
14: { //Object
|
||||
1:void(0), //end of Object
|
||||
2:'TransactionMetaData', 3:'CreatedNode',4:'DeletedNode',5:'ModifiedNode',
|
||||
6:'PreviousFields', 7:'FinalFields',8:'NewFields',9:'TemplateEntry',
|
||||
},
|
||||
15: { //Array
|
||||
1:void(0), //end of Array
|
||||
2:'SigningAccounts', 3:'TxnSignatures',4:'Signatures',5:'Template',
|
||||
6:'Necessary', 7:'Sufficient',8:'AffectedNodes',
|
||||
},
|
||||
|
||||
//Uncommon types
|
||||
16: { //Int8
|
||||
1:'CloseResolution', 2:'TemplateEntryType',3:'TransactionResult'
|
||||
},
|
||||
17: { //Hash160
|
||||
1:'TakerPaysCurrency', 2:'TakerPaysIssuer',3:'TakerGetsCurrency',4:'TakerGetsIssuer'
|
||||
},
|
||||
18: { //PathSet
|
||||
1:'Paths'
|
||||
},
|
||||
19: { //Vector256
|
||||
1:'Indexes', 2:'Hashes', 3:'Features'
|
||||
}
|
||||
};
|
||||
|
||||
var INVERSE_FIELDS_MAP = { };
|
||||
|
||||
Object.keys(FIELDS_MAP).forEach(function(k1) {
|
||||
Object.keys(FIELDS_MAP[k1]).forEach(function(k2) {
|
||||
INVERSE_FIELDS_MAP[FIELDS_MAP[k1][k2]] = [ Number(k1), Number(k2) ];
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user