@stellar-expert/tx-meta-effects-parser 7.0.0-rc.8 → 7.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,73 @@
1
+ const {StrKey, Asset} = require('@stellar/stellar-base')
2
+ const {TxMetaEffectParserError} = require('../errors')
3
+
4
+ /**
5
+ * @param {String} address
6
+ * @return {String}
7
+ */
8
+ function normalizeAddress(address) {
9
+ const prefix = address[0]
10
+ if (prefix === 'G')
11
+ return address
12
+ if (prefix !== 'M')
13
+ throw new TypeError('Expected ED25519 or Muxed address')
14
+ const rawBytes = StrKey.decodeMed25519PublicKey(address)
15
+ return StrKey.encodeEd25519PublicKey(rawBytes.subarray(0, 32))
16
+ }
17
+
18
+
19
+ /**
20
+ * @param {String} address
21
+ * @return {Boolean}
22
+ */
23
+ function isContractAddress(address) {
24
+ return address.length === 56 && address[0] === 'C'
25
+ }
26
+
27
+ /**
28
+ * @param {String} asset
29
+ * @return {Asset}
30
+ */
31
+ function toStellarAsset(asset) {
32
+ if (asset === 'XLM')
33
+ return Asset.native()
34
+ if (asset.includes('-')) {
35
+ const [code, issuer] = asset.split('-')
36
+ return new Asset(code, issuer)
37
+ }
38
+ throw new TypeError('Unsupported asset format ' + asset)
39
+ }
40
+
41
+ /**
42
+ * @param {String} amount
43
+ * @param {Boolean} [throwIfInvalid]
44
+ * @return {String|null}
45
+ */
46
+ function validateAmount(amount, throwIfInvalid = true) {
47
+ let parsed
48
+ try {
49
+ if (typeof amount !== 'string')
50
+ throw new TypeError('Invalid amount type')
51
+ parsed = BigInt(amount)
52
+ } catch (e) {
53
+ if (!throwIfInvalid)
54
+ return null
55
+ throw new TxMetaEffectParserError('Invalid amount: ' + amount)
56
+ }
57
+ if (parsed < 0n) {
58
+ if (!throwIfInvalid)
59
+ return null
60
+ throw new TxMetaEffectParserError('Negative effect amount: ' + amount)
61
+ }
62
+ return amount
63
+ }
64
+
65
+ /**
66
+ * @param largeInt
67
+ * @return {String}
68
+ */
69
+ function parseLargeInt(largeInt) {
70
+ return largeInt._value.toString()
71
+ }
72
+
73
+ module.exports = {normalizeAddress, isContractAddress, toStellarAsset, validateAmount, parseLargeInt}