@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/README.md +160 -109
- package/jest.config.cjs +4 -0
- package/lib/stellarbroker.js +1 -1
- package/lib/stellarbroker.js.map +1 -1
- package/package.json +10 -9
- package/src/asset.js +51 -47
- package/src/authorization.js +65 -0
- package/src/client.js +353 -369
- package/src/errors.js +55 -53
- package/src/index.d.ts +95 -25
- package/src/index.js +4 -3
- package/src/mediator.js +309 -0
- package/src/quote-request.js +56 -60
- package/src/tx-processor.js +133 -161
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
|
|
10
|
-
return
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
+
*/
|