@wishknish/knishio-client-js 0.7.4 → 0.7.5
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/dist/client.cjs.js +81 -27
- package/dist/client.cjs.js.map +1 -1
- package/dist/client.es.mjs +1256 -767
- package/dist/client.es.mjs.map +1 -1
- package/dist/client.iife.js +81 -27
- package/dist/client.iife.js.map +1 -1
- package/package.json +1 -1
- package/src/KnishIOClient.js +47 -2
- package/src/Molecule.js +102 -14
- package/src/Wallet.js +48 -1
- package/src/index.js +6 -0
- package/src/libraries/CheckMolecule.js +211 -5
- package/src/query/QueryMetaTypeViaMolecule.js +223 -0
- package/src/response/ResponseContinuId.js +1 -1
- package/src/response/ResponseMetaTypeViaMolecule.js +210 -0
- package/src/response/ResponseWalletList.js +1 -1
|
@@ -0,0 +1,210 @@
|
|
|
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-JS for information.
|
|
45
|
+
|
|
46
|
+
License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
import Query from '../query/Query.js'
|
|
50
|
+
import Response from './Response.js'
|
|
51
|
+
import CheckMolecule from '../libraries/CheckMolecule.js'
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Response for MetaType queries via Molecule data.
|
|
55
|
+
*
|
|
56
|
+
* Instead of using the redundant instance-level `metas` field,
|
|
57
|
+
* this response extracts metadata from molecule atoms' `metasJson`,
|
|
58
|
+
* producing a payload format compatible with ResponseMetaType and
|
|
59
|
+
* ResponseMetaTypeViaAtom for drop-in replacement usage.
|
|
60
|
+
*/
|
|
61
|
+
export default class ResponseMetaTypeViaMolecule extends Response {
|
|
62
|
+
/**
|
|
63
|
+
* Class constructor
|
|
64
|
+
*
|
|
65
|
+
* @param {Query} query
|
|
66
|
+
* @param {object} json
|
|
67
|
+
*/
|
|
68
|
+
constructor ({
|
|
69
|
+
query,
|
|
70
|
+
json
|
|
71
|
+
}) {
|
|
72
|
+
super({
|
|
73
|
+
query,
|
|
74
|
+
json,
|
|
75
|
+
dataKey: 'data.MetaTypeViaAtom'
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Extracts metas from a molecule's atoms' metasJson for a specific instance.
|
|
81
|
+
* Filters atoms by matching metaType and metaId, then parses metasJson.
|
|
82
|
+
*
|
|
83
|
+
* @param {object} molecule - Molecule data with atoms array
|
|
84
|
+
* @param {string} metaType - Instance meta type to filter by
|
|
85
|
+
* @param {string} metaId - Instance meta ID to filter by
|
|
86
|
+
* @return {Array<{molecularHash: string, position: string, key: string, value: string, createdAt: string}>}
|
|
87
|
+
*/
|
|
88
|
+
static extractMetasFromMolecule (molecule, metaType, metaId) {
|
|
89
|
+
if (!molecule || !molecule.atoms) {
|
|
90
|
+
return []
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const metas = []
|
|
94
|
+
|
|
95
|
+
for (const atom of molecule.atoms) {
|
|
96
|
+
// Filter atoms to those matching this instance's metaType and metaId
|
|
97
|
+
if (atom.metaType !== metaType || atom.metaId !== metaId) {
|
|
98
|
+
continue
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!atom.metasJson) {
|
|
102
|
+
continue
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let parsed
|
|
106
|
+
try {
|
|
107
|
+
parsed = JSON.parse(atom.metasJson)
|
|
108
|
+
if (!Array.isArray(parsed)) {
|
|
109
|
+
continue
|
|
110
|
+
}
|
|
111
|
+
} catch (e) {
|
|
112
|
+
continue
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
for (const entry of parsed) {
|
|
116
|
+
metas.push({
|
|
117
|
+
molecularHash: molecule.molecularHash,
|
|
118
|
+
position: atom.position,
|
|
119
|
+
key: entry.key,
|
|
120
|
+
value: entry.value,
|
|
121
|
+
createdAt: atom.createdAt
|
|
122
|
+
})
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return metas
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Returns meta type instance results with metas synthesized from molecule data.
|
|
131
|
+
* Produces the same payload format as ResponseMetaType and ResponseMetaTypeViaAtom:
|
|
132
|
+
* { instances, instanceCount, paginatorInfo }
|
|
133
|
+
*
|
|
134
|
+
* @return {null|{instances: Array, instanceCount: Array, paginatorInfo: object}}
|
|
135
|
+
*/
|
|
136
|
+
payload () {
|
|
137
|
+
const metaTypeData = this.data()
|
|
138
|
+
|
|
139
|
+
if (!metaTypeData || metaTypeData.length === 0) {
|
|
140
|
+
return null
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const response = {
|
|
144
|
+
instances: {},
|
|
145
|
+
instanceCount: {},
|
|
146
|
+
paginatorInfo: {}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const metaData = metaTypeData.pop()
|
|
150
|
+
|
|
151
|
+
if (metaData.instances) {
|
|
152
|
+
response.instances = metaData.instances.map(instance => {
|
|
153
|
+
// Prefer server-filtered metas (from metas sub-field) when available
|
|
154
|
+
let metas = instance.metas
|
|
155
|
+
if (!metas || metas.length === 0) {
|
|
156
|
+
// Fallback: synthesize from molecule atoms' metasJson
|
|
157
|
+
metas = ResponseMetaTypeViaMolecule.extractMetasFromMolecule(
|
|
158
|
+
instance.molecule,
|
|
159
|
+
instance.metaType,
|
|
160
|
+
instance.metaId
|
|
161
|
+
)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
...instance,
|
|
166
|
+
metas
|
|
167
|
+
}
|
|
168
|
+
})
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (metaData.instanceCount) {
|
|
172
|
+
response.instanceCount = metaData.instanceCount
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (metaData.paginatorInfo) {
|
|
176
|
+
response.paginatorInfo = metaData.paginatorInfo
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return response
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Verifies the cryptographic integrity of all molecules associated
|
|
184
|
+
* with meta instances in this response. For each instance, reconstructs
|
|
185
|
+
* the molecule from server data and runs CheckMolecule.verify() to validate
|
|
186
|
+
* the molecular hash and OTS signature.
|
|
187
|
+
*
|
|
188
|
+
* @return {{ verified: boolean, molecules: Array<{ molecularHash: string, verified: boolean, error: string|null }> }}
|
|
189
|
+
*/
|
|
190
|
+
verifyIntegrity () {
|
|
191
|
+
const results = []
|
|
192
|
+
const metaTypeData = this.data()
|
|
193
|
+
|
|
194
|
+
if (!metaTypeData || metaTypeData.length === 0) {
|
|
195
|
+
return { verified: true, molecules: results }
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const instances = metaTypeData[metaTypeData.length - 1]?.instances || []
|
|
199
|
+
|
|
200
|
+
for (const instance of instances) {
|
|
201
|
+
if (!instance.molecule) continue
|
|
202
|
+
results.push(CheckMolecule.verifyFromServerData(instance.molecule))
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return {
|
|
206
|
+
verified: results.length === 0 || results.every(r => r.verified),
|
|
207
|
+
molecules: results
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
@@ -122,7 +122,7 @@ export default class ResponseWalletList extends Response {
|
|
|
122
122
|
}
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
-
wallet.balance =
|
|
125
|
+
wallet.balance = String(data.amount != null ? data.amount : 0)
|
|
126
126
|
wallet.pubkey = data.pubkey
|
|
127
127
|
wallet.createdAt = data.createdAt
|
|
128
128
|
|