Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

api.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  5. }) : (function(o, m, k, k2) {
  6. if (k2 === undefined) k2 = k;
  7. o[k2] = m[k];
  8. }));
  9. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  10. Object.defineProperty(o, "default", { enumerable: true, value: v });
  11. }) : function(o, v) {
  12. o["default"] = v;
  13. });
  14. var __importStar = (this && this.__importStar) || function (mod) {
  15. if (mod && mod.__esModule) return mod;
  16. var result = {};
  17. if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  18. __setModuleDefault(result, mod);
  19. return result;
  20. };
  21. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  22. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  23. return new (P || (P = Promise))(function (resolve, reject) {
  24. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  25. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  26. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  27. step((generator = generator.apply(thisArg, _arguments || [])).next());
  28. });
  29. };
  30. var __importDefault = (this && this.__importDefault) || function (mod) {
  31. return (mod && mod.__esModule) ? mod : { "default": mod };
  32. };
  33. Object.defineProperty(exports, "__esModule", { value: true });
  34. exports.RippleAPI = void 0;
  35. const events_1 = require("events");
  36. const common_1 = require("./common");
  37. const server_1 = require("./server/server");
  38. const transaction_1 = __importDefault(require("./ledger/transaction"));
  39. const transactions_1 = __importDefault(require("./ledger/transactions"));
  40. const trustlines_1 = __importDefault(require("./ledger/trustlines"));
  41. const balances_1 = __importDefault(require("./ledger/balances"));
  42. const balance_sheet_1 = __importDefault(require("./ledger/balance-sheet"));
  43. const pathfind_1 = __importDefault(require("./ledger/pathfind"));
  44. const orders_1 = __importDefault(require("./ledger/orders"));
  45. const orderbook_1 = require("./ledger/orderbook");
  46. const settings_1 = require("./ledger/settings");
  47. const accountinfo_1 = __importDefault(require("./ledger/accountinfo"));
  48. const accountobjects_1 = __importDefault(require("./ledger/accountobjects"));
  49. const payment_channel_1 = __importDefault(require("./ledger/payment-channel"));
  50. const payment_1 = __importDefault(require("./transaction/payment"));
  51. const trustline_1 = __importDefault(require("./transaction/trustline"));
  52. const order_1 = __importDefault(require("./transaction/order"));
  53. const ordercancellation_1 = __importDefault(require("./transaction/ordercancellation"));
  54. const escrow_creation_1 = __importDefault(require("./transaction/escrow-creation"));
  55. const escrow_execution_1 = __importDefault(require("./transaction/escrow-execution"));
  56. const escrow_cancellation_1 = __importDefault(require("./transaction/escrow-cancellation"));
  57. const payment_channel_create_1 = __importDefault(require("./transaction/payment-channel-create"));
  58. const payment_channel_fund_1 = __importDefault(require("./transaction/payment-channel-fund"));
  59. const payment_channel_claim_1 = __importDefault(require("./transaction/payment-channel-claim"));
  60. const check_create_1 = __importDefault(require("./transaction/check-create"));
  61. const check_cancel_1 = __importDefault(require("./transaction/check-cancel"));
  62. const check_cash_1 = __importDefault(require("./transaction/check-cash"));
  63. const settings_2 = __importDefault(require("./transaction/settings"));
  64. const ticket_1 = __importDefault(require("./transaction/ticket"));
  65. const sign_1 = __importDefault(require("./transaction/sign"));
  66. const combine_1 = __importDefault(require("./transaction/combine"));
  67. const submit_1 = __importDefault(require("./transaction/submit"));
  68. const utils_1 = require("./offline/utils");
  69. const derive_1 = require("./offline/derive");
  70. const ledgerhash_1 = __importDefault(require("./offline/ledgerhash"));
  71. const sign_payment_channel_claim_1 = __importDefault(require("./offline/sign-payment-channel-claim"));
  72. const verify_payment_channel_claim_1 = __importDefault(require("./offline/verify-payment-channel-claim"));
  73. const ledger_1 = __importDefault(require("./ledger/ledger"));
  74. const rangeset_1 = __importDefault(require("./common/rangeset"));
  75. const ledgerUtils = __importStar(require("./ledger/utils"));
  76. const transactionUtils = __importStar(require("./transaction/utils"));
  77. const schemaValidator = __importStar(require("./common/schema-validator"));
  78. const serverinfo_1 = require("./common/serverinfo");
  79. const utils_2 = require("./ledger/utils");
  80. const ripple_address_codec_1 = require("ripple-address-codec");
  81. const hashes_1 = require("./common/hashes");
  82. const wallet_generation_1 = __importDefault(require("./wallet/wallet-generation"));
  83. function getCollectKeyFromCommand(command) {
  84. switch (command) {
  85. case 'account_offers':
  86. case 'book_offers':
  87. return 'offers';
  88. case 'account_lines':
  89. return 'lines';
  90. default:
  91. return undefined;
  92. }
  93. }
  94. class RippleAPI extends events_1.EventEmitter {
  95. constructor(options = {}) {
  96. super();
  97. this.generateAddress = utils_1.generateAddress;
  98. this.generateXAddress = utils_1.generateXAddress;
  99. this.connect = server_1.connect;
  100. this.disconnect = server_1.disconnect;
  101. this.isConnected = server_1.isConnected;
  102. this.getServerInfo = serverinfo_1.getServerInfo;
  103. this.getFee = serverinfo_1.getFee;
  104. this.getLedgerVersion = server_1.getLedgerVersion;
  105. this.getTransaction = transaction_1.default;
  106. this.getTransactions = transactions_1.default;
  107. this.getTrustlines = trustlines_1.default;
  108. this.getBalances = balances_1.default;
  109. this.getBalanceSheet = balance_sheet_1.default;
  110. this.getPaths = pathfind_1.default;
  111. this.getOrderbook = orderbook_1.getOrderbook;
  112. this.getOrders = orders_1.default;
  113. this.getSettings = settings_1.getSettings;
  114. this.getAccountInfo = accountinfo_1.default;
  115. this.getAccountObjects = accountobjects_1.default;
  116. this.getPaymentChannel = payment_channel_1.default;
  117. this.getLedger = ledger_1.default;
  118. this.parseAccountFlags = settings_1.parseAccountFlags;
  119. this.preparePayment = payment_1.default;
  120. this.prepareTrustline = trustline_1.default;
  121. this.prepareOrder = order_1.default;
  122. this.prepareOrderCancellation = ordercancellation_1.default;
  123. this.prepareEscrowCreation = escrow_creation_1.default;
  124. this.prepareEscrowExecution = escrow_execution_1.default;
  125. this.prepareEscrowCancellation = escrow_cancellation_1.default;
  126. this.preparePaymentChannelCreate = payment_channel_create_1.default;
  127. this.preparePaymentChannelFund = payment_channel_fund_1.default;
  128. this.preparePaymentChannelClaim = payment_channel_claim_1.default;
  129. this.prepareCheckCreate = check_create_1.default;
  130. this.prepareCheckCash = check_cash_1.default;
  131. this.prepareCheckCancel = check_cancel_1.default;
  132. this.prepareTicketCreate = ticket_1.default;
  133. this.prepareSettings = settings_2.default;
  134. this.sign = sign_1.default;
  135. this.combine = combine_1.default;
  136. this.submit = submit_1.default;
  137. this.deriveKeypair = derive_1.deriveKeypair;
  138. this.deriveAddress = derive_1.deriveAddress;
  139. this.computeLedgerHash = ledgerhash_1.default;
  140. this.signPaymentChannelClaim = sign_payment_channel_claim_1.default;
  141. this.verifyPaymentChannelClaim = verify_payment_channel_claim_1.default;
  142. this.generateFaucetWallet = wallet_generation_1.default;
  143. this.errors = common_1.errors;
  144. this.xrpToDrops = common_1.xrpToDrops;
  145. this.dropsToXrp = common_1.dropsToXrp;
  146. this.rippleTimeToISO8601 = common_1.rippleTimeToISO8601;
  147. this.iso8601ToRippleTime = common_1.iso8601ToRippleTime;
  148. this.txFlags = common_1.txFlags;
  149. this.accountSetFlags = common_1.constants.AccountSetFlags;
  150. this.isValidAddress = schemaValidator.isValidAddress;
  151. this.isValidSecret = schemaValidator.isValidSecret;
  152. common_1.validate.apiOptions(options);
  153. this._feeCushion = options.feeCushion || 1.2;
  154. this._maxFeeXRP = options.maxFeeXRP || '2';
  155. const serverURL = options.server;
  156. if (serverURL != null) {
  157. this.connection = new common_1.Connection(serverURL, options);
  158. this.connection.on('ledgerClosed', (message) => {
  159. this.emit('ledger', server_1.formatLedgerClose(message));
  160. });
  161. this.connection.on('error', (errorCode, errorMessage, data) => {
  162. this.emit('error', errorCode, errorMessage, data);
  163. });
  164. this.connection.on('connected', () => {
  165. this.emit('connected');
  166. });
  167. this.connection.on('disconnected', (code) => {
  168. let finalCode = code;
  169. if (finalCode === 1005 || finalCode === 4000) {
  170. finalCode = 1000;
  171. }
  172. this.emit('disconnected', finalCode);
  173. });
  174. }
  175. else {
  176. this.connection = new common_1.Connection(null, options);
  177. }
  178. }
  179. request(command, params = {}) {
  180. return __awaiter(this, void 0, void 0, function* () {
  181. return this.connection.request(Object.assign(Object.assign({}, params), { command, account: params.account ? common_1.ensureClassicAddress(params.account) : undefined }));
  182. });
  183. }
  184. hasNextPage(currentResponse) {
  185. return !!currentResponse.marker;
  186. }
  187. requestNextPage(command, params = {}, currentResponse) {
  188. return __awaiter(this, void 0, void 0, function* () {
  189. if (!currentResponse.marker) {
  190. return Promise.reject(new common_1.errors.NotFoundError('response does not have a next page'));
  191. }
  192. const nextPageParams = Object.assign({}, params, {
  193. marker: currentResponse.marker
  194. });
  195. return this.request(command, nextPageParams);
  196. });
  197. }
  198. prepareTransaction(txJSON, instructions = {}) {
  199. return __awaiter(this, void 0, void 0, function* () {
  200. return transactionUtils.prepareTransaction(txJSON, this, instructions);
  201. });
  202. }
  203. convertStringToHex(string) {
  204. return transactionUtils.convertStringToHex(string);
  205. }
  206. _requestAll(command, params = {}, options = {}) {
  207. return __awaiter(this, void 0, void 0, function* () {
  208. const collectKey = options.collect || getCollectKeyFromCommand(command);
  209. if (!collectKey) {
  210. throw new common_1.errors.ValidationError(`no collect key for command ${command}`);
  211. }
  212. const countTo = params.limit != null ? params.limit : Infinity;
  213. let count = 0;
  214. let marker = params.marker;
  215. let lastBatchLength;
  216. const results = [];
  217. do {
  218. const countRemaining = utils_2.clamp(countTo - count, 10, 400);
  219. const repeatProps = Object.assign(Object.assign({}, params), { limit: countRemaining, marker });
  220. const singleResult = yield this.request(command, repeatProps);
  221. const collectedData = singleResult[collectKey];
  222. marker = singleResult['marker'];
  223. results.push(singleResult);
  224. const isExpectedFormat = Array.isArray(collectedData);
  225. if (isExpectedFormat) {
  226. count += collectedData.length;
  227. lastBatchLength = collectedData.length;
  228. }
  229. else {
  230. lastBatchLength = 0;
  231. }
  232. } while (!!marker && count < countTo && lastBatchLength !== 0);
  233. return results;
  234. });
  235. }
  236. }
  237. exports.RippleAPI = RippleAPI;
  238. RippleAPI._PRIVATE = {
  239. validate: common_1.validate,
  240. RangeSet: rangeset_1.default,
  241. ledgerUtils,
  242. schemaValidator
  243. };
  244. RippleAPI.renameCounterpartyToIssuer = utils_2.renameCounterpartyToIssuer;
  245. RippleAPI.formatBidsAndAsks = orderbook_1.formatBidsAndAsks;
  246. RippleAPI.deriveXAddress = derive_1.deriveXAddress;
  247. RippleAPI.deriveClassicAddress = derive_1.deriveAddress;
  248. RippleAPI.classicAddressToXAddress = ripple_address_codec_1.classicAddressToXAddress;
  249. RippleAPI.xAddressToClassicAddress = ripple_address_codec_1.xAddressToClassicAddress;
  250. RippleAPI.isValidXAddress = ripple_address_codec_1.isValidXAddress;
  251. RippleAPI.isValidClassicAddress = ripple_address_codec_1.isValidClassicAddress;
  252. RippleAPI.encodeSeed = ripple_address_codec_1.encodeSeed;
  253. RippleAPI.decodeSeed = ripple_address_codec_1.decodeSeed;
  254. RippleAPI.encodeAccountID = ripple_address_codec_1.encodeAccountID;
  255. RippleAPI.decodeAccountID = ripple_address_codec_1.decodeAccountID;
  256. RippleAPI.encodeNodePublic = ripple_address_codec_1.encodeNodePublic;
  257. RippleAPI.decodeNodePublic = ripple_address_codec_1.decodeNodePublic;
  258. RippleAPI.encodeAccountPublic = ripple_address_codec_1.encodeAccountPublic;
  259. RippleAPI.decodeAccountPublic = ripple_address_codec_1.decodeAccountPublic;
  260. RippleAPI.encodeXAddress = ripple_address_codec_1.encodeXAddress;
  261. RippleAPI.decodeXAddress = ripple_address_codec_1.decodeXAddress;
  262. RippleAPI.computeBinaryTransactionHash = hashes_1.computeBinaryTransactionHash;
  263. RippleAPI.computeTransactionHash = hashes_1.computeTransactionHash;
  264. RippleAPI.computeBinaryTransactionSigningHash = hashes_1.computeBinaryTransactionSigningHash;
  265. RippleAPI.computeAccountLedgerObjectID = hashes_1.computeAccountLedgerObjectID;
  266. RippleAPI.computeSignerListLedgerObjectID = hashes_1.computeSignerListLedgerObjectID;
  267. RippleAPI.computeOrderID = hashes_1.computeOrderID;
  268. RippleAPI.computeTrustlineHash = hashes_1.computeTrustlineHash;
  269. RippleAPI.computeTransactionTreeHash = hashes_1.computeTransactionTreeHash;
  270. RippleAPI.computeStateTreeHash = hashes_1.computeStateTreeHash;
  271. RippleAPI.computeLedgerHash = ledgerhash_1.default;
  272. RippleAPI.computeEscrowHash = hashes_1.computeEscrowHash;
  273. RippleAPI.computePaymentChannelHash = hashes_1.computePaymentChannelHash;
  274. RippleAPI.txFlags = common_1.txFlags;
  275. RippleAPI.accountSetFlags = common_1.constants.AccountSetFlags;
  276. //# sourceMappingURL=api.js.map