@wishknish/knishio-client-ts 0.7.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/LICENSE +674 -0
- package/README.md +425 -0
- package/dist/index.cjs +9961 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.iife.js +32241 -0
- package/dist/index.iife.js.map +1 -0
- package/dist/index.js +9853 -0
- package/dist/index.js.map +1 -0
- package/package.json +113 -0
- package/src/AuthToken.ts +214 -0
- package/src/KnishIOClient.ts +2020 -0
- package/src/constants.ts +398 -0
- package/src/core/Atom.ts +646 -0
- package/src/core/AtomMeta.ts +278 -0
- package/src/core/Meta.ts +428 -0
- package/src/core/Molecule.ts +825 -0
- package/src/core/PolicyMeta.ts +130 -0
- package/src/core/TokenUnit.ts +148 -0
- package/src/core/Wallet.ts +467 -0
- package/src/exception/AtomIndexException.ts +97 -0
- package/src/exception/AtomsMissingException.ts +109 -0
- package/src/exception/AuthorizationRejectedException.ts +63 -0
- package/src/exception/BalanceInsufficientException.ts +58 -0
- package/src/exception/BaseException.ts +275 -0
- package/src/exception/BatchIdException.ts +58 -0
- package/src/exception/CodeException.ts +119 -0
- package/src/exception/DecryptionKeyException.ts +65 -0
- package/src/exception/InvalidResponseException.ts +112 -0
- package/src/exception/MetaMissingException.ts +58 -0
- package/src/exception/MolecularHashMismatchException.ts +115 -0
- package/src/exception/MolecularHashMissingException.ts +58 -0
- package/src/exception/NegativeAmountException.ts +58 -0
- package/src/exception/PolicyInvalidException.ts +58 -0
- package/src/exception/SignatureMalformedException.ts +58 -0
- package/src/exception/SignatureMismatchException.ts +98 -0
- package/src/exception/StackableUnitAmountException.ts +65 -0
- package/src/exception/StackableUnitDecimalsException.ts +65 -0
- package/src/exception/TransferBalanceException.ts +98 -0
- package/src/exception/TransferMalformedException.ts +58 -0
- package/src/exception/TransferMismatchedException.ts +58 -0
- package/src/exception/TransferRemainderException.ts +58 -0
- package/src/exception/TransferToSelfException.ts +58 -0
- package/src/exception/TransferUnbalancedException.ts +58 -0
- package/src/exception/UnauthenticatedException.ts +147 -0
- package/src/exception/WalletCredentialException.ts +108 -0
- package/src/exception/WalletShadowException.ts +65 -0
- package/src/exception/WrongTokenTypeException.ts +58 -0
- package/src/exception/index.ts +272 -0
- package/src/index.ts +512 -0
- package/src/instance/rules/Callback.ts +257 -0
- package/src/instance/rules/Condition.ts +96 -0
- package/src/instance/rules/Meta.ts +163 -0
- package/src/instance/rules/Rule.ts +29 -0
- package/src/instance/rules/exception/RuleArgumentException.ts +65 -0
- package/src/libraries/CheckMolecule.ts +581 -0
- package/src/libraries/Decimal.ts +94 -0
- package/src/libraries/Dot.ts +202 -0
- package/src/libraries/GraphQLClient.ts +276 -0
- package/src/libraries/Hex.ts +155 -0
- package/src/libraries/UrqlClientWrapper.ts +336 -0
- package/src/libraries/array.ts +91 -0
- package/src/libraries/crypto.ts +816 -0
- package/src/libraries/strings.ts +458 -0
- package/src/mutation/Mutation.ts +103 -0
- package/src/mutation/MutationActiveSession.ts +108 -0
- package/src/mutation/MutationClaimShadowWallet.ts +91 -0
- package/src/mutation/MutationCreateIdentifier.ts +86 -0
- package/src/mutation/MutationCreateMeta.ts +92 -0
- package/src/mutation/MutationCreateRule.ts +90 -0
- package/src/mutation/MutationCreateToken.ts +92 -0
- package/src/mutation/MutationCreateWallet.ts +78 -0
- package/src/mutation/MutationDepositBufferToken.ts +72 -0
- package/src/mutation/MutationLinkIdentifier.ts +87 -0
- package/src/mutation/MutationProposeMolecule.ts +147 -0
- package/src/mutation/MutationRequestAuthorization.ts +77 -0
- package/src/mutation/MutationRequestAuthorizationGuest.ts +85 -0
- package/src/mutation/MutationRequestTokens.ts +98 -0
- package/src/mutation/MutationTransferTokens.ts +87 -0
- package/src/mutation/MutationWithdrawBufferToken.ts +73 -0
- package/src/query/Query.ts +187 -0
- package/src/query/QueryActiveSession.ts +89 -0
- package/src/query/QueryAtom.ts +275 -0
- package/src/query/QueryBalance.ts +102 -0
- package/src/query/QueryBatch.ts +145 -0
- package/src/query/QueryBatchHistory.ts +86 -0
- package/src/query/QueryContinuId.ts +91 -0
- package/src/query/QueryMetaType.ts +177 -0
- package/src/query/QueryMetaTypeViaAtom.ts +199 -0
- package/src/query/QueryPolicy.ts +91 -0
- package/src/query/QueryToken.ts +90 -0
- package/src/query/QueryUserActivity.ts +153 -0
- package/src/query/QueryWalletBundle.ts +92 -0
- package/src/query/QueryWalletList.ts +106 -0
- package/src/response/EnhancedResponse.ts +345 -0
- package/src/response/Response.ts +254 -0
- package/src/response/ResponseActiveSession.ts +72 -0
- package/src/response/ResponseAtom.ts +132 -0
- package/src/response/ResponseAuthorizationGuest.ts +119 -0
- package/src/response/ResponseBalance.ts +153 -0
- package/src/response/ResponseClaimShadowWallet.ts +56 -0
- package/src/response/ResponseContinuId.ts +101 -0
- package/src/response/ResponseCreateIdentifier.ts +56 -0
- package/src/response/ResponseCreateMeta.ts +58 -0
- package/src/response/ResponseCreateRule.ts +56 -0
- package/src/response/ResponseCreateToken.ts +58 -0
- package/src/response/ResponseCreateWallet.ts +58 -0
- package/src/response/ResponseLinkIdentifier.ts +87 -0
- package/src/response/ResponseMetaBatch.ts +72 -0
- package/src/response/ResponseMetaType.ts +108 -0
- package/src/response/ResponseMetaTypeViaAtom.ts +108 -0
- package/src/response/ResponsePolicy.ts +90 -0
- package/src/response/ResponseProposeMolecule.ts +155 -0
- package/src/response/ResponseQueryActiveSession.ts +105 -0
- package/src/response/ResponseQueryUserActivity.ts +89 -0
- package/src/response/ResponseRequestAuthorization.ts +100 -0
- package/src/response/ResponseRequestAuthorizationGuest.ts +133 -0
- package/src/response/ResponseRequestTokens.ts +58 -0
- package/src/response/ResponseTransferTokens.ts +72 -0
- package/src/response/ResponseWalletBundle.ts +95 -0
- package/src/response/ResponseWalletList.ts +165 -0
- package/src/schemas/index.ts +457 -0
- package/src/subscribe/ActiveSessionSubscribe.ts +72 -0
- package/src/subscribe/ActiveWalletSubscribe.ts +99 -0
- package/src/subscribe/CreateMoleculeSubscribe.ts +106 -0
- package/src/subscribe/Subscribe.ts +182 -0
- package/src/subscribe/WalletStatusSubscribe.ts +70 -0
- package/src/subscribe/index.ts +59 -0
- package/src/types/assertions.ts +722 -0
- package/src/types/client.ts +567 -0
- package/src/types/crypto.ts +541 -0
- package/src/types/graphql.ts +630 -0
- package/src/types/guards.ts +659 -0
- package/src/types/index.ts +614 -0
- package/src/types/response.ts +133 -0
- package/src/types/template-literals.ts +382 -0
- package/src/validation/UNIVERSAL_CONFIGURATION_INTERFACES.ts +580 -0
- package/src/validation/ValidationService.ts +607 -0
- package/src/validation/schemas.ts +447 -0
- package/src/versions/HashAtom.ts +170 -0
- package/src/versions/Version4.ts +120 -0
- package/src/versions/index.ts +80 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility class for accessing nested object properties using dot notation
|
|
3
|
+
* Matches JavaScript SDK Dot class functionality exactly
|
|
4
|
+
*/
|
|
5
|
+
export default class Dot {
|
|
6
|
+
private static arr: string[]
|
|
7
|
+
private static key: string | number
|
|
8
|
+
private static __nextKey: number
|
|
9
|
+
private static __next: boolean
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Initialize the Dot utility with the given object and key path
|
|
13
|
+
* @param obj - The object or array to traverse
|
|
14
|
+
* @param keys - The dot-notated string of keys
|
|
15
|
+
* @private
|
|
16
|
+
*/
|
|
17
|
+
private static __init(obj: any, keys: string): void {
|
|
18
|
+
this.arr = String(keys).split('.')
|
|
19
|
+
this.key = this.arr.shift() as string
|
|
20
|
+
|
|
21
|
+
// Convert to number if the key is a valid integer
|
|
22
|
+
const numberKey = Number(this.key)
|
|
23
|
+
if (Number.isInteger(numberKey)) {
|
|
24
|
+
this.key = numberKey
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
this.__nextKey = this.arr.length
|
|
28
|
+
this.__next = this.__tic(obj)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Check if the current key exists in the object
|
|
33
|
+
* @param obj - The object or array to check
|
|
34
|
+
* @return Whether the key exists
|
|
35
|
+
* @private
|
|
36
|
+
*/
|
|
37
|
+
private static __tic(obj: any): boolean {
|
|
38
|
+
if (!Array.isArray(obj) && !(obj instanceof Object)) {
|
|
39
|
+
return false
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return typeof obj[this.key] !== 'undefined'
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Check if a nested property exists in an object using dot notation
|
|
47
|
+
* @param obj - The object or array to search
|
|
48
|
+
* @param keys - The path to the property, using dot notation
|
|
49
|
+
* @return True if the property exists, false otherwise
|
|
50
|
+
*/
|
|
51
|
+
static has(obj: any, keys: string): boolean {
|
|
52
|
+
this.__init(obj, keys)
|
|
53
|
+
|
|
54
|
+
if (!this.__next) {
|
|
55
|
+
return false
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (this.__nextKey) {
|
|
59
|
+
return this.has(obj[this.key], this.arr.join('.'))
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return true
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Get a nested property from an object using dot notation
|
|
67
|
+
* @param obj - The object or array to search
|
|
68
|
+
* @param keys - The path to the property, using dot notation
|
|
69
|
+
* @param defaultValue - The default value to return if the property doesn't exist
|
|
70
|
+
* @return The value at the specified path, or defaultValue if not found
|
|
71
|
+
*/
|
|
72
|
+
static get(obj: any, keys: string, defaultValue: any = null): any {
|
|
73
|
+
this.__init(obj, keys)
|
|
74
|
+
|
|
75
|
+
if (!this.__next) {
|
|
76
|
+
return defaultValue
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (this.__nextKey) {
|
|
80
|
+
return this.get(obj[this.key], this.arr.join('.'), defaultValue)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return obj[this.key]
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Set a nested property in an object using dot notation
|
|
88
|
+
* @param obj - The object or array to modify
|
|
89
|
+
* @param keys - The path to the property, using dot notation
|
|
90
|
+
* @param value - The value to set
|
|
91
|
+
* @return The modified object
|
|
92
|
+
*/
|
|
93
|
+
static set(obj: any, keys: string, value: any): any {
|
|
94
|
+
if (typeof obj !== 'object' || obj === null) {
|
|
95
|
+
obj = {}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const keysArray = String(keys).split('.')
|
|
99
|
+
let current = obj
|
|
100
|
+
|
|
101
|
+
for (let i = 0; i < keysArray.length - 1; i++) {
|
|
102
|
+
const key = keysArray[i]
|
|
103
|
+
if (!key) continue // Skip undefined keys
|
|
104
|
+
|
|
105
|
+
// Convert to number if valid integer
|
|
106
|
+
const numberKey = Number(key)
|
|
107
|
+
const actualKey: string | number = Number.isInteger(numberKey) ? numberKey : key
|
|
108
|
+
|
|
109
|
+
if (!(actualKey in current) || typeof current[actualKey] !== 'object') {
|
|
110
|
+
// Determine if next key is numeric to decide between array or object
|
|
111
|
+
const nextKey = keysArray[i + 1]
|
|
112
|
+
const nextNumberKey = Number(nextKey)
|
|
113
|
+
current[actualKey] = Number.isInteger(nextNumberKey) ? [] : {}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
current = current[actualKey]
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const lastKey = keysArray[keysArray.length - 1]
|
|
120
|
+
if (lastKey !== undefined) {
|
|
121
|
+
const numberKey = Number(lastKey)
|
|
122
|
+
const actualLastKey: string | number = Number.isInteger(numberKey) ? numberKey : lastKey
|
|
123
|
+
current[actualLastKey] = value
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return obj
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Delete a nested property from an object using dot notation
|
|
131
|
+
* @param obj - The object or array to modify
|
|
132
|
+
* @param keys - The path to the property, using dot notation
|
|
133
|
+
* @return True if the property was deleted, false otherwise
|
|
134
|
+
*/
|
|
135
|
+
static delete(obj: any, keys: string): boolean {
|
|
136
|
+
if (!this.has(obj, keys)) {
|
|
137
|
+
return false
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const keysArray = String(keys).split('.')
|
|
141
|
+
const lastKey = keysArray.pop() as string
|
|
142
|
+
|
|
143
|
+
if (keysArray.length === 0) {
|
|
144
|
+
const numberKey = Number(lastKey)
|
|
145
|
+
const actualKey = Number.isInteger(numberKey) ? numberKey : lastKey
|
|
146
|
+
delete obj[actualKey]
|
|
147
|
+
return true
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const parent = this.get(obj, keysArray.join('.'))
|
|
151
|
+
if (parent && typeof parent === 'object') {
|
|
152
|
+
const numberKey = Number(lastKey)
|
|
153
|
+
const actualKey = Number.isInteger(numberKey) ? numberKey : lastKey
|
|
154
|
+
delete parent[actualKey]
|
|
155
|
+
return true
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return false
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Flatten an object with nested properties into dot notation
|
|
163
|
+
* @param obj - The object to flatten
|
|
164
|
+
* @param prefix - The prefix for keys (used recursively)
|
|
165
|
+
* @return A flattened object with dot-notation keys
|
|
166
|
+
*/
|
|
167
|
+
static flatten(obj: any, prefix = ''): Record<string, any> {
|
|
168
|
+
const result: Record<string, any> = {}
|
|
169
|
+
|
|
170
|
+
for (const key in obj) {
|
|
171
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
172
|
+
const value = obj[key]
|
|
173
|
+
const newKey = prefix ? `${prefix}.${key}` : key
|
|
174
|
+
|
|
175
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
176
|
+
Object.assign(result, this.flatten(value, newKey))
|
|
177
|
+
} else {
|
|
178
|
+
result[newKey] = value
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return result
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Expand a flattened object with dot-notation keys back to nested structure
|
|
188
|
+
* @param obj - The flattened object with dot-notation keys
|
|
189
|
+
* @return The expanded nested object
|
|
190
|
+
*/
|
|
191
|
+
static expand(obj: Record<string, any>): any {
|
|
192
|
+
const result: any = {}
|
|
193
|
+
|
|
194
|
+
for (const key in obj) {
|
|
195
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
196
|
+
this.set(result, key, obj[key])
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return result
|
|
201
|
+
}
|
|
202
|
+
}
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/*
|
|
2
|
+
(
|
|
3
|
+
(/(
|
|
4
|
+
(//(
|
|
5
|
+
(///(
|
|
6
|
+
(/////(
|
|
7
|
+
(//////( )
|
|
8
|
+
(////////( (/)
|
|
9
|
+
(////////( (///)
|
|
10
|
+
(//////////( (////)
|
|
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
|
+
Powered by Knish.IO: Connecting a Decentralized World
|
|
43
|
+
|
|
44
|
+
Please visit https://github.com/WishKnish/KnishIO-Client-TS for information.
|
|
45
|
+
|
|
46
|
+
License: https://github.com/WishKnish/KnishIO-Client-TS/blob/master/LICENSE
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
import {
|
|
50
|
+
createClient,
|
|
51
|
+
Client,
|
|
52
|
+
subscriptionExchange,
|
|
53
|
+
cacheExchange,
|
|
54
|
+
fetchExchange
|
|
55
|
+
} from '@urql/core'
|
|
56
|
+
import { createClient as createWSClient } from 'graphql-ws'
|
|
57
|
+
import { pipe, subscribe } from 'wonka'
|
|
58
|
+
import type { GraphQLClient as IGraphQLClient } from '@/types'
|
|
59
|
+
import type Wallet from '@/core/Wallet'
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* GraphQL client wrapper for KnishIO SDK
|
|
63
|
+
* Provides a unified interface for GraphQL operations
|
|
64
|
+
*/
|
|
65
|
+
export default class GraphQLClient implements IGraphQLClient {
|
|
66
|
+
private $__client: Client
|
|
67
|
+
private $__authToken: string = ''
|
|
68
|
+
private $__pubkey: string | null = null
|
|
69
|
+
private $__wallet: Wallet | null = null
|
|
70
|
+
private serverUri: string
|
|
71
|
+
private socketUri: string | null = null
|
|
72
|
+
private cipherLink: boolean
|
|
73
|
+
private $__subscriptionManager: Map<string, { unsubscribe: () => void }> = new Map()
|
|
74
|
+
|
|
75
|
+
constructor({
|
|
76
|
+
serverUri,
|
|
77
|
+
socket = null,
|
|
78
|
+
encrypt = false
|
|
79
|
+
}: {
|
|
80
|
+
serverUri: string
|
|
81
|
+
socket?: { socketUri: string | null; appKey?: string } | null
|
|
82
|
+
encrypt?: boolean
|
|
83
|
+
}) {
|
|
84
|
+
this.serverUri = serverUri
|
|
85
|
+
this.socketUri = socket?.socketUri || null
|
|
86
|
+
this.cipherLink = !!encrypt
|
|
87
|
+
this.$__client = this.createUrqlClient({ serverUri, socket, encrypt })
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
private createUrqlClient({
|
|
91
|
+
serverUri,
|
|
92
|
+
socket,
|
|
93
|
+
encrypt
|
|
94
|
+
}: {
|
|
95
|
+
serverUri: string
|
|
96
|
+
socket?: { socketUri: string | null } | null
|
|
97
|
+
encrypt?: boolean
|
|
98
|
+
}): Client {
|
|
99
|
+
const exchanges = [cacheExchange, fetchExchange]
|
|
100
|
+
|
|
101
|
+
// Add subscription support if socket is configured
|
|
102
|
+
if (socket?.socketUri) {
|
|
103
|
+
const wsClient = createWSClient({
|
|
104
|
+
url: socket.socketUri,
|
|
105
|
+
connectionParams: () => ({
|
|
106
|
+
authToken: this.$__authToken
|
|
107
|
+
})
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
exchanges.push(
|
|
111
|
+
subscriptionExchange({
|
|
112
|
+
forwardSubscription: (operation) => ({
|
|
113
|
+
subscribe: (sink) => {
|
|
114
|
+
const disposable = wsClient.subscribe(operation, sink)
|
|
115
|
+
return { unsubscribe: disposable }
|
|
116
|
+
}
|
|
117
|
+
})
|
|
118
|
+
})
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return createClient({
|
|
123
|
+
url: serverUri,
|
|
124
|
+
exchanges,
|
|
125
|
+
fetchOptions: () => ({
|
|
126
|
+
headers: {
|
|
127
|
+
'X-Auth-Token': this.$__authToken
|
|
128
|
+
},
|
|
129
|
+
// Add 60 second timeout
|
|
130
|
+
signal: AbortSignal.timeout(60000)
|
|
131
|
+
})
|
|
132
|
+
})
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
setAuthData({
|
|
136
|
+
token,
|
|
137
|
+
pubkey,
|
|
138
|
+
wallet
|
|
139
|
+
}: {
|
|
140
|
+
token: string
|
|
141
|
+
pubkey: string
|
|
142
|
+
wallet?: Wallet
|
|
143
|
+
}): void {
|
|
144
|
+
this.$__authToken = token
|
|
145
|
+
this.$__pubkey = pubkey
|
|
146
|
+
this.$__wallet = wallet || null
|
|
147
|
+
|
|
148
|
+
// Recreate client with new auth data
|
|
149
|
+
this.$__client = this.createUrqlClient({
|
|
150
|
+
serverUri: this.serverUri,
|
|
151
|
+
socket: this.socketUri ? { socketUri: this.socketUri } : null,
|
|
152
|
+
encrypt: this.cipherLink
|
|
153
|
+
})
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async query<TResult = unknown, TVariables = Record<string, any>>(
|
|
157
|
+
request: { query: string; variables?: TVariables },
|
|
158
|
+
variables?: TVariables
|
|
159
|
+
): Promise<any> {
|
|
160
|
+
const finalVariables = variables || request.variables || {}
|
|
161
|
+
const result = await this.$__client.query(request.query, finalVariables).toPromise()
|
|
162
|
+
return this.formatResponse(result)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async mutate<TResult = unknown, TVariables = Record<string, any>>(
|
|
166
|
+
request: { mutation: string; variables?: TVariables },
|
|
167
|
+
variables?: TVariables
|
|
168
|
+
): Promise<any> {
|
|
169
|
+
const finalVariables = variables || request.variables || {}
|
|
170
|
+
const result = await this.$__client.mutation(request.mutation, finalVariables).toPromise()
|
|
171
|
+
return this.formatResponse(result)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async mutation<TResult = unknown, TVariables = Record<string, any>>(
|
|
175
|
+
request: { mutation: string; variables?: TVariables },
|
|
176
|
+
variables?: TVariables
|
|
177
|
+
): Promise<any> {
|
|
178
|
+
return this.mutate(request, variables)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
subscribe(
|
|
182
|
+
request: {
|
|
183
|
+
query: string
|
|
184
|
+
variables?: Record<string, any>
|
|
185
|
+
operationName?: string
|
|
186
|
+
},
|
|
187
|
+
callback: (result: any) => void
|
|
188
|
+
): { unsubscribe: () => void } {
|
|
189
|
+
const { query, variables, operationName = 'subscription' } = request
|
|
190
|
+
|
|
191
|
+
const { unsubscribe } = pipe(
|
|
192
|
+
this.$__client.subscription(query, variables || {}),
|
|
193
|
+
subscribe((result) => {
|
|
194
|
+
callback(this.formatResponse(result))
|
|
195
|
+
})
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
// Store subscription for later cleanup
|
|
199
|
+
this.$__subscriptionManager.set(operationName, { unsubscribe })
|
|
200
|
+
|
|
201
|
+
return {
|
|
202
|
+
unsubscribe: () => this.unsubscribe(operationName)
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
private formatResponse(result: any): any {
|
|
207
|
+
// Match old Apollo response format
|
|
208
|
+
return {
|
|
209
|
+
data: result.data,
|
|
210
|
+
errors: result.error ? [result.error] : undefined
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
unsubscribe(operationName: string): void {
|
|
215
|
+
const subscription = this.$__subscriptionManager.get(operationName)
|
|
216
|
+
if (subscription) {
|
|
217
|
+
subscription.unsubscribe()
|
|
218
|
+
this.$__subscriptionManager.delete(operationName)
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
unsubscribeAll(): void {
|
|
223
|
+
this.$__subscriptionManager.forEach((subscription, operationName) => {
|
|
224
|
+
this.unsubscribe(operationName)
|
|
225
|
+
})
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
setEncryption(encrypt: boolean = false): void {
|
|
229
|
+
this.cipherLink = encrypt
|
|
230
|
+
this.$__client = this.createUrqlClient({
|
|
231
|
+
serverUri: this.serverUri,
|
|
232
|
+
socket: this.socketUri ? { socketUri: this.socketUri } : null,
|
|
233
|
+
encrypt
|
|
234
|
+
})
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Getters for compatibility
|
|
238
|
+
getAuthToken(): string {
|
|
239
|
+
return this.$__authToken
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
getPubKey(): string | null {
|
|
243
|
+
return this.$__pubkey
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
getWallet(): Wallet | null {
|
|
247
|
+
return this.$__wallet
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
getServerUri(): string {
|
|
251
|
+
return this.serverUri
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
getSocketUri(): string | null {
|
|
255
|
+
return this.socketUri
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
getUri(): string {
|
|
259
|
+
return this.serverUri
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
setUri(uri: string): void {
|
|
263
|
+
this.serverUri = uri
|
|
264
|
+
this.$__client = this.createUrqlClient({
|
|
265
|
+
serverUri: uri,
|
|
266
|
+
socket: this.socketUri ? { socketUri: this.socketUri } : null,
|
|
267
|
+
encrypt: this.cipherLink
|
|
268
|
+
})
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
socketDisconnect(): void {
|
|
272
|
+
if (this.socketUri) {
|
|
273
|
+
this.unsubscribeAll()
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/*
|
|
2
|
+
(
|
|
3
|
+
(/(
|
|
4
|
+
(//(
|
|
5
|
+
(///(
|
|
6
|
+
(/////(
|
|
7
|
+
(//////( )
|
|
8
|
+
(////////( (/)
|
|
9
|
+
(////////( (///)
|
|
10
|
+
(//////////( (////)
|
|
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
|
+
Powered by Knish.IO: Connecting a Decentralized World
|
|
43
|
+
|
|
44
|
+
Please visit https://github.com/WishKnish/KnishIO-Client-TS for information.
|
|
45
|
+
|
|
46
|
+
License: https://github.com/WishKnish/KnishIO-Client-TS/blob/master/LICENSE
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Create Uint8Array buffers from hexadecimal strings, and vice versa.
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
// Global polyfill for environments that don't have self
|
|
54
|
+
if (typeof self === 'undefined') {
|
|
55
|
+
// @ts-ignore - Polyfill for environments without self
|
|
56
|
+
globalThis.self = globalThis
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
interface HexOptions {
|
|
60
|
+
grouping?: number
|
|
61
|
+
rowlength?: number
|
|
62
|
+
uppercase?: boolean
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export default class Hex {
|
|
66
|
+
/**
|
|
67
|
+
* Converts the given buffer to a string containing its hexadecimal representation.
|
|
68
|
+
*/
|
|
69
|
+
static toHex(arr: ArrayLike<number> | ArrayBuffer | Uint8Array, options?: HexOptions): string {
|
|
70
|
+
const numberToHex = (val: number, uppercase: boolean): string => {
|
|
71
|
+
const set = uppercase
|
|
72
|
+
? ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
|
|
73
|
+
: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
|
|
74
|
+
|
|
75
|
+
return set[Math.floor(val / 16)] + set[val % 16]
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const opts: Required<HexOptions> = Object.assign(
|
|
79
|
+
{
|
|
80
|
+
grouping: 0,
|
|
81
|
+
rowlength: 0,
|
|
82
|
+
uppercase: false
|
|
83
|
+
},
|
|
84
|
+
options || {}
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
// Convert to array-like structure for consistent access
|
|
88
|
+
let arrayLike: ArrayLike<number>
|
|
89
|
+
if (arr instanceof ArrayBuffer) {
|
|
90
|
+
arrayLike = new Uint8Array(arr)
|
|
91
|
+
} else {
|
|
92
|
+
arrayLike = arr as ArrayLike<number>
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
let str = ''
|
|
96
|
+
let group = 0
|
|
97
|
+
let column = 0
|
|
98
|
+
|
|
99
|
+
for (let i = 0; i < arrayLike.length; ++i) {
|
|
100
|
+
str += numberToHex(arrayLike[i], opts.uppercase)
|
|
101
|
+
|
|
102
|
+
if (i === arrayLike.length - 1) {
|
|
103
|
+
break
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (opts.grouping > 0 && ++group === opts.grouping) {
|
|
107
|
+
group = 0
|
|
108
|
+
|
|
109
|
+
if (opts.rowlength > 0 && ++column === opts.rowlength) {
|
|
110
|
+
column = 0
|
|
111
|
+
str += '\n'
|
|
112
|
+
} else {
|
|
113
|
+
str += ' '
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return str
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Takes a string containing hexadecimal and returns the equivalent as a Uint8Array buffer.
|
|
123
|
+
*
|
|
124
|
+
* Whitespace is ignored. If an odd number of characters are specified,
|
|
125
|
+
* it will act as if preceded with a leading 0; that is, "FFF" is equivalent to "0FFF".
|
|
126
|
+
*/
|
|
127
|
+
static toUint8Array(str: string): Uint8Array {
|
|
128
|
+
let target = str.toLowerCase().replace(/\s/g, '')
|
|
129
|
+
|
|
130
|
+
if (target.length % 2 === 1) {
|
|
131
|
+
target = `0${target}`
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const buffer = new Uint8Array(Math.floor(target.length / 2))
|
|
135
|
+
let curr = -1
|
|
136
|
+
|
|
137
|
+
for (let i = 0; i < target.length; ++i) {
|
|
138
|
+
const c = target[i]
|
|
139
|
+
const val = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'].indexOf(c)
|
|
140
|
+
|
|
141
|
+
if (val === -1) {
|
|
142
|
+
throw new Error('unexpected character')
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (curr === -1) {
|
|
146
|
+
curr = 16 * val
|
|
147
|
+
} else {
|
|
148
|
+
buffer[Math.floor(i / 2)] = curr + val
|
|
149
|
+
curr = -1
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return buffer
|
|
154
|
+
}
|
|
155
|
+
}
|