@stellar-broker/client 0.4.1 → 0.6.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,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
+ */