Source

js-stellar-base/src/operations/revoke_sponsorship.js

  1. import xdr from '../xdr';
  2. import { StrKey } from '../strkey';
  3. import { Keypair } from '../keypair';
  4. import { Asset } from '../asset';
  5. import { LiquidityPoolId } from '../liquidity_pool_id';
  6. /**
  7. * Create a "revoke sponsorship" operation for an account.
  8. *
  9. * @function
  10. * @alias Operation.revokeAccountSponsorship
  11. * @param {object} opts Options object
  12. * @param {string} opts.account - The sponsored account ID.
  13. * @param {string} [opts.source] - The source account for the operation. Defaults to the transaction's source account.
  14. * @returns {xdr.Operation} xdr operation
  15. *
  16. * @example
  17. * const op = Operation.revokeAccountSponsorship({
  18. * account: 'GDGU5OAPHNPU5UCLE5RDJHG7PXZFQYWKCFOEXSXNMR6KRQRI5T6XXCD7
  19. * });
  20. *
  21. */
  22. export function revokeAccountSponsorship(opts = {}) {
  23. if (!StrKey.isValidEd25519PublicKey(opts.account)) {
  24. throw new Error('account is invalid');
  25. }
  26. const ledgerKey = xdr.LedgerKey.account(
  27. new xdr.LedgerKeyAccount({
  28. accountId: Keypair.fromPublicKey(opts.account).xdrAccountId()
  29. })
  30. );
  31. const op = xdr.RevokeSponsorshipOp.revokeSponsorshipLedgerEntry(ledgerKey);
  32. const opAttributes = {};
  33. opAttributes.body = xdr.OperationBody.revokeSponsorship(op);
  34. this.setSourceAccount(opAttributes, opts);
  35. return new xdr.Operation(opAttributes);
  36. }
  37. /**
  38. * Create a "revoke sponsorship" operation for a trustline.
  39. *
  40. * @function
  41. * @alias Operation.revokeTrustlineSponsorship
  42. * @param {object} opts Options object
  43. * @param {string} opts.account - The account ID which owns the trustline.
  44. * @param {Asset | LiquidityPoolId} opts.asset - The trustline asset.
  45. * @param {string} [opts.source] - The source account for the operation. Defaults to the transaction's source account.
  46. * @returns {xdr.Operation} xdr operation
  47. *
  48. * @example
  49. * const op = Operation.revokeTrustlineSponsorship({
  50. * account: 'GDGU5OAPHNPU5UCLE5RDJHG7PXZFQYWKCFOEXSXNMR6KRQRI5T6XXCD7
  51. * asset: new StellarBase.LiquidityPoolId(
  52. * 'USDUSD',
  53. * 'GDGU5OAPHNPU5UCLE5RDJHG7PXZFQYWKCFOEXSXNMR6KRQRI5T6XXCD7'
  54. * )
  55. * });
  56. *
  57. */
  58. export function revokeTrustlineSponsorship(opts = {}) {
  59. if (!StrKey.isValidEd25519PublicKey(opts.account)) {
  60. throw new Error('account is invalid');
  61. }
  62. let asset;
  63. if (opts.asset instanceof Asset) {
  64. asset = opts.asset.toTrustLineXDRObject();
  65. } else if (opts.asset instanceof LiquidityPoolId) {
  66. asset = opts.asset.toXDRObject();
  67. } else {
  68. throw new TypeError('asset must be an Asset or LiquidityPoolId');
  69. }
  70. const ledgerKey = xdr.LedgerKey.trustline(
  71. new xdr.LedgerKeyTrustLine({
  72. accountId: Keypair.fromPublicKey(opts.account).xdrAccountId(),
  73. asset
  74. })
  75. );
  76. const op = xdr.RevokeSponsorshipOp.revokeSponsorshipLedgerEntry(ledgerKey);
  77. const opAttributes = {};
  78. opAttributes.body = xdr.OperationBody.revokeSponsorship(op);
  79. this.setSourceAccount(opAttributes, opts);
  80. return new xdr.Operation(opAttributes);
  81. }
  82. /**
  83. * Create a "revoke sponsorship" operation for an offer.
  84. *
  85. * @function
  86. * @alias Operation.revokeOfferSponsorship
  87. * @param {object} opts Options object
  88. * @param {string} opts.seller - The account ID which created the offer.
  89. * @param {string} opts.offerId - The offer ID.
  90. * @param {string} [opts.source] - The source account for the operation. Defaults to the transaction's source account.
  91. * @returns {xdr.Operation} xdr operation
  92. *
  93. * @example
  94. * const op = Operation.revokeOfferSponsorship({
  95. * seller: 'GDGU5OAPHNPU5UCLE5RDJHG7PXZFQYWKCFOEXSXNMR6KRQRI5T6XXCD7
  96. * offerId: '1234'
  97. * });
  98. *
  99. */
  100. export function revokeOfferSponsorship(opts = {}) {
  101. if (!StrKey.isValidEd25519PublicKey(opts.seller)) {
  102. throw new Error('seller is invalid');
  103. }
  104. if (typeof opts.offerId !== 'string') {
  105. throw new Error('offerId is invalid');
  106. }
  107. const ledgerKey = xdr.LedgerKey.offer(
  108. new xdr.LedgerKeyOffer({
  109. sellerId: Keypair.fromPublicKey(opts.seller).xdrAccountId(),
  110. offerId: xdr.Int64.fromString(opts.offerId)
  111. })
  112. );
  113. const op = xdr.RevokeSponsorshipOp.revokeSponsorshipLedgerEntry(ledgerKey);
  114. const opAttributes = {};
  115. opAttributes.body = xdr.OperationBody.revokeSponsorship(op);
  116. this.setSourceAccount(opAttributes, opts);
  117. return new xdr.Operation(opAttributes);
  118. }
  119. /**
  120. * Create a "revoke sponsorship" operation for a data entry.
  121. *
  122. * @function
  123. * @alias Operation.revokeDataSponsorship
  124. * @param {object} opts Options object
  125. * @param {string} opts.account - The account ID which owns the data entry.
  126. * @param {string} opts.name - The name of the data entry
  127. * @param {string} [opts.source] - The source account for the operation. Defaults to the transaction's source account.
  128. * @returns {xdr.Operation} xdr operation
  129. *
  130. * @example
  131. * const op = Operation.revokeDataSponsorship({
  132. * account: 'GDGU5OAPHNPU5UCLE5RDJHG7PXZFQYWKCFOEXSXNMR6KRQRI5T6XXCD7
  133. * name: 'foo'
  134. * });
  135. *
  136. */
  137. export function revokeDataSponsorship(opts = {}) {
  138. if (!StrKey.isValidEd25519PublicKey(opts.account)) {
  139. throw new Error('account is invalid');
  140. }
  141. if (typeof opts.name !== 'string' || opts.name.length > 64) {
  142. throw new Error('name must be a string, up to 64 characters');
  143. }
  144. const ledgerKey = xdr.LedgerKey.data(
  145. new xdr.LedgerKeyData({
  146. accountId: Keypair.fromPublicKey(opts.account).xdrAccountId(),
  147. dataName: opts.name
  148. })
  149. );
  150. const op = xdr.RevokeSponsorshipOp.revokeSponsorshipLedgerEntry(ledgerKey);
  151. const opAttributes = {};
  152. opAttributes.body = xdr.OperationBody.revokeSponsorship(op);
  153. this.setSourceAccount(opAttributes, opts);
  154. return new xdr.Operation(opAttributes);
  155. }
  156. /**
  157. * Create a "revoke sponsorship" operation for a claimable balance.
  158. *
  159. * @function
  160. * @alias Operation.revokeClaimableBalanceSponsorship
  161. * @param {object} opts Options object
  162. * @param {string} opts.balanceId - The sponsored claimable balance ID.
  163. * @param {string} [opts.source] - The source account for the operation. Defaults to the transaction's source account.
  164. * @returns {xdr.Operation} xdr operation
  165. *
  166. * @example
  167. * const op = Operation.revokeClaimableBalanceSponsorship({
  168. * balanceId: '00000000da0d57da7d4850e7fc10d2a9d0ebc731f7afb40574c03395b17d49149b91f5be',
  169. * });
  170. *
  171. */
  172. export function revokeClaimableBalanceSponsorship(opts = {}) {
  173. if (typeof opts.balanceId !== 'string') {
  174. throw new Error('balanceId is invalid');
  175. }
  176. const ledgerKey = xdr.LedgerKey.claimableBalance(
  177. new xdr.LedgerKeyClaimableBalance({
  178. balanceId: xdr.ClaimableBalanceId.fromXDR(opts.balanceId, 'hex')
  179. })
  180. );
  181. const op = xdr.RevokeSponsorshipOp.revokeSponsorshipLedgerEntry(ledgerKey);
  182. const opAttributes = {};
  183. opAttributes.body = xdr.OperationBody.revokeSponsorship(op);
  184. this.setSourceAccount(opAttributes, opts);
  185. return new xdr.Operation(opAttributes);
  186. }
  187. /**
  188. * Creates a "revoke sponsorship" operation for a liquidity pool.
  189. *
  190. * @function
  191. * @alias Operation.revokeLiquidityPoolSponsorship
  192. * @param {object} opts – Options object.
  193. * @param {string} opts.liquidityPoolId - The sponsored liquidity pool ID in 'hex' string.
  194. * @param {string} [opts.source] - The source account for the operation. Defaults to the transaction's source account.
  195. * @returns {xdr.Operation} xdr Operation.
  196. *
  197. * @example
  198. * const op = Operation.revokeLiquidityPoolSponsorship({
  199. * liquidityPoolId: 'dd7b1ab831c273310ddbec6f97870aa83c2fbd78ce22aded37ecbf4f3380fac7',
  200. * });
  201. *
  202. */
  203. export function revokeLiquidityPoolSponsorship(opts = {}) {
  204. if (typeof opts.liquidityPoolId !== 'string') {
  205. throw new Error('liquidityPoolId is invalid');
  206. }
  207. const ledgerKey = xdr.LedgerKey.liquidityPool(
  208. new xdr.LedgerKeyLiquidityPool({
  209. liquidityPoolId: xdr.PoolId.fromXDR(opts.liquidityPoolId, 'hex')
  210. })
  211. );
  212. const op = xdr.RevokeSponsorshipOp.revokeSponsorshipLedgerEntry(ledgerKey);
  213. const opAttributes = {
  214. body: xdr.OperationBody.revokeSponsorship(op)
  215. };
  216. this.setSourceAccount(opAttributes, opts);
  217. return new xdr.Operation(opAttributes);
  218. }
  219. /**
  220. * Create a "revoke sponsorship" operation for a signer.
  221. *
  222. * @function
  223. * @alias Operation.revokeSignerSponsorship
  224. * @param {object} opts Options object
  225. * @param {string} opts.account - The account ID where the signer sponsorship is being removed from.
  226. * @param {object} opts.signer - The signer whose sponsorship is being removed.
  227. * @param {string} [opts.signer.ed25519PublicKey] - The ed25519 public key of the signer.
  228. * @param {Buffer|string} [opts.signer.sha256Hash] - sha256 hash (Buffer or hex string).
  229. * @param {Buffer|string} [opts.signer.preAuthTx] - Hash (Buffer or hex string) of transaction.
  230. * @param {string} [opts.source] - The source account for the operation. Defaults to the transaction's source account.
  231. * @returns {xdr.Operation} xdr operation
  232. *
  233. * @example
  234. * const op = Operation.revokeSignerSponsorship({
  235. * account: 'GDGU5OAPHNPU5UCLE5RDJHG7PXZFQYWKCFOEXSXNMR6KRQRI5T6XXCD7
  236. * signer: {
  237. * ed25519PublicKey: 'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ'
  238. * }
  239. * })
  240. *
  241. */
  242. export function revokeSignerSponsorship(opts = {}) {
  243. if (!StrKey.isValidEd25519PublicKey(opts.account)) {
  244. throw new Error('account is invalid');
  245. }
  246. let key;
  247. if (opts.signer.ed25519PublicKey) {
  248. if (!StrKey.isValidEd25519PublicKey(opts.signer.ed25519PublicKey)) {
  249. throw new Error('signer.ed25519PublicKey is invalid.');
  250. }
  251. const rawKey = StrKey.decodeEd25519PublicKey(opts.signer.ed25519PublicKey);
  252. key = new xdr.SignerKey.signerKeyTypeEd25519(rawKey);
  253. } else if (opts.signer.preAuthTx) {
  254. let buffer;
  255. if (typeof opts.signer.preAuthTx === 'string') {
  256. buffer = Buffer.from(opts.signer.preAuthTx, 'hex');
  257. } else {
  258. buffer = opts.signer.preAuthTx;
  259. }
  260. if (!(Buffer.isBuffer(buffer) && buffer.length === 32)) {
  261. throw new Error('signer.preAuthTx must be 32 bytes Buffer.');
  262. }
  263. key = new xdr.SignerKey.signerKeyTypePreAuthTx(buffer);
  264. } else if (opts.signer.sha256Hash) {
  265. let buffer;
  266. if (typeof opts.signer.sha256Hash === 'string') {
  267. buffer = Buffer.from(opts.signer.sha256Hash, 'hex');
  268. } else {
  269. buffer = opts.signer.sha256Hash;
  270. }
  271. if (!(Buffer.isBuffer(buffer) && buffer.length === 32)) {
  272. throw new Error('signer.sha256Hash must be 32 bytes Buffer.');
  273. }
  274. key = new xdr.SignerKey.signerKeyTypeHashX(buffer);
  275. } else {
  276. throw new Error('signer is invalid');
  277. }
  278. const signer = new xdr.RevokeSponsorshipOpSigner({
  279. accountId: Keypair.fromPublicKey(opts.account).xdrAccountId(),
  280. signerKey: key
  281. });
  282. const op = xdr.RevokeSponsorshipOp.revokeSponsorshipSigner(signer);
  283. const opAttributes = {};
  284. opAttributes.body = xdr.OperationBody.revokeSponsorship(op);
  285. this.setSourceAccount(opAttributes, opts);
  286. return new xdr.Operation(opAttributes);
  287. }