You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Test.ts 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import { z } from "zod";
  2. import { DataParser, RJSVM_Builder, RJSVM_Implementations, RJSVM_Interface, RJSVM, RJSVM_Config } from "../src/main";
  3. //Test requirements
  4. import { assert, expect } from 'chai';
  5. import { describe, it } from "mocha";
  6. import { makeTestnetWallet } from "./tools";
  7. import { Wallet } from "xrpio";
  8. import { Datawriter } from "../src/RJSVM/datawriter/datawriter";
  9. import { InsufficientFeeError, RestrictedAccessError } from "../src/util/errors";
  10. var should = require('chai').should();
  11. var chai = require('chai');
  12. var chaiAsPromised = require('chai-as-promised');
  13. chai.use(chaiAsPromised);
  14. const xrpNode = "wss://s.altnet.rippletest.net:51233"
  15. let ownerWallet: Wallet //the RJSVM owner
  16. let userWallet: Wallet //a RJSVM user
  17. let listeningWallet: Wallet //wallet the RJSVM listens to
  18. let drainWallet: Wallet //random wallet to send stuff to, could be any user-controlled secondary wallet
  19. let rjsvm: RJSVM
  20. let user_datawriter: Datawriter
  21. let owner_datawriter: Datawriter
  22. const setup = async () => {
  23. // [ownerWallet, listeningWallet, userWallet, drainWallet] = await Promise.all([makeTestnetWallet(),makeTestnetWallet(),makeTestnetWallet(),makeTestnetWallet()])
  24. [ownerWallet, listeningWallet, userWallet, drainWallet] = [
  25. {
  26. secret: 'sEd7tPDHFk3w7ABSaCPYi12iwucMQLt',
  27. address: 'rDML7QhJeVxZTwsjPtm4iyLUJTCzzuppMo'
  28. },
  29. {
  30. secret: 'sEdSgbyouvLM26vq2F2XthfKGWndP72',
  31. address: 'rPvwbb4JHncG9rKVrYATJw6HRixR69wgjg'
  32. },
  33. {
  34. secret: 'sEd7c1Y8JwWyA2owbchMWnuzAhB3ypY',
  35. address: 'rwuuWdyxwf3o78Ce2DAZrYCQ12SSawTp6M'
  36. },
  37. {
  38. secret: 'sEdScantwGA5gYe4atKPbG5bsriURpf',
  39. address: 'rDiwSioTRyV29rNk2FskxqFtMaRsmWvS7C'
  40. }
  41. ]
  42. owner_datawriter = new Datawriter({
  43. receiveAddress: drainWallet.address,
  44. sendWallet: ownerWallet,
  45. xrpNode: xrpNode,
  46. contractAddress: listeningWallet.address
  47. })
  48. user_datawriter = new Datawriter({
  49. receiveAddress: drainWallet.address,
  50. sendWallet: userWallet,
  51. xrpNode: xrpNode,
  52. contractAddress: listeningWallet.address
  53. })
  54. //await owner_dw.callEndpoint('submit', { title: "1", body: "2", from: "3" }, 100)
  55. //await owner_dw.callEndpoint('submit', { title: "1", body: "2", from: "3" }, 100)
  56. //await user_dw.callEndpoint('restricted', { title: "user", body: "user", from: "user" })
  57. //await owner_dw.callEndpoint('restricted', { title: "owner", body: "owner", from: "owner" })
  58. // #########################
  59. // Define parameter types
  60. // #########################
  61. const shoutSchema = z.object({
  62. title: z.string(),
  63. body: z.string(),
  64. from: z.string(),
  65. id: z.optional(z.string())
  66. })
  67. type Shout = z.infer<typeof shoutSchema>
  68. type State = {
  69. shouts: Shout[]
  70. }
  71. // #########################
  72. // Define endpoints
  73. // #########################
  74. type RJSVM_Endpoints = {
  75. submit: (data: Shout) => void
  76. restricted: (data: Shout) => void
  77. }
  78. // #########################
  79. // Define init state
  80. // #########################
  81. abstract class RJSVM_Base
  82. extends RJSVM<State, RJSVM_Endpoints>
  83. implements RJSVM_Interface<RJSVM_Base> {
  84. owner = ownerWallet.address
  85. state: State = {
  86. shouts: []
  87. }
  88. }
  89. // #########################
  90. // Implement logic
  91. // #########################
  92. const RJSVM_Contract: RJSVM_Implementations<RJSVM_Base> = {
  93. submit: {
  94. implementation: function (env, shout) {
  95. this.state.shouts.unshift(shout)
  96. },
  97. visibility: 'public',
  98. fee: 10,
  99. parameterSchema: shoutSchema
  100. },
  101. restricted: {
  102. implementation: function (env, shout) {
  103. this.state.shouts.unshift(shout)
  104. },
  105. visibility: 'owner',
  106. parameterSchema: shoutSchema
  107. },
  108. }
  109. // #########################
  110. // Build and connect
  111. // #########################
  112. const Rjsvm = RJSVM_Builder.from(RJSVM_Base, RJSVM_Contract);
  113. const conf: RJSVM_Config = {
  114. listeningAddress: listeningWallet.address,
  115. rippleNode: xrpNode
  116. }
  117. rjsvm = new Rjsvm(conf)
  118. await rjsvm.connect()
  119. }
  120. describe('RJSVM basic functions', () => {
  121. before(async function () {
  122. this.timeout(100000)
  123. await setup()
  124. })
  125. after(async () => {
  126. await rjsvm.disconnect()
  127. })
  128. it('Env contains the payload carrying transaction', function (done) {
  129. this.timeout(45000)
  130. console.log("Making wallet...")
  131. makeTestnetWallet().then(async testWallet => {
  132. console.log(testWallet)
  133. //create a mock RJSVM
  134. abstract class RJSVM_Base
  135. extends RJSVM<undefined, { testEndpoint: () => void }>
  136. implements RJSVM_Interface<RJSVM_Base> {
  137. owner = ownerWallet.address
  138. state = undefined
  139. }
  140. //Implementation of the 'test' endpoint
  141. const RJSVM_Contract: RJSVM_Implementations<RJSVM_Base> = {
  142. testEndpoint: {
  143. implementation: function (env) {
  144. expect(env.Account).to.be.equal(userWallet.address)
  145. expect(env.Amount).to.be.equal('1')
  146. expect(env.Destination).to.be.equal(testWallet.address)
  147. expect(Number(env.Fee)).to.be.greaterThanOrEqual(10)
  148. expect(env.LastLedgerSequence).to.be.a('number')
  149. expect(env.Memos).to.be.an('Array')
  150. expect(env.Sequence).to.be.a('number')
  151. expect(env.SigningPubKey).to.be.a('string')
  152. expect(env.TransactionType).to.be.equal('Payment')
  153. expect(env.TxnSignature).to.be.a('string')
  154. expect(env.date).to.be.a('number')
  155. expect(env.hash).to.be.a('string')
  156. expect(env.inLedger).to.be.a('number')
  157. expect(env.ledger_index).to.be.a('number')
  158. runnable.disconnect()
  159. done()
  160. },
  161. visibility: 'public',
  162. parameterSchema: z.any()
  163. },
  164. }
  165. const Rjsvm = RJSVM_Builder.from(RJSVM_Base, RJSVM_Contract)
  166. const runnable = new Rjsvm({ listeningAddress: testWallet.address, rippleNode: xrpNode })
  167. await runnable.connect()
  168. const dw = new Datawriter({
  169. contractAddress: testWallet.address,
  170. sendWallet: userWallet,
  171. receiveAddress: drainWallet.address,
  172. xrpNode: xrpNode
  173. })
  174. dw.callEndpoint('testEndpoint', "")
  175. })
  176. })
  177. it('Called endpoint triggers in RJSVM', function (done) {
  178. this.timeout(45000)
  179. const data = { title: "1", body: "2", from: "3" };
  180. rjsvm.once('submit', (payload) => {
  181. expect(payload).to.be.an('object')
  182. expect(payload).to.deep.equal(data)
  183. done()
  184. })
  185. owner_datawriter.callEndpoint('submit', data, 10)
  186. })
  187. it('Calling an endpoint with insufficient fee fails', function (done) {
  188. this.timeout(45000)
  189. const data = { title: "f", body: "f", from: "f" };
  190. rjsvm.once('error', (err) => {
  191. expect(err).to.be.instanceOf(InsufficientFeeError)
  192. done()
  193. })
  194. owner_datawriter.callEndpoint('submit', data, 1)
  195. })
  196. it('Restricted endpoint can be called by owner', function (done) {
  197. this.timeout(45000)
  198. const data = { title: "11", body: "22", from: "33" };
  199. rjsvm.once('restricted', (payload) => {
  200. expect(payload).to.be.an('object')
  201. expect(payload).to.deep.equal(data)
  202. done()
  203. })
  204. owner_datawriter.callEndpoint('restricted', data)
  205. })
  206. it('Restricted endpoint cannot be called by non-owner', function (done) {
  207. this.timeout(45000)
  208. const data = { title: "e", body: "e", from: "e" };
  209. rjsvm.once('error', (err) => {
  210. expect(err).to.be.instanceOf(RestrictedAccessError)
  211. done()
  212. })
  213. user_datawriter.callEndpoint('restricted', data)
  214. })
  215. })