import { RippleAPI } from 'ripple-lib' import { htmlTxt, longText, makeTestnetWallet, TEST_CONFIG, TEST_DATA } from './CONSTANTS' import { getLatestSequence, sendPayment, treeRead, treeWrite } from '../src/xrpIO/ripple-binding' import * as chai from 'chai'; import { Wallet } from '../src/util/types'; const utf8 = require('utf8') const base64 = require('base-64') const expect = chai.expect let sendWallet: Wallet let receiveWallet: Wallet let api: RippleAPI describe('XRPIO', () => { before(async function () { this.timeout(15000) sendWallet = await makeTestnetWallet() receiveWallet = await makeTestnetWallet() await new Promise((res, rej) => setTimeout(res, 10000)) //it takes a moment for the wallets to become active }) beforeEach(async () => { try { api = new RippleAPI({ server: TEST_CONFIG.rippleNode }) return await api.connect() } catch (e) { console.log(e) throw e } }) afterEach(async function () { try { return await api.disconnect() } catch (e) { console.log(e) } }) let latestSeq describe('plumbing', () => { it('getLatestSequence', async () => { latestSeq = await getLatestSequence(api, sendWallet.address) expect(latestSeq).to.exist expect(latestSeq).to.be.a('number') expect(latestSeq).to.be.greaterThan(0) }) }) describe('payment', () => { it('sendPayment', async function () { this.timeout(10000) const result = await sendPayment(api, [{ data: "123" }], sendWallet.address, receiveWallet.address, sendWallet.secret, latestSeq + 1) expect(result).to.exist expect(result.resultCode).to.be.equal('tesSUCCESS') }) }) describe('I/O', () => { let treeRoot it(`can tree write (${TEST_DATA.length} bytes)`, async function () { this.timeout(10000) treeRoot = await treeWrite(api, TEST_DATA, sendWallet, receiveWallet.address) }) it('can tree read', async function () { this.timeout(15000) const data = await treeRead(api, [treeRoot]) expect(data).to.be.equal(TEST_DATA) }) it(`can tree write large (${longText.length} bytes)`, async function () { this.timeout(30000) treeRoot = await treeWrite(api, longText, sendWallet, receiveWallet.address) }) it('can tree read large', async function () { this.timeout(30000) const data = await treeRead(api, [treeRoot]) expect(data).to.be.equal(longText) }) it(`can tree write extra large (${htmlTxt.length} bytes)`, async function () { this.timeout(300000) treeRoot = await treeWrite(api, htmlTxt, sendWallet, receiveWallet.address) }) it('can tree read extra large', async function () { this.timeout(300000) const data = await treeRead(api, [treeRoot]) expect(data).to.be.equal(htmlTxt) }) it("can r/w binary", async function () { this.timeout(10000); const bytes = utf8.encode(TEST_DATA) const encoded = base64.encode(bytes) const txHash = await treeWrite(api, encoded, sendWallet, receiveWallet.address) const res = await treeRead(api, [txHash]) const decoded_bytes = base64.decode(res) const text = utf8.decode(decoded_bytes) expect(text).to.be.equal(TEST_DATA) }) }) })