UNPKG

swtc-chains

Version:
1,314 lines (1,307 loc) 36.6 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); const ZERO = "0000000000000000000000000000000000000000000000000000000000000000"; const CHAINS = [ { code: "bitcoin", currency: "BTC", simple: true, ACCOUNT_ALPHABET: "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" }, { code: "ripple", currency: "XRP", simple: true, ACCOUNT_ALPHABET: "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz" }, { code: "stellar", currency: "XLM", simple: true, ACCOUNT_ALPHABET: "gsphnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCr65jkm8oFqi1tuvAxyz" }, { code: "call", currency: "CALL", ACCOUNT_ALPHABET: "cpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2brdeCg65jkm8oFqi1tuvAxyz" }, { code: "stream", currency: "STM", ACCOUNT_ALPHABET: "vpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1turAxyz" }, { code: "jingtum", currency: "SWT", issuer: "jGa9J9TkqtBcUoHe2zqhVFFbgUVED6o9or", CURRENCIES: { CNT: "CNY", JCC: "JJCC", SLASH: "JSLASH", MOAC: "JMOAC", CALL: "JCALL", EKT: "JEKT", ETH: "JETH" }, XLIB: { default_ws: "ws.swtclib.ca:5020", default_api: "api.swtclib.ca:5080", default_ws_failover: "ws-failover.swtclib.ca:5020", default_api_failover: "api-failover.swtclib.ca:5080" }, ACCOUNT_ALPHABET: "jpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65rkm8oFqi1tuvAxyz", SEED_PREFIX: 33, ACCOUNT_PREFIX: 0, ACCOUNT_ZERO: "jjjjjjjjjjjjjjjjjjjjjhoLvTp", ACCOUNT_ONE: "jjjjjjjjjjjjjjjjjjjjBZbvri", fee: 10000 }, { code: "bizain", currency: "BWT", issuer: "bf42S78serP2BeSx7HGtwQR2QASYaHVqyb", XLIB: { default_ws: "ws-bwt.swtclib.ca:5020", default_api: "api-bwt.swtclib.ca:5080", default_ws_failover: "ws-bwt-failover.swtclib.ca:5020", default_api_failover: "api-bwt-failover.swtclib.ca:5080" }, ACCOUNT_ALPHABET: "bpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2jcdeCg65rkm8oFqi1tuvAxyz", SEED_PREFIX: 33, ACCOUNT_PREFIX: 0, ACCOUNT_ZERO: "bbbbbbbbbbbbbbbbbbbbbhoLvTp", ACCOUNT_ONE: "bbbbbbbbbbbbbbbbbbbbBZjvri", fee: 10 }, { code: "bvcadt", currency: "BVC", ACCOUNT_ALPHABET: "bpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2rcdeCg65jkm8oFqi1tuvAxyz" } ]; const LEDGER_FLAGS = { account_root: { PasswordSpent: 0x00010000, RequireDestTag: 0x00020000, RequireAuth: 0x00040000, DisallowSWT: 0x00080000, DisableMaster: 0x00100000 }, offer: { Passive: 0x00010000, Sell: 0x00020000 }, state: { LowReserve: 0x00010000, HighReserve: 0x00020000, LowAuth: 0x00040000, HighAuth: 0x00080000, LowNoSkywell: 0x00100000, HighNoSkywell: 0x00200000 } }; const FLAGS = { OfferCreate: { Passive: 0x00010000, ImmediateOrCancel: 0x00020000, FillOrKill: 0x00040000, Sell: 0x00080000 } }; const REQUIRED = 0; const OPTIONAL = 1; const DEFAULT = 2; const BASE = [ ["TransactionType", REQUIRED], ["Flags", OPTIONAL], ["SourceTag", OPTIONAL], ["LastLedgerSequence", OPTIONAL], ["Account", REQUIRED], ["Sequence", OPTIONAL], ["Fee", REQUIRED], ["OperationLimit", OPTIONAL], ["SigningPubKey", OPTIONAL], ["TxnSignature", OPTIONAL] ]; const TRANSACTION_TYPES = { AccountSet: [ 3, ...BASE, ...[ ["EmailHash", OPTIONAL], ["WalletLocator", OPTIONAL], ["WalletSize", OPTIONAL], ["MessageKey", OPTIONAL], ["Domain", OPTIONAL], ["TransferRate", OPTIONAL] ] ], TrustSet: [ 20, ...BASE, ...[ ["LimitAmount", OPTIONAL], ["QualityIn", OPTIONAL], ["QualityOut", OPTIONAL] ] ], RelationSet: [ 21, ...BASE, ...[ ["Target", REQUIRED], ["RelationType", REQUIRED], ["LimitAmount", OPTIONAL] ] ], RelationDel: [ 22, ...BASE, ...[ ["Target", REQUIRED], ["RelationType", REQUIRED], ["LimitAmount", OPTIONAL] ] ], OfferCreate: [ 7, ...BASE, ...[ ["TakerPays", REQUIRED], ["TakerGets", REQUIRED], ["AppType", OPTIONAL], ["Expiration", OPTIONAL] ] ], OfferCancel: [8, ...BASE, ...[["OfferSequence", REQUIRED]]], SetRegularKey: [5, ...BASE, ...[["RegularKey", REQUIRED]]], Payment: [ 0, ...BASE, ...[ ["Destination", REQUIRED], ["Amount", REQUIRED], ["SendMax", OPTIONAL], ["Paths", DEFAULT], ["InvoiceID", OPTIONAL], ["DestinationTag", OPTIONAL] ] ], Contract: [ 9, ...BASE, ...[ ["Expiration", REQUIRED], ["BondAmount", REQUIRED], ["StampEscrow", REQUIRED], ["JingtumEscrow", REQUIRED], ["CreateCode", OPTIONAL], ["FundCode", OPTIONAL], ["RemoveCode", OPTIONAL], ["ExpireCode", OPTIONAL] ] ], RemoveContract: [10, ...BASE, ...[["Target", REQUIRED]]], EnableFeature: [100, ...BASE, ...[["Feature", REQUIRED]]], SetFee: [ 101, ...BASE, ...[ ["Features", REQUIRED], ["BaseFee", REQUIRED], ["ReferenceFeeUnits", REQUIRED], ["ReserveBase", REQUIRED], ["ReserveIncrement", REQUIRED] ] ], ConfigContract: [ 30, ...BASE, ...[ ["Method", REQUIRED], ["Payload", OPTIONAL], ["Destination", OPTIONAL], ["Amount", OPTIONAL], ["Contracttype", OPTIONAL], ["ContractMethod", OPTIONAL], ["Args", OPTIONAL] ] ], AlethContract: [ 31, ...BASE, ...[ ["Method", REQUIRED], ["Contracttype", OPTIONAL], ["ContractMethod", OPTIONAL], ["Amount", OPTIONAL], ["Payload", OPTIONAL], ["Args", OPTIONAL], ["Destination", OPTIONAL] ] ], Brokerage: [ 205, ...BASE, ...[ ["OfferFeeRateNum", REQUIRED], ["OfferFeeRateDen", REQUIRED], ["AppType", REQUIRED], ["Amount", REQUIRED] ] ], SignerListSet: [ 207, ...BASE, ...[ ["SignerQuorum", REQUIRED], ["SignerEntries", OPTIONAL] ] ] }; const SLE_BASE = [ ["LedgerIndex", OPTIONAL], ["LedgerEntryType", REQUIRED], ["Flags", REQUIRED] ]; const LEDGER_ENTRY_TYPES = { AccountRoot: [ 97, ...SLE_BASE, ...[ ["Sequence", REQUIRED], ["PreviousTxnLgrSeq", REQUIRED], ["TransferRate", OPTIONAL], ["WalletSize", OPTIONAL], ["OwnerCount", REQUIRED], ["EmailHash", OPTIONAL], ["PreviousTxnID", REQUIRED], ["AccountTxnID", OPTIONAL], ["WalletLocator", OPTIONAL], ["Balance", REQUIRED], ["MessageKey", OPTIONAL], ["Domain", OPTIONAL], ["Account", REQUIRED], ["RegularKey", OPTIONAL] ] ], Contract: [ 99, ...SLE_BASE, ...[ ["PreviousTxnLgrSeq", REQUIRED], ["Expiration", REQUIRED], ["BondAmount", REQUIRED], ["PreviousTxnID", REQUIRED], ["Balance", REQUIRED], ["FundCode", OPTIONAL], ["RemoveCode", OPTIONAL], ["ExpireCode", OPTIONAL], ["CreateCode", OPTIONAL], ["Account", REQUIRED], ["Owner", REQUIRED], ["Issuer", REQUIRED] ] ], DirectoryNode: [ 100, ...SLE_BASE, ...[ ["IndexNext", OPTIONAL], ["IndexPrevious", OPTIONAL], ["ExchangeRate", OPTIONAL], ["RootIndex", REQUIRED], ["Owner", OPTIONAL], ["TakerPaysCurrency", OPTIONAL], ["TakerPaysIssuer", OPTIONAL], ["TakerGetsCurrency", OPTIONAL], ["TakerGetsIssuer", OPTIONAL], ["Indexes", REQUIRED] ] ], EnabledFeatures: [102, ...SLE_BASE, ...[["Features", REQUIRED]]], FeeSettings: [ 115, ...SLE_BASE, ...[ ["ReferenceFeeUnits", REQUIRED], ["ReserveBase", REQUIRED], ["ReserveIncrement", REQUIRED], ["BaseFee", REQUIRED], ["LedgerIndex", OPTIONAL] ] ], GeneratorMap: [103, ...SLE_BASE, ...[["Generator", REQUIRED]]], LedgerHashes: [ 104, ...SLE_BASE, ...[ ["LedgerEntryType", REQUIRED], ["Flags", REQUIRED], ["FirstLedgerSequence", OPTIONAL], ["LastLedgerSequence", OPTIONAL], ["LedgerIndex", OPTIONAL], ["Hashes", REQUIRED] ] ], Nickname: [ 110, ...SLE_BASE, ...[ ["LedgerEntryType", REQUIRED], ["Flags", REQUIRED], ["LedgerIndex", OPTIONAL], ["MinimumOffer", OPTIONAL], ["Account", REQUIRED] ] ], Offer: [ 111, ...SLE_BASE, ...[ ["LedgerEntryType", REQUIRED], ["Flags", REQUIRED], ["Sequence", REQUIRED], ["PreviousTxnLgrSeq", REQUIRED], ["Expiration", OPTIONAL], ["BookNode", REQUIRED], ["OwnerNode", REQUIRED], ["PreviousTxnID", REQUIRED], ["LedgerIndex", OPTIONAL], ["BookDirectory", REQUIRED], ["TakerPays", REQUIRED], ["TakerGets", REQUIRED], ["Account", REQUIRED] ] ], SkywellState: [ 114, ...SLE_BASE, ...[ ["LedgerEntryType", REQUIRED], ["Flags", REQUIRED], ["PreviousTxnLgrSeq", REQUIRED], ["HighQualityIn", OPTIONAL], ["HighQualityOut", OPTIONAL], ["LowQualityIn", OPTIONAL], ["LowQualityOut", OPTIONAL], ["LowNode", OPTIONAL], ["HighNode", OPTIONAL], ["PreviousTxnID", REQUIRED], ["LedgerIndex", OPTIONAL], ["Balance", REQUIRED], ["LowLimit", REQUIRED], ["HighLimit", REQUIRED] ] ] }; const METADATA = [ ["TransactionIndex", REQUIRED], ["TransactionResult", REQUIRED], ["AffectedNodes", REQUIRED] ]; const TYPES_MAP = [ void 0, "Int16", "Int32", "Int64", "Hash128", "Hash256", "Amount", "VL", "Account", void 0, void 0, void 0, void 0, void 0, "Object", "Array", "Int8", "Hash160", "PathSet", "Vector256" ]; const FIELDS_MAP = { 1: { 1: "LedgerEntryType", 2: "TransactionType", 3: "SignerWeight" }, 2: { 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", 15: "Timestamp", 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", 35: "RelationType", 36: "Method", 37: "AppType", 38: "SignerQuorum", 39: "Contracttype" }, 3: { 1: "IndexNext", 2: "IndexPrevious", 3: "BookNode", 4: "OwnerNode", 5: "BaseFee", 6: "ExchangeRate", 7: "LowNode", 8: "HighNode", 9: "OfferFeeRateNum", 10: "OfferFeeRateDen" }, 4: { 1: "EmailHash" }, 5: { 1: "LedgerHash", 2: "ParentHash", 3: "TransactionHash", 4: "AccountHash", 5: "PreviousTxnID", 6: "LedgerIndex", 7: "WalletLocator", 8: "RootIndex", 9: "AccountTxnID", 16: "BookDirectory", 17: "InvoiceID", 18: "Nickname", 19: "Amendment", 20: "TicketID" }, 6: { 1: "Amount", 2: "Balance", 3: "LimitAmount", 4: "TakerPays", 5: "TakerGets", 6: "LowLimit", 7: "HighLimit", 8: "Fee", 9: "SendMax", 16: "MinimumOffer", 17: "JingtumEscrow", 18: "DeliveredAmount" }, 7: { 1: "PublicKey", 2: "MessageKey", 3: "SigningPubKey", 4: "TxnSignature", 5: "Generator", 6: "Signature", 7: "Domain", 8: "FundCode", 9: "RemoveCode", 10: "ExpireCode", 11: "CreateCode", 12: "MemoType", 13: "MemoData", 14: "MemoFormat", 15: "Payload", 17: "ContractMethod", 18: "Parameter", 20: "MethodSignature" }, 8: { 1: "Account", 2: "Owner", 3: "Destination", 4: "Issuer", 7: "Target", 8: "RegularKey", 9: "FeeAccountID", 13: "Platform" }, 14: { 1: void 0, 2: "TransactionMetaData", 3: "CreatedNode", 4: "DeletedNode", 5: "ModifiedNode", 6: "PreviousFields", 7: "FinalFields", 8: "NewFields", 9: "TemplateEntry", 10: "Memo", 11: "Arg", 12: "SignerEntry", 13: "Signer" }, 15: { 1: void 0, 2: "SigningAccounts", 3: "TxnSignatures", 4: "Signatures", 5: "Template", 6: "Necessary", 7: "Sufficient", 8: "AffectedNodes", 9: "Memos", 10: "Args", 11: "SignerEntries", 12: "Signers" }, 16: { 1: "CloseResolution", 2: "TemplateEntryType", 3: "TransactionResult", 4: "ContractParamsType" }, 17: { 1: "TakerPaysCurrency", 2: "TakerPaysIssuer", 3: "TakerGetsCurrency", 4: "TakerGetsIssuer" }, 18: { 1: "Paths" }, 19: { 1: "Indexes", 2: "Hashes", 3: "Amendments" } }; const INVERSE_FIELDS_MAP = { Flags: [2, 2], LedgerEntryType: [1, 1], TransactionType: [1, 2], SignerWeight: [1, 3], SourceTag: [2, 3], Sequence: [2, 4], PreviousTxnLgrSeq: [2, 5], LedgerSequence: [2, 6], CloseTime: [2, 7], ParentCloseTime: [2, 8], SigningTime: [2, 9], Expiration: [2, 10], TransferRate: [2, 11], WalletSize: [2, 12], OwnerCount: [2, 13], DestinationTag: [2, 14], Timestamp: [2, 15], HighQualityIn: [2, 16], HighQualityOut: [2, 17], LowQualityIn: [2, 18], LowQualityOut: [2, 19], QualityIn: [2, 20], QualityOut: [2, 21], StampEscrow: [2, 22], BondAmount: [2, 23], LoadFee: [2, 24], OfferSequence: [2, 25], FirstLedgerSequence: [2, 26], LastLedgerSequence: [2, 27], TransactionIndex: [2, 28], OperationLimit: [2, 29], ReferenceFeeUnits: [2, 30], ReserveBase: [2, 31], ReserveIncrement: [2, 32], SetFlag: [2, 33], ClearFlag: [2, 34], RelationType: [2, 35], Method: [2, 36], AppType: [2, 37], SignerQuorum: [2, 38], Contracttype: [2, 39], IndexNext: [3, 1], IndexPrevious: [3, 2], BookNode: [3, 3], OwnerNode: [3, 4], BaseFee: [3, 5], ExchangeRate: [3, 6], LowNode: [3, 7], HighNode: [3, 8], OfferFeeRateNum: [3, 9], OfferFeeRateDen: [3, 10], EmailHash: [4, 1], LedgerHash: [5, 1], ParentHash: [5, 2], TransactionHash: [5, 3], AccountHash: [5, 4], PreviousTxnID: [5, 5], LedgerIndex: [5, 6], WalletLocator: [5, 7], RootIndex: [5, 8], AccountTxnID: [5, 9], BookDirectory: [5, 16], InvoiceID: [5, 17], Nickname: [5, 18], Amendment: [5, 19], TicketID: [5, 20], Amount: [6, 1], Balance: [6, 2], LimitAmount: [6, 3], TakerPays: [6, 4], TakerGets: [6, 5], LowLimit: [6, 6], HighLimit: [6, 7], Fee: [6, 8], SendMax: [6, 9], MinimumOffer: [6, 16], JingtumEscrow: [6, 17], DeliveredAmount: [6, 18], PublicKey: [7, 1], MessageKey: [7, 2], SigningPubKey: [7, 3], TxnSignature: [7, 4], Generator: [7, 5], Signature: [7, 6], Domain: [7, 7], FundCode: [7, 8], RemoveCode: [7, 9], ExpireCode: [7, 10], CreateCode: [7, 11], MemoType: [7, 12], MemoData: [7, 13], MemoFormat: [7, 14], Payload: [7, 15], ContractMethod: [7, 17], Parameter: [7, 18], MethodSignature: [7, 20], Account: [8, 1], Owner: [8, 2], Destination: [8, 3], Issuer: [8, 4], Target: [8, 7], RegularKey: [8, 8], FeeAccountID: [8, 9], Platform: [8, 13], undefined: [15, 1], TransactionMetaData: [14, 2], CreatedNode: [14, 3], DeletedNode: [14, 4], ModifiedNode: [14, 5], PreviousFields: [14, 6], FinalFields: [14, 7], NewFields: [14, 8], TemplateEntry: [14, 9], Memo: [14, 10], Arg: [14, 11], SignerEntry: [14, 12], Signer: [14, 13], SigningAccounts: [15, 2], TxnSignatures: [15, 3], Signatures: [15, 4], Template: [15, 5], Necessary: [15, 6], Sufficient: [15, 7], AffectedNodes: [15, 8], Memos: [15, 9], Args: [15, 10], SignerEntries: [15, 11], Signers: [15, 12], CloseResolution: [16, 1], TemplateEntryType: [16, 2], TransactionResult: [16, 3], ContractParamsType: [16, 4], TakerPaysCurrency: [17, 1], TakerPaysIssuer: [17, 2], TakerGetsCurrency: [17, 3], TakerGetsIssuer: [17, 4], Paths: [18, 1], Indexes: [19, 1], Hashes: [19, 2], Amendments: [19, 3] }; const MIN_CURRENCY_LEN = 3; const MAX_CURRENCY_LEN = 6; const TUM_NAME_LEN = 40; const AMOUNT_CONSTS = { currency_one: 1, currency_xns: 0, xns_precision: 6, bi_5: 5, bi_7: 7, bi_10: 10, bi_1e14: 1e14, bi_1e16: 1e16, bi_1e17: 1e17, bi_1e32: 1e32, bi_man_max_value: 9999999999999999, bi_man_min_value: 1e15, bi_xns_max: 9e18, bi_xns_min: -9e18, bi_xns_unit: 1e6, cMinOffset: -96, cMaxOffset: 80, max_value: "9999999999999999e80", min_value: "-1000000000000000e-96" }; const allNumeric = (text) => { const numbers = /^[0-9]+$/; return Boolean(String(text).match(numbers)); }; const isFloat = (val) => { const floatRegex = /^-?\d+(?:[.,]\d*?)?$/; return Boolean(floatRegex.test(val) && !Number.isNaN(parseFloat(val))); }; const isLetterNumer = (str) => { const numbers = /^[0-9A-Z]+$/i; return Boolean(String(str).match(numbers)); }; const isCurrency = (code) => { return (typeof code === "string" && Boolean(code && code.length >= MIN_CURRENCY_LEN && code.length <= MAX_CURRENCY_LEN)); }; const isCustomTum = (code) => { return isLetterNumer(code) && String(code).length === TUM_NAME_LEN; }; const isTumCode = (code) => { return typeof code === "string" && (isCurrency(code) || isCustomTum(code)); }; const isRelation = (str) => { return typeof str === "string" && (str === "freeze" || str === "autorize"); }; const get_transaction_type = (structure) => { let output; switch (typeof structure) { case "number": switch (structure) { case 0: output = "Payment"; break; case 3: output = "AccountSet"; break; case 5: output = "SetRegularKey"; break; case 7: output = "OfferCreate"; break; case 8: output = "OfferCancel"; break; case 9: output = "Contract"; break; case 10: output = "RemoveContract"; break; case 20: output = "TrustSet"; break; case 21: output = "RelationSet"; break; case 22: output = "RelationDel"; break; case 30: output = "ConfigContract"; break; case 100: output = "EnableFeature"; break; case 101: output = "SetFee"; break; case 205: output = "Brokerage"; break; default: throw new Error("Invalid transaction type!"); } break; case "string": switch (structure) { case "Payment": output = 0; break; case "AccountSet": output = 3; break; case "SetRegularKey": output = 5; break; case "OfferCreate": output = 7; break; case "OfferCancel": output = 8; break; case "Contract": output = 9; break; case "RemoveContract": output = 10; break; case "TrustSet": output = 20; break; case "RelationSet": output = 21; break; case "RelationDel": output = 22; break; case "ConfigContract": output = 30; break; case "EnableFeature": output = 100; break; case "SetFee": output = 101; break; case "Brokerage": output = 205; break; default: throw new Error("Invalid transaction type!"); } break; default: throw new Error("Invalid input type for transaction type!"); } return output; }; const get_transaction_result = (structure) => { let output; switch (typeof structure) { case "number": switch (structure) { case 0: output = "tesSUCCESS"; break; case 100: output = "tecCLAIM"; break; case 101: output = "tecPATH_PARTIAL"; break; case 102: output = "tecUNFUNDED_ADD"; break; case 103: output = "tecUNFUNDED_OFFER"; break; case 104: output = "tecUNFUNDED_PAYMENT"; break; case 105: output = "tecFAILED_PROCESSING"; break; case 121: output = "tecDIR_FULL"; break; case 122: output = "tecINSUF_RESERVE_LINE"; break; case 141: output = "tecINSUFFICIENT_RESERVE"; break; default: throw new Error("Invalid transaction result!"); } break; case "string": switch (structure) { case "tesSUCCESS": output = 0; break; case "tecCLAIM": output = 100; break; case "tecPATH_PARTIAL": output = 101; break; case "tecUNFUNDED_ADD": output = 102; break; case "tecUNFUNDED_OFFER": output = 103; break; case "tecUNFUNDED_PAYMENT": output = 104; break; case "tecFAILED_PROCESSING": output = 105; break; case "tecDIR_FULL": output = 121; break; case "tecINSUF_RESERVE_LINE": output = 122; break; case "tecINSUFFICIENT_RESERVE": output = 141; break; default: throw new Error("Invalid transaction result!"); } break; default: throw new Error("Invalid input type for transaction result!"); } return output; }; const get_ledger_entry_type = (structure) => { let output; switch (typeof structure) { case "number": switch (structure) { case 97: output = "AccountRoot"; break; case 99: output = "Contract"; break; case 100: output = "DirectoryNode"; break; case 102: output = "EnabledFeatures"; break; case 115: output = "FeeSettings"; break; case 103: output = "GeneratorMap"; break; case 104: output = "LedgerHashes"; break; case 110: output = "Nickname"; break; case 111: output = "Offer"; break; case 114: output = "SkywellState"; break; default: throw new Error("Invalid input type for ransaction result!"); } break; case "string": switch (structure) { case "AccountRoot": output = 97; break; case "Contract": output = 99; break; case "DirectoryNode": output = 100; break; case "EnabledFeatures": output = 102; break; case "FeeSettings": output = 115; break; case "GeneratorMap": output = 103; break; case "LedgerHashes": output = 104; break; case "Nickname": output = 110; break; case "Offer": output = 111; break; case "SkywellState": output = 114; break; default: output = 0; } break; default: output = "UndefinedLedgerEntry"; } return output; }; const get_dec_from_hexchar = (in_char) => { if (in_char.length > 1) { return 0; } const asc_code = in_char.charCodeAt(0); if (asc_code > 48 && asc_code < 58) { return asc_code - 48; } else if (asc_code > 64 && asc_code < 91) { return asc_code - 55; } else if (asc_code > 96 && asc_code < 123) { return asc_code - 87; } return 0; }; const hex_str_to_byte_array = (in_str) => { let i; const out = []; const str = in_str.replace(/\s|0x/g, ""); for (i = 0; i < str.length; i += 2) { if (i + 1 === str.length) { out.push(get_dec_from_hexchar(str.charAt(i)) * 16); } else { out.push(get_dec_from_hexchar(str.charAt(i)) * 16 + get_dec_from_hexchar(str.charAt(i + 1))); } } return out; }; const get_char_from_num = (in_num) => { if (in_num >= 0 && in_num < 10) { return in_num + 48; } if (in_num >= 10 && in_num < 16) { return in_num + 55; } }; const readAndSum = (so, bytes) => { let sum = 0; if (bytes > 4) { throw new Error("This function only supports up to four bytes."); } for (let i = 0; i < bytes; i++) { const byte = so.read(1)[0]; sum += byte << (8 * (bytes - i - 1)); } return sum >>> 0; }; const isNumber = val => { return typeof val === "number" && isFinite(val); }; const convertIntegerToByteArray = (val, bytes) => { if (!isNumber(val)) { throw new Error("Value is not a number " + bytes); } if (val < 0 || val >= Math.pow(256, bytes)) { throw new Error("Value out of bounds"); } const newBytes = []; for (let i = 0; i < bytes; i++) { newBytes.unshift((val >>> (i * 8)) & 0xff); } return newBytes; }; const convertStringToHex = in_str => { const str = unescape(encodeURIComponent(in_str)); let out_str = ""; let i; for (i = 0; i < str.length; i++) { out_str += (" 00" + Number(str.charCodeAt(i)).toString(16)).substr(-2); } return out_str.toUpperCase(); }; const convertHexToString = hexString => { let out_str = ""; let i; for (i = 0; i < hexString.length; i += 2) { const tmp = "0x" + hexString.slice(i, i + 2); out_str += String.fromCharCode(parseInt(tmp, 16)); } return decodeURIComponent(escape(out_str)); }; const sort_fields = keys => { function sort_field_compare(a, b) { const a_field_coordinates = INVERSE_FIELDS_MAP[a]; const a_type_bits = a_field_coordinates[0]; const a_field_bits = a_field_coordinates[1]; const b_field_coordinates = INVERSE_FIELDS_MAP[b]; const b_type_bits = b_field_coordinates[0]; const b_field_bits = b_field_coordinates[1]; return a_type_bits !== b_type_bits ? a_type_bits - b_type_bits : a_field_bits - b_field_bits; } return keys.sort(sort_field_compare); }; const isString = (val) => { return typeof val === "string"; }; const convertByteArrayToHex = byte_array => { return byte_array .map(byteValue => { const hex = byteValue.toString(16).toUpperCase(); return hex.length > 1 ? hex : "0" + hex; }) .join(""); }; const isHexInt64String = val => { return isString(val) && /^[0-9A-F]{0,16}$/i.test(val); }; const isHexHASH256String = val => { return isString(val) && /^[0-9A-F]{0,64}$/i.test(val); }; function funcGetChain(chain_or_token) { const chains = CHAINS.filter(chain => chain.code.toLowerCase() === chain_or_token.toLowerCase() || chain.currency.toUpperCase() === chain_or_token.toUpperCase()); return chains.length === 1 ? chains[0] : undefined; } function funcSeqEqual(arr1, arr2) { if (arr1.length !== arr2.length) { return false; } for (let i = 0; i < arr1.length; i++) { if (arr1[i] !== arr2[i]) { return false; } } return true; } function isSequence(val) { return val.length !== undefined; } function funcConcatArgs(...args) { const ret = []; args.forEach(arg => { if (isSequence(arg)) { for (const e of arg) { ret.push(e); } } else { ret.push(arg); } }); return ret; } function funcHexToBytes(hex) { const bytes = []; for (let c = 0; c < hex.length; c += 2) { bytes.push(parseInt(hex.substr(c, 2), 16)); } return bytes; } function funcBytesToHex(bytes) { const hex = []; for (const byte of bytes) { const current = byte < 0 ? byte + 256 : byte; hex.push((current >>> 4).toString(16)); hex.push((current & 0xf).toString(16)); } return hex.join("").toUpperCase(); } function funcHexToString(h) { const a = []; let i = 0; if (h.length % 2) { a.push(String.fromCharCode(parseInt(h.substring(0, 1), 16))); i = 1; } for (; i < h.length; i += 2) { a.push(String.fromCharCode(parseInt(h.substring(i, i + 2), 16))); } return a.join(""); } function funcStringToHex(s) { let result = ""; for (const e of s) { const b = e.charCodeAt(0); result += b < 16 ? "0" + b.toString(16) : b.toString(16); } return result; } function funcString2Hex(s) { let result = funcStringToHex(s); if (result.length < 64) { result += ZERO.substr(result.length); } return result; } function funcNumber2Hex(n) { n = n.toString(16); return ZERO.substr(0, 64 - n.length) + n; } function funcHex2Number(h) { return parseInt(h, 16); } function funcIsEmpty(value) { const type = typeof value; if ((value !== null && type === "object") || type === "function") { const properties = Object.keys(value); if (properties.length === 0 || properties.size === 0) { return true; } } return !value; } exports.AMOUNT_CONSTS = AMOUNT_CONSTS; exports.CHAINS = CHAINS; exports.FIELDS_MAP = FIELDS_MAP; exports.FLAGS = FLAGS; exports.INVERSE_FIELDS_MAP = INVERSE_FIELDS_MAP; exports.LEDGER_ENTRY_TYPES = LEDGER_ENTRY_TYPES; exports.LEDGER_FLAGS = LEDGER_FLAGS; exports.MAX_CURRENCY_LEN = MAX_CURRENCY_LEN; exports.METADATA = METADATA; exports.MIN_CURRENCY_LEN = MIN_CURRENCY_LEN; exports.REQUIRED = REQUIRED; exports.TRANSACTION_TYPES = TRANSACTION_TYPES; exports.TUM_NAME_LEN = TUM_NAME_LEN; exports.TYPES_MAP = TYPES_MAP; exports.ZERO = ZERO; exports.allNumeric = allNumeric; exports.convertByteArrayToHex = convertByteArrayToHex; exports.convertHexToString = convertHexToString; exports.convertIntegerToByteArray = convertIntegerToByteArray; exports.convertStringToHex = convertStringToHex; exports.funcBytesToHex = funcBytesToHex; exports.funcConcatArgs = funcConcatArgs; exports.funcGetChain = funcGetChain; exports.funcHex2Number = funcHex2Number; exports.funcHexToBytes = funcHexToBytes; exports.funcHexToString = funcHexToString; exports.funcIsEmpty = funcIsEmpty; exports.funcNumber2Hex = funcNumber2Hex; exports.funcSeqEqual = funcSeqEqual; exports.funcString2Hex = funcString2Hex; exports.funcStringToHex = funcStringToHex; exports.get_char_from_num = get_char_from_num; exports.get_dec_from_hexchar = get_dec_from_hexchar; exports.get_ledger_entry_type = get_ledger_entry_type; exports.get_transaction_result = get_transaction_result; exports.get_transaction_type = get_transaction_type; exports.hex_str_to_byte_array = hex_str_to_byte_array; exports.isCurrency = isCurrency; exports.isCustomTum = isCustomTum; exports.isFloat = isFloat; exports.isHexHASH256String = isHexHASH256String; exports.isHexInt64String = isHexInt64String; exports.isLetterNumer = isLetterNumer; exports.isNumber = isNumber; exports.isRelation = isRelation; exports.isString = isString; exports.isTumCode = isTumCode; exports.readAndSum = readAndSum; exports.sort_fields = sort_fields;