Source

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

  1. import xdr from '../xdr';
  2. import { Keypair } from '../keypair';
  3. import { StrKey } from '../strkey';
  4. /**
  5. * @deprecated since v5.0
  6. *
  7. * Returns an XDR AllowTrustOp. An "allow trust" operation authorizes another
  8. * account to hold your account's credit for a given asset.
  9. *
  10. * @function
  11. * @alias Operation.allowTrust
  12. *
  13. * @param {object} opts Options object
  14. * @param {string} opts.trustor - The trusting account (the one being authorized)
  15. * @param {string} opts.assetCode - The asset code being authorized.
  16. * @param {(0|1|2)} opts.authorize - `1` to authorize, `2` to authorize to maintain liabilities, and `0` to deauthorize.
  17. * @param {string} [opts.source] - The source account (defaults to transaction source).
  18. *
  19. * @returns {xdr.AllowTrustOp} Allow Trust operation
  20. */
  21. export function allowTrust(opts) {
  22. if (!StrKey.isValidEd25519PublicKey(opts.trustor)) {
  23. throw new Error('trustor is invalid');
  24. }
  25. const attributes = {};
  26. attributes.trustor = Keypair.fromPublicKey(opts.trustor).xdrAccountId();
  27. if (opts.assetCode.length <= 4) {
  28. const code = opts.assetCode.padEnd(4, '\0');
  29. attributes.asset = xdr.AssetCode.assetTypeCreditAlphanum4(code);
  30. } else if (opts.assetCode.length <= 12) {
  31. const code = opts.assetCode.padEnd(12, '\0');
  32. attributes.asset = xdr.AssetCode.assetTypeCreditAlphanum12(code);
  33. } else {
  34. throw new Error('Asset code must be 12 characters at max.');
  35. }
  36. if (typeof opts.authorize === 'boolean') {
  37. if (opts.authorize) {
  38. attributes.authorize = xdr.TrustLineFlags.authorizedFlag().value;
  39. } else {
  40. attributes.authorize = 0;
  41. }
  42. } else {
  43. attributes.authorize = opts.authorize;
  44. }
  45. const allowTrustOp = new xdr.AllowTrustOp(attributes);
  46. const opAttributes = {};
  47. opAttributes.body = xdr.OperationBody.allowTrust(allowTrustOp);
  48. this.setSourceAccount(opAttributes, opts);
  49. return new xdr.Operation(opAttributes);
  50. }