@xyo-network/boundwitness-builder 5.3.22 → 5.3.25

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xyo-network/boundwitness-builder",
3
- "version": "5.3.22",
3
+ "version": "5.3.25",
4
4
  "description": "Primary SDK for using XYO Protocol 2.0",
5
5
  "homepage": "https://xyo.network",
6
6
  "bugs": {
@@ -30,52 +30,48 @@
30
30
  "types": "dist/neutral/index.d.ts",
31
31
  "files": [
32
32
  "dist",
33
- "src",
34
33
  "!**/*.bench.*",
35
34
  "!**/*.spec.*",
36
35
  "!**/*.test.*",
37
36
  "README.md"
38
37
  ],
39
38
  "dependencies": {
40
- "@xyo-network/account-model": "~5.3.22",
41
- "@xyo-network/boundwitness-model": "~5.3.22",
42
- "@xyo-network/hash": "~5.3.22",
43
- "@xyo-network/payload": "~5.3.22",
44
- "@xyo-network/payload-builder": "~5.3.22",
45
- "@xyo-network/payload-model": "~5.3.22",
46
- "async-mutex": "~0.5.0"
39
+ "async-mutex": "~0.5.0",
40
+ "@xyo-network/account-model": "~5.3.25",
41
+ "@xyo-network/boundwitness-model": "~5.3.25",
42
+ "@xyo-network/payload": "~5.3.25",
43
+ "@xyo-network/hash": "~5.3.25",
44
+ "@xyo-network/payload-builder": "~5.3.25",
45
+ "@xyo-network/payload-model": "~5.3.25"
47
46
  },
48
47
  "devDependencies": {
49
48
  "@opentelemetry/api": "^1.9.1",
50
49
  "@types/node": "^25.5.0",
51
- "@xylabs/sdk-js": "^5.0.91",
52
- "@xylabs/ts-scripts-common": "~7.6.8",
53
- "@xylabs/ts-scripts-yarn3": "~7.6.8",
54
- "@xylabs/tsconfig": "~7.6.8",
55
- "@xylabs/vitest-extended": "~5.0.91",
56
- "@xyo-network/account": "~5.3.22",
57
- "@xyo-network/account-model": "~5.3.22",
58
- "@xyo-network/boundwitness-model": "~5.3.22",
59
- "@xyo-network/elliptic": "~5.3.22",
60
- "@xyo-network/hash": "~5.3.22",
61
- "@xyo-network/payload": "~5.3.22",
62
- "@xyo-network/payload-builder": "~5.3.22",
63
- "@xyo-network/payload-model": "~5.3.22",
64
- "@xyo-network/payload-utils": "~5.3.22",
65
- "@xyo-network/wallet": "~5.3.22",
50
+ "@xylabs/sdk-js": "^5.0.93",
51
+ "@xylabs/ts-scripts-common": "~7.6.16",
52
+ "@xylabs/ts-scripts-pnpm": "~7.6.16",
53
+ "@xylabs/tsconfig": "~7.6.16",
54
+ "@xylabs/vitest-extended": "~5.0.93",
66
55
  "acorn": "^8.16.0",
67
56
  "async-mutex": "~0.5.0",
68
57
  "axios": "^1.14.0",
69
- "cosmiconfig": "^9.0.1",
70
- "esbuild": "^0.27.4",
71
- "eslint": "^10.1.0",
58
+ "esbuild": "^0.28.0",
72
59
  "ethers": "^6.16.0",
73
- "rollup": "^4.60.1",
74
60
  "tslib": "^2.8.1",
75
61
  "typescript": "~5.9.3",
76
62
  "vite": "^8.0.3",
77
63
  "vitest": "~4.1.2",
78
- "zod": "^4.3.6"
64
+ "zod": "^4.3.6",
65
+ "@xyo-network/boundwitness-model": "~5.3.25",
66
+ "@xyo-network/account-model": "~5.3.25",
67
+ "@xyo-network/elliptic": "~5.3.25",
68
+ "@xyo-network/account": "~5.3.25",
69
+ "@xyo-network/payload": "~5.3.25",
70
+ "@xyo-network/hash": "~5.3.25",
71
+ "@xyo-network/payload-builder": "~5.3.25",
72
+ "@xyo-network/payload-model": "~5.3.25",
73
+ "@xyo-network/payload-utils": "~5.3.25",
74
+ "@xyo-network/wallet": "~5.3.25"
79
75
  },
80
76
  "peerDependencies": {
81
77
  "@xylabs/sdk-js": "^5",
@@ -86,4 +82,4 @@
86
82
  "publishConfig": {
87
83
  "access": "public"
88
84
  }
89
- }
85
+ }
@@ -1,257 +0,0 @@
1
- import type {
2
- Address, Hash, Hex,
3
- } from '@xylabs/sdk-js'
4
- import {
5
- asAddress, asHash, assertEx,
6
- hexFromArrayBuffer, toArrayBuffer,
7
- } from '@xylabs/sdk-js'
8
- import type { AccountInstance } from '@xyo-network/account-model'
9
- import type {
10
- BoundWitness,
11
- PossiblySigned,
12
- Signed,
13
- Unsigned,
14
- } from '@xyo-network/boundwitness-model'
15
- import { BoundWitnessSchema } from '@xyo-network/boundwitness-model'
16
- import {
17
- ObjectHasher, removeEmptyFields, sortFields,
18
- } from '@xyo-network/hash'
19
- import type { PayloadBuilderOptions } from '@xyo-network/payload-builder'
20
- import { omitSchema, PayloadBuilder } from '@xyo-network/payload-builder'
21
- import type {
22
- ModuleError, Payload, Schema,
23
- WithoutClientMeta,
24
- WithoutMeta,
25
- WithoutSchema,
26
- WithoutStorageMeta,
27
- } from '@xyo-network/payload-model'
28
- import { Mutex } from 'async-mutex'
29
-
30
- import { GeneratedBoundWitnessFields } from './GeneratedBoundWitnessFields.ts'
31
- import { uniqueAccounts } from './uniqueAccounts.ts'
32
-
33
- export class BoundWitnessBuilder<
34
- TBoundWitness extends BoundWitness = BoundWitness,
35
- TPayload extends Payload = Payload>
36
- extends PayloadBuilder<
37
- TBoundWitness,
38
- Promise<[PossiblySigned<TBoundWitness>, TPayload[], ModuleError[]]>
39
- > {
40
- private static readonly _buildMutex = new Mutex()
41
-
42
- private _accounts: AccountInstance[] = []
43
- private _errorHashes?: Hash[]
44
- private _errors: ModuleError[] = []
45
- private _payloadHashes?: Hash[]
46
- private _payloadSchemas?: Schema[]
47
- private _payloads: TPayload[] = []
48
-
49
- constructor(options?: Omit<PayloadBuilderOptions, 'schema'>) {
50
- super({ ...options, schema: BoundWitnessSchema })
51
- }
52
-
53
- protected get addresses(): Address[] {
54
- uniqueAccounts(this._accounts, true)
55
- return this._accounts.map(account => account.address.toLowerCase()) as Address[]
56
- }
57
-
58
- protected get payloadSchemas(): string[] {
59
- return (
60
- this._payloadSchemas
61
- ?? this._payloads.map((payload) => {
62
- return assertEx(payload.schema, () => this.missingSchemaMessage(payload))
63
- })
64
- )
65
- }
66
-
67
- protected get previousHashBytes(): (ArrayBufferLike | null)[] {
68
- return this._accounts.map(account => account.previousHashBytes ?? null)
69
- }
70
-
71
- protected get previousHashes(): (Hash | null)[] {
72
- return this._accounts.map(account => account.previousHash ?? null)
73
- }
74
-
75
- static addressIndex<T extends BoundWitness>(payload: T, address: Address) {
76
- const index = payload.addresses.indexOf(address)
77
- if (index === -1) {
78
- throw new Error('Invalid address')
79
- }
80
- return index
81
- }
82
-
83
- static previousHash<T extends BoundWitness>(boundWitness: T, address: Address) {
84
- return asHash(boundWitness.previous_hashes[this.addressIndex(boundWitness, address)])?.toLowerCase()
85
- }
86
-
87
- protected static async linkingFields<T extends BoundWitness>(
88
- accounts: AccountInstance[],
89
- payloads?: Payload[],
90
- ): Promise<Pick<T, GeneratedBoundWitnessFields>> {
91
- const addresses = accounts.map(account => asAddress(hexFromArrayBuffer(account.addressBytes, { prefix: false }), true))
92
- const previous_hashes = accounts.map(account => account.previousHash ?? null)
93
- const payload_hashes = payloads ? await BoundWitnessBuilder.hashes(payloads) : []
94
- const payload_schemas = payloads?.map(({ schema }) => schema) ?? []
95
- return {
96
- addresses, payload_hashes, payload_schemas, previous_hashes,
97
- }
98
- }
99
-
100
- protected static signature<T extends BoundWitness>(payload: T, address: Address) {
101
- return payload.$signatures[this.addressIndex(payload, address)]
102
- }
103
-
104
- protected static async signatures(accounts: AccountInstance[], dataHash: Hash): Promise<string[]> {
105
- const hashBytes = toArrayBuffer(dataHash)
106
- const previousHashesBytes = accounts?.map(account => account.previousHashBytes)
107
- return await Promise.all(accounts.map(async (account, index) => hexFromArrayBuffer((await account.sign(hashBytes, previousHashesBytes[index]))[0])))
108
- }
109
-
110
- private static validateGeneratedFields(fields: Pick<BoundWitness, GeneratedBoundWitnessFields>) {
111
- assertEx(fields.payload_hashes?.length === fields.payload_schemas?.length, () => 'Payload hash/schema mismatch')
112
- assertEx(!(fields.payload_hashes as (Hash | null)[]).includes(null), () => 'nulls found in hashes')
113
- assertEx(!(fields.payload_schemas as (Schema | null)[]).includes(null), () => 'nulls found in schemas')
114
- }
115
-
116
- override async build(): Promise<[Signed<TBoundWitness>, TPayload[], ModuleError[]]>
117
- override async build(sign: false): Promise<[Unsigned<TBoundWitness>, TPayload[], ModuleError[]]>
118
- override async build(sign: true): Promise<[Signed<TBoundWitness>, TPayload[], ModuleError[]]>
119
- override async build(sign: boolean = true): Promise<[PossiblySigned<TBoundWitness>, TPayload[], ModuleError[]]> {
120
- return await BoundWitnessBuilder._buildMutex.runExclusive(async () => {
121
- const dataHashableFields = await this.dataHashableFields()
122
- const $signatures = sign ? await this.sign() : this.addresses.map(() => null)
123
-
124
- const ret = {
125
- ...this._meta, ...dataHashableFields, ...this._fields, $signatures,
126
- } as Signed<TBoundWitness>
127
- return [
128
- ret,
129
- this._payloads,
130
- this._errors,
131
- ]
132
- })
133
- }
134
-
135
- async dataHash() {
136
- const dataHashableFields = await this.dataHashableFields()
137
- const hash = await ObjectHasher.hash(dataHashableFields)
138
- return hash
139
- }
140
-
141
- override async dataHashableFields(): Promise<WithoutMeta<TBoundWitness>> {
142
- const generatedFields: Pick<TBoundWitness, GeneratedBoundWitnessFields> = await this.generatedFields()
143
- BoundWitnessBuilder.validateGeneratedFields(generatedFields)
144
- const fields: WithoutSchema<TBoundWitness> = {
145
- ...PayloadBuilder.omitMeta(this._fields ?? {}),
146
- ...generatedFields,
147
- } as WithoutSchema<TBoundWitness>
148
- return await BoundWitnessBuilder.dataHashableFields<TBoundWitness>(this._schema, fields)
149
- }
150
-
151
- error(payload?: ModuleError) {
152
- assertEx(this._errorHashes === undefined, () => 'Can not set errors when hashes already set')
153
- if (payload) {
154
- this._errors.push(assertEx(sortFields(payload)))
155
- }
156
- return this
157
- }
158
-
159
- errors(errors?: (ModuleError | null)[]) {
160
- if (errors) {
161
- for (const error of errors) {
162
- if (error !== null) {
163
- this.error(error)
164
- }
165
- }
166
- }
167
- return this
168
- }
169
-
170
- override fields(fields: WithoutSchema<Omit<WithoutStorageMeta<WithoutClientMeta<TBoundWitness>>,
171
- 'addresses' | 'payload_hashes' | 'payload_schemas' | 'previous_hashes'>>): this {
172
- // clean it up just incase
173
- const clone = structuredClone(fields) as unknown as Partial<TBoundWitness>
174
- for (const field of GeneratedBoundWitnessFields) {
175
- delete clone[field]
176
- }
177
- // we need to do the cast here since ts seems to not like nested, yet same, generics
178
- this._fields
179
- = BoundWitnessBuilder.omitStorageMeta(
180
- BoundWitnessBuilder.omitClientMeta(
181
- omitSchema(removeEmptyFields(clone)),
182
- ),
183
- ) as unknown as WithoutStorageMeta<WithoutClientMeta<WithoutSchema<TBoundWitness>>>
184
- return this
185
- }
186
-
187
- hashes(hashes: Hash[], schema: Schema[]) {
188
- assertEx(this.payloads.length === 0, () => 'Can not set hashes when payloads already set')
189
- this._payloadHashes = hashes
190
- this._payloadSchemas = schema
191
- return this
192
- }
193
-
194
- payload(payload?: TPayload) {
195
- assertEx(this._payloadHashes === undefined, () => 'Can not set payloads when hashes already set')
196
- if (payload) {
197
- this._payloads.push(assertEx(sortFields<TPayload>(payload)))
198
- }
199
- return this
200
- }
201
-
202
- payloads(payloads?: (TPayload | null)[]) {
203
- if (payloads)
204
- for (const payload of payloads) {
205
- if (payload !== null) {
206
- this.payload(payload)
207
- }
208
- }
209
- return this
210
- }
211
-
212
- signer(account: AccountInstance) {
213
- uniqueAccounts([...this._accounts, account], true)
214
- this._accounts?.push(account)
215
- return this
216
- }
217
-
218
- signers(accounts: AccountInstance[]) {
219
- uniqueAccounts([...this._accounts, ...accounts], true)
220
- this._accounts?.push(...accounts)
221
- return this
222
- }
223
-
224
- sourceQuery(sourceQuery: Hash) {
225
- this._meta = {
226
- ...this._meta,
227
- $sourceQuery: sourceQuery,
228
- } as typeof this._meta
229
- return this
230
- }
231
-
232
- /** @deprecated use signer instead */
233
- witness(account: AccountInstance) {
234
- this._accounts?.push(account)
235
- return this
236
- }
237
-
238
- /** @deprecated use signers instead */
239
- witnesses(accounts: AccountInstance[]) {
240
- this._accounts?.push(...accounts)
241
- return this
242
- }
243
-
244
- protected async sign(): Promise<Hex[]> {
245
- uniqueAccounts(this._accounts, true)
246
- const hashBytes = toArrayBuffer(await this.dataHash())
247
- return await Promise.all(this._accounts.map(async account => hexFromArrayBuffer((await account.sign(hashBytes))[0])))
248
- }
249
-
250
- private async generatedFields(): Promise<Pick<TBoundWitness, GeneratedBoundWitnessFields>> {
251
- return await BoundWitnessBuilder.linkingFields(this._accounts, this._payloads)
252
- }
253
-
254
- private missingSchemaMessage(payload: Payload) {
255
- return `Builder: Missing Schema\n${JSON.stringify(payload, null, 2)}`
256
- }
257
- }
@@ -1,2 +0,0 @@
1
- export const GeneratedBoundWitnessFields = ['addresses', 'payload_hashes', 'payload_schemas', 'previous_hashes'] as const
2
- export type GeneratedBoundWitnessFields = typeof GeneratedBoundWitnessFields[number]
@@ -1,27 +0,0 @@
1
- import { assertEx } from '@xylabs/sdk-js'
2
- import type { QueryBoundWitness } from '@xyo-network/boundwitness-model'
3
- import { PayloadBuilder } from '@xyo-network/payload'
4
- import type { Query, WithoutMeta } from '@xyo-network/payload-model'
5
-
6
- import { BoundWitnessBuilder } from '../BoundWitnessBuilder.ts'
7
-
8
- export class QueryBoundWitnessBuilder<
9
- TBoundWitness extends QueryBoundWitness = QueryBoundWitness,
10
- TQuery extends Query = Query,
11
- > extends BoundWitnessBuilder<TBoundWitness> {
12
- private _query: TQuery | undefined
13
-
14
- override async dataHashableFields(): Promise<WithoutMeta<TBoundWitness>> {
15
- const fields = {
16
- ...(await super.dataHashableFields()),
17
- query: await PayloadBuilder.dataHash(assertEx(this._query, () => 'No Query Specified')),
18
- } as TBoundWitness
19
- return { ...fields } as WithoutMeta<TBoundWitness>
20
- }
21
-
22
- query<T extends TQuery>(query: T) {
23
- this.payload(query)
24
- this._query = query
25
- return this
26
- }
27
- }
@@ -1 +0,0 @@
1
- export * from './QueryBoundWitnessBuilder.ts'
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './BoundWitnessBuilder.ts'
2
- export * from './Query/index.ts'
@@ -1,7 +0,0 @@
1
- import type { AccountInstance } from '@xyo-network/account-model'
2
-
3
- import { uniqueAddresses } from './uniqueAddresses.ts'
4
-
5
- export const uniqueAccounts = (accounts: AccountInstance[], throwOnFalse = false) => {
6
- return uniqueAddresses(accounts.map(account => account.address), throwOnFalse)
7
- }
@@ -1,15 +0,0 @@
1
- import type { Address } from '@xylabs/sdk-js'
2
-
3
- export const uniqueAddresses = (addresses: Address[], throwOnFalse = false) => {
4
- const addressesSet = new Set<Address>()
5
- for (const address of addresses) {
6
- if (addressesSet.has(address)) {
7
- if (throwOnFalse) {
8
- throw new Error('Duplicate address')
9
- }
10
- return false
11
- }
12
- addressesSet.add(address)
13
- }
14
- return true
15
- }