claimant.js

  1. import xdr from './xdr';
  2. import { Keypair } from './keypair';
  3. import { StrKey } from './strkey';
  4. /**
  5. * Claimant class represents an xdr.Claimant
  6. *
  7. * The claim predicate is optional, it defaults to unconditional if none is specified.
  8. *
  9. * @constructor
  10. * @param {string} destination - The destination account ID.
  11. * @param {xdr.ClaimPredicate} [predicate] - The claim predicate.
  12. */
  13. export class Claimant {
  14. constructor(destination, predicate) {
  15. if (destination && !StrKey.isValidEd25519PublicKey(destination)) {
  16. throw new Error('Destination is invalid');
  17. }
  18. this._destination = destination;
  19. if (!predicate) {
  20. this._predicate = xdr.ClaimPredicate.claimPredicateUnconditional();
  21. } else if (predicate instanceof xdr.ClaimPredicate) {
  22. this._predicate = predicate;
  23. } else {
  24. throw new Error('Predicate should be an xdr.ClaimPredicate');
  25. }
  26. }
  27. /**
  28. * Returns an unconditional claim predicate
  29. * @Return {xdr.ClaimPredicate}
  30. */
  31. static predicateUnconditional() {
  32. return xdr.ClaimPredicate.claimPredicateUnconditional();
  33. }
  34. /**
  35. * Returns an `and` claim predicate
  36. * @param {xdr.ClaimPredicate} left an xdr.ClaimPredicate
  37. * @param {xdr.ClaimPredicate} right an xdr.ClaimPredicate
  38. * @Return {xdr.ClaimPredicate}
  39. */
  40. static predicateAnd(left, right) {
  41. if (!(left instanceof xdr.ClaimPredicate)) {
  42. throw new Error('left Predicate should be an xdr.ClaimPredicate');
  43. }
  44. if (!(right instanceof xdr.ClaimPredicate)) {
  45. throw new Error('right Predicate should be an xdr.ClaimPredicate');
  46. }
  47. return xdr.ClaimPredicate.claimPredicateAnd([left, right]);
  48. }
  49. /**
  50. * Returns an `or` claim predicate
  51. * @param {xdr.ClaimPredicate} left an xdr.ClaimPredicate
  52. * @param {xdr.ClaimPredicate} right an xdr.ClaimPredicate
  53. * @Return {xdr.ClaimPredicate}
  54. */
  55. static predicateOr(left, right) {
  56. if (!(left instanceof xdr.ClaimPredicate)) {
  57. throw new Error('left Predicate should be an xdr.ClaimPredicate');
  58. }
  59. if (!(right instanceof xdr.ClaimPredicate)) {
  60. throw new Error('right Predicate should be an xdr.ClaimPredicate');
  61. }
  62. return xdr.ClaimPredicate.claimPredicateOr([left, right]);
  63. }
  64. /**
  65. * Returns a `not` claim predicate
  66. * @param {xdr.ClaimPredicate} predicate an xdr.ClaimPredicate
  67. * @Return {xdr.ClaimPredicate}
  68. */
  69. static predicateNot(predicate) {
  70. if (!(predicate instanceof xdr.ClaimPredicate)) {
  71. throw new Error('right Predicate should be an xdr.ClaimPredicate');
  72. }
  73. return xdr.ClaimPredicate.claimPredicateNot(predicate);
  74. }
  75. /**
  76. * Returns a `BeforeAbsoluteTime` claim predicate
  77. *
  78. * This predicate will be fulfilled if the closing time of the ledger that
  79. * includes the CreateClaimableBalance operation is less than this (absolute)
  80. * Unix timestamp (expressed in seconds).
  81. *
  82. * @param {string} absBefore Unix epoch (in seconds) as a string
  83. * @Return {xdr.ClaimPredicate}
  84. */
  85. static predicateBeforeAbsoluteTime(absBefore) {
  86. return xdr.ClaimPredicate.claimPredicateBeforeAbsoluteTime(
  87. xdr.Int64.fromString(absBefore)
  88. );
  89. }
  90. /**
  91. * Returns a `BeforeRelativeTime` claim predicate
  92. *
  93. * This predicate will be fulfilled if the closing time of the ledger that
  94. * includes the CreateClaimableBalance operation plus this relative time delta
  95. * (in seconds) is less than the current time.
  96. *
  97. * @param {strings} seconds seconds since closeTime of the ledger in which the ClaimableBalanceEntry was created (as string)
  98. * @Return {xdr.ClaimPredicate}
  99. */
  100. static predicateBeforeRelativeTime(seconds) {
  101. return xdr.ClaimPredicate.claimPredicateBeforeRelativeTime(
  102. xdr.Int64.fromString(seconds)
  103. );
  104. }
  105. /**
  106. * Returns a claimant object from its XDR object representation.
  107. * @param {xdr.Claimant} claimantXdr - The claimant xdr object.
  108. * @returns {Claimant}
  109. */
  110. static fromXDR(claimantXdr) {
  111. let value;
  112. switch (claimantXdr.switch()) {
  113. case xdr.ClaimantType.claimantTypeV0():
  114. value = claimantXdr.v0();
  115. return new this(
  116. StrKey.encodeEd25519PublicKey(value.destination().ed25519()),
  117. value.predicate()
  118. );
  119. default:
  120. throw new Error(`Invalid claimant type: ${claimantXdr.switch().name}`);
  121. }
  122. }
  123. /**
  124. * Returns the xdr object for this claimant.
  125. * @returns {xdr.Claimant} XDR Claimant object
  126. */
  127. toXDRObject() {
  128. const claimant = new xdr.ClaimantV0({
  129. destination: Keypair.fromPublicKey(this._destination).xdrAccountId(),
  130. predicate: this._predicate
  131. });
  132. return xdr.Claimant.claimantTypeV0(claimant);
  133. }
  134. /**
  135. * @type {string}
  136. * @readonly
  137. */
  138. get destination() {
  139. return this._destination;
  140. }
  141. set destination(value) {
  142. throw new Error('Claimant is immutable');
  143. }
  144. /**
  145. * @type {xdr.ClaimPredicate}
  146. * @readonly
  147. */
  148. get predicate() {
  149. return this._predicate;
  150. }
  151. set predicate(value) {
  152. throw new Error('Claimant is immutable');
  153. }
  154. }