|
@@ -1,197 +0,0 @@
|
1
|
|
-import { Memo, Wallet } from '../util/types'
|
2
|
|
-import { MIN_XRP_FEE, MIN_XRP_TX_VALUE } from '../util/protocol.constants';
|
3
|
|
-
|
4
|
|
-import { Instructions, LedgerClosedEvent, RippleAPI } from 'ripple-lib'
|
5
|
|
-import { Payment } from 'ripple-lib/dist/npm/transaction/payment';
|
6
|
|
-
|
7
|
|
-import * as zlib from 'zlib'
|
8
|
|
-import * as util from 'util'
|
9
|
|
-
|
10
|
|
-const chunkString = (str: string, length: number) => str.match(new RegExp('.{1,' + length + '}', 'gs'));
|
11
|
|
-const PAYLOAD_SIZE = 925
|
12
|
|
-const debug = false
|
13
|
|
-
|
14
|
|
-const cloneApi = async (api: RippleAPI): Promise<RippleAPI> => {
|
15
|
|
- try {
|
16
|
|
- const subApi = new RippleAPI({ server: api.connection['_url'] })
|
17
|
|
- await subApi.connect()
|
18
|
|
- return subApi
|
19
|
|
- } catch (e) {
|
20
|
|
- if (debug) {
|
21
|
|
- console.log("CLONEAPI ERR", e)
|
22
|
|
- }
|
23
|
|
- return await cloneApi(api)
|
24
|
|
- }
|
25
|
|
-}
|
26
|
|
-
|
27
|
|
-export const getLatestSequence = async (api: RippleAPI, accountAddress: string): Promise<number> => {
|
28
|
|
- if (debug) console.log("Getting acc info for", accountAddress)
|
29
|
|
- const accountInfo = await api.getAccountInfo(accountAddress, {})
|
30
|
|
- return Number(accountInfo.sequence - 1)
|
31
|
|
-}
|
32
|
|
-
|
33
|
|
-const compressB64 = async (data: string) => (await util.promisify(zlib.deflate)(Buffer.from(data, 'utf-8'))).toString('base64')
|
34
|
|
-const decompressB64 = async (data: string) => (await util.promisify(zlib.inflate)(Buffer.from(data, 'base64'))).toString('utf-8')
|
35
|
|
-
|
36
|
|
-const sendReliably = (api: RippleAPI, signed: any, preparedPayment,): Promise<any> => new Promise((res, rej) => {
|
37
|
|
- const ledgerClosedCallback = async (event: LedgerClosedEvent) => {
|
38
|
|
- let status
|
39
|
|
- try {
|
40
|
|
- status = await api.getTransaction(signed.id, {
|
41
|
|
- minLedgerVersion: 25235454
|
42
|
|
- })
|
43
|
|
- } catch (e) {
|
44
|
|
-
|
45
|
|
- // Typical error when the tx hasn't been validated yet:
|
46
|
|
- if ((e as Error).name !== 'MissingLedgerHistoryError') {
|
47
|
|
- //console.log(e)
|
48
|
|
- }
|
49
|
|
-
|
50
|
|
- if (event.ledger_index > preparedPayment.instructions.maxLedgerVersion + 3) {
|
51
|
|
- // Assumptions:
|
52
|
|
- // - We are still connected to the same rippled server
|
53
|
|
- // - No ledger gaps occurred
|
54
|
|
- // - All ledgers between the time we submitted the tx and now have been checked for the tx
|
55
|
|
- status = {
|
56
|
|
- finalResult: 'Transaction was not, and never will be, included in a validated ledger'
|
57
|
|
- }
|
58
|
|
- return rej(status);
|
59
|
|
-
|
60
|
|
- } else {
|
61
|
|
- // Check again later:
|
62
|
|
- api.connection.once('ledgerClosed', ledgerClosedCallback)
|
63
|
|
- return
|
64
|
|
- }
|
65
|
|
- }
|
66
|
|
- return res(status)
|
67
|
|
- }
|
68
|
|
- api.connection.once('ledgerClosed', ledgerClosedCallback)
|
69
|
|
-})
|
70
|
|
-
|
71
|
|
-export const sendPayment = async (api: RippleAPI, data: Memo[], from: string, to: string, secret: string, sequence: number) => {
|
72
|
|
- if (debug) console.log("Sending payment with seq", sequence)
|
73
|
|
- const options: Instructions = {
|
74
|
|
- maxLedgerVersionOffset: 5,
|
75
|
|
- fee: MIN_XRP_FEE,
|
76
|
|
- sequence: sequence,
|
77
|
|
- };
|
78
|
|
- const payment: Payment = {
|
79
|
|
- source: {
|
80
|
|
- address: from,
|
81
|
|
- maxAmount: {
|
82
|
|
- value: MIN_XRP_TX_VALUE,
|
83
|
|
- currency: 'XRP'
|
84
|
|
- },
|
85
|
|
- },
|
86
|
|
- destination: {
|
87
|
|
- address: to,
|
88
|
|
- amount: {
|
89
|
|
- value: MIN_XRP_TX_VALUE,
|
90
|
|
- currency: 'XRP'
|
91
|
|
- },
|
92
|
|
- },
|
93
|
|
- memos: data,
|
94
|
|
- };
|
95
|
|
-
|
96
|
|
- const _api = await cloneApi(api)
|
97
|
|
- try {
|
98
|
|
- const prepared = await _api.preparePayment(from, payment, options)
|
99
|
|
- const signed = _api.sign(prepared.txJSON, secret);
|
100
|
|
- const txHash = await _api.submit(signed.signedTransaction)
|
101
|
|
- //if(debug) console.log("Transaction submitted", txHash)
|
102
|
|
- await sendReliably(_api, signed, prepared)
|
103
|
|
-
|
104
|
|
- return txHash
|
105
|
|
- } catch (error) {
|
106
|
|
- if (debug)
|
107
|
|
- console.log("SENDPAYMENT ERROR", error)
|
108
|
|
- throw error
|
109
|
|
- } finally {
|
110
|
|
- _api.disconnect()
|
111
|
|
- }
|
112
|
|
-}
|
113
|
|
-
|
114
|
|
-export const getTransactions = async (api: RippleAPI, address: string, minLedgerVersion: number = 25235454): Promise<any[]> => {
|
115
|
|
- const txs = await api.getTransactions(address, {
|
116
|
|
- minLedgerVersion: minLedgerVersion,
|
117
|
|
- earliestFirst: true,
|
118
|
|
- excludeFailures: true,
|
119
|
|
- })
|
120
|
|
- return txs
|
121
|
|
-}
|
122
|
|
-
|
123
|
|
-export const writeRaw = async (api: RippleAPI, data: Memo, from:
|
124
|
|
- string, to: string, secret: string, sequence?: number): Promise<string> => {
|
125
|
|
- //if (memoSize(data) > 1000) throw new Error("data length exceeds capacity")
|
126
|
|
- try {
|
127
|
|
- if (!sequence) {
|
128
|
|
- const accountInfo = await getLatestSequence(api, from)
|
129
|
|
- sequence = accountInfo + 1
|
130
|
|
- }
|
131
|
|
-
|
132
|
|
- const resp = await sendPayment(api, [data], from, to, secret, sequence)
|
133
|
|
- return resp['tx_json'].hash
|
134
|
|
- } catch (error) {
|
135
|
|
- if (debug) {
|
136
|
|
- console.log("WRITERAW ERR", error);
|
137
|
|
- }
|
138
|
|
- throw error
|
139
|
|
- }
|
140
|
|
-}
|
141
|
|
-
|
142
|
|
-
|
143
|
|
-export const readRaw = async (api: RippleAPI, hash: string): Promise<Memo> => {
|
144
|
|
- api = await cloneApi(api)
|
145
|
|
- let tx
|
146
|
|
- try {
|
147
|
|
- tx = await api.getTransaction(hash, {
|
148
|
|
- minLedgerVersion: 25235454
|
149
|
|
- })
|
150
|
|
- } catch (e) {
|
151
|
|
- // if(debug){
|
152
|
|
- console.log("READRAW ERR", e)
|
153
|
|
- api.isConnected
|
154
|
|
- // }
|
155
|
|
- throw e
|
156
|
|
- } finally {
|
157
|
|
- await api.disconnect()
|
158
|
|
- }
|
159
|
|
-
|
160
|
|
- if (!tx || !tx.specification || !tx.specification['memos'] || !tx.specification['memos'][0]) {
|
161
|
|
- console.log(tx)
|
162
|
|
- throw new Error('Invalid Transaction ' + hash)
|
163
|
|
- }
|
164
|
|
- return tx.specification['memos'][0]
|
165
|
|
-}
|
166
|
|
-
|
167
|
|
-export const subscribe = async (api: RippleAPI, address: string, callback: (tx) => any) => {
|
168
|
|
- api.connection.on('transaction', (tx) => callback(tx))
|
169
|
|
- await api.connection.request({
|
170
|
|
- command: 'subscribe',
|
171
|
|
- accounts: [address],
|
172
|
|
- })
|
173
|
|
-}
|
174
|
|
-
|
175
|
|
-export const treeWrite = async (api: RippleAPI, data: string, wallet: Wallet, to: string, format: 'L' | 'N' = 'L'): Promise<string> => {
|
176
|
|
- data = await compressB64(data)
|
177
|
|
- const chunks = chunkString(data, PAYLOAD_SIZE)
|
178
|
|
- const latestSequence = await getLatestSequence(api, wallet.address)
|
179
|
|
- const hashes = await Promise.all(Object.entries(chunks).map(([i, chunk]) => writeRaw(api, { data: chunk, format: format }, wallet.address, to, wallet.secret, latestSequence + Number(i) + 1)))
|
180
|
|
-
|
181
|
|
- if (hashes.length === 1) {
|
182
|
|
- return hashes[0]
|
183
|
|
- }
|
184
|
|
-
|
185
|
|
- return await treeWrite(api, JSON.stringify(hashes), wallet, to, 'N')
|
186
|
|
-}
|
187
|
|
-
|
188
|
|
-export const treeRead = async (api: RippleAPI, hashes: string[]): Promise<string> => {
|
189
|
|
- const memos = await Promise.all(hashes.map(hash => readRaw(api, hash)))
|
190
|
|
- const payload: string = await decompressB64(memos.map(memo => memo.data).join(''))
|
191
|
|
-
|
192
|
|
- if (memos.some(memo => memo.format === 'N')) {
|
193
|
|
- return await treeRead(api, JSON.parse(payload))
|
194
|
|
- }
|
195
|
|
-
|
196
|
|
- return payload
|
197
|
|
-}
|