@stellar-broker/client 0.4.1 → 0.5.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.
package/src/asset.js CHANGED
@@ -1,47 +1,51 @@
1
- import {Asset, StrKey} from '@stellar/stellar-sdk'
2
- import errors from './errors.js'
3
-
4
- /**
5
- * @param {string} asset
6
- * @return {Asset}
7
- */
8
- export function convertToStellarAsset(asset) {
9
- if (asset === 'xlm' || asset === 'XLM')
10
- return Asset.native()
11
- const [code, issuer] = asset.includes('-') ?
12
- asset.split('-') :
13
- asset.split(':')
14
- return new Asset(code, issuer)
15
- }
16
-
17
- export function parseAsset(asset, parameter) {
18
- if (typeof asset === 'string') {
19
- if (asset === 'XLM' || asset === 'xlm' || asset === 'native')
20
- return 'XLM'
21
- if (asset.includes(':')) {
22
- const [code, issuer] = asset.split(':')
23
- validateCode(code)
24
- validateAccount(issuer, parameter)
25
- return code + '-' + issuer
26
- }
27
- if (asset.includes('-')) {
28
- const [code, issuer] = asset.split('-')
29
- validateCode(code)
30
- validateAccount(issuer, parameter)
31
- return asset
32
- }
33
- }
34
- throw errors.invalidQuoteParam(parameter, 'Invalid asset')
35
- }
36
-
37
- function validateCode(code, parameter) {
38
- if (!/^[a-zA-Z0-9]{1,12}$/.test(code))
39
- throw errors.invalidQuoteParam(parameter, 'Invalid asset code: ' + (!code ? 'missing' : code))
40
- return code
41
- }
42
-
43
- function validateAccount(account, param) {
44
- if (!account || !StrKey.isValidEd25519PublicKey(account))
45
- throw errors.invalidQuoteParam(param, 'Invalid account address: ' + (!account ? 'missing' : account))
46
- return account
47
- }
1
+ import {Asset, StrKey} from '@stellar/stellar-sdk'
2
+ import errors from './errors.js'
3
+
4
+ /**
5
+ * @param {string|Asset|{}} asset
6
+ * @return {Asset}
7
+ */
8
+ export function convertToStellarAsset(asset) {
9
+ if (asset instanceof Asset)
10
+ return asset
11
+ if (asset === 'xlm' || asset === 'XLM' || asset.asset_type === 'native')
12
+ return Asset.native()
13
+ if (asset.asset_type)
14
+ return new Asset(asset.asset_code, asset.asset_issuer)
15
+ const [code, issuer] = asset.includes('-') ?
16
+ asset.split('-') :
17
+ asset.split(':')
18
+ return new Asset(code, issuer)
19
+ }
20
+
21
+ export function parseAsset(asset, parameter) {
22
+ if (typeof asset === 'string') {
23
+ if (asset === 'XLM' || asset === 'xlm' || asset === 'native')
24
+ return 'XLM'
25
+ if (asset.includes(':')) {
26
+ const [code, issuer] = asset.split(':')
27
+ validateCode(code)
28
+ validateAccount(issuer, parameter)
29
+ return code + '-' + issuer
30
+ }
31
+ if (asset.includes('-')) {
32
+ const [code, issuer] = asset.split('-')
33
+ validateCode(code)
34
+ validateAccount(issuer, parameter)
35
+ return asset
36
+ }
37
+ }
38
+ throw errors.invalidQuoteParam(parameter, 'Invalid asset')
39
+ }
40
+
41
+ function validateCode(code, parameter) {
42
+ if (!/^[a-zA-Z0-9]{1,12}$/.test(code))
43
+ throw errors.invalidQuoteParam(parameter, 'Invalid asset code: ' + (!code ? 'missing' : code))
44
+ return code
45
+ }
46
+
47
+ function validateAccount(account, param) {
48
+ if (!account || !StrKey.isValidEd25519PublicKey(account))
49
+ throw errors.invalidQuoteParam(param, 'Invalid account address: ' + (!account ? 'missing' : account))
50
+ return account
51
+ }
@@ -0,0 +1,65 @@
1
+ import {Keypair} from '@stellar/stellar-sdk'
2
+ import errors from './errors.js'
3
+
4
+ export class AuthorizationWrapper {
5
+ /**
6
+ * @param {ClientAuthorizationParams} authorization
7
+ */
8
+ constructor(authorization) {
9
+ if (typeof authorization === 'string') {
10
+ try {
11
+ this.authorization = Keypair.fromSecret(authorization)
12
+ return
13
+ } catch (e) {
14
+ throw errors.invalidAuthorizationParam()
15
+ }
16
+ } else if (typeof authorization === 'function') {
17
+ this.authorization = authorization
18
+ return
19
+ }
20
+ throw errors.invalidAuthorizationParam()
21
+ }
22
+
23
+ /**
24
+ * @type {Keypair|ClientAuthorizationCallback}
25
+ * @private
26
+ */
27
+ authorization
28
+
29
+ /**
30
+ * Authorize transaction or invocation footprint hash
31
+ * @param {TransactionI|Buffer} payload
32
+ * @return {Promise<TransactionI|Buffer>}
33
+ */
34
+ async authorize(payload) {
35
+ if (this.authorization instanceof Keypair) {
36
+ if (payload.sign){
37
+ payload.sign(this.authorization)
38
+ return payload
39
+ }
40
+ return this.authorization.sign(payload)
41
+ }
42
+ return await promisify(this.authorization(payload))
43
+ }
44
+ }
45
+
46
+ /**
47
+ * @param {*|Promise} callResult
48
+ * @return {Promise}
49
+ */
50
+ function promisify(callResult) {
51
+ if (!(callResult instanceof Promise)) {
52
+ if (!callResult)
53
+ return Promise.reject()
54
+ callResult = Promise.resolve(callResult)
55
+ }
56
+ return callResult
57
+ }
58
+
59
+ /**
60
+ * @typedef {function(TransactionI|Buffer):Promise<TransactionI|Buffer>} ClientAuthorizationCallback - Async authorization callback
61
+ */
62
+
63
+ /**
64
+ * @typedef {string|ClientAuthorizationCallback} ClientAuthorizationParams - Authorization method, either account secret key or an authorization callback
65
+ */