@xyo-network/xl1-wrappers 1.7.15

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 @@
1
+ export * from './createSignatureWrappers.ts'
@@ -0,0 +1,172 @@
1
+ import type { Hash } from '@xylabs/hex'
2
+ import { hexToBigInt } from '@xylabs/hex'
3
+ import { isDefined } from '@xylabs/typeof'
4
+ import { validateTransaction } from '@xyo-network/chain-validation'
5
+ import { PayloadBuilder } from '@xyo-network/payload-builder'
6
+ import type {
7
+ Payload, Schema, WithHashStorageMeta,
8
+ WithStorageMeta,
9
+ } from '@xyo-network/payload-model'
10
+ import type {
11
+ AllowedBlockPayload, HydratedTransaction, Transfer,
12
+ } from '@xyo-network/xl1-protocol'
13
+ import { isTransfer, XYO_ZERO_ADDRESS } from '@xyo-network/xl1-protocol'
14
+ import type {
15
+ HydratedTransactionInstance, SignatureInstance,
16
+ TransactionFeesInstance,
17
+ } from '@xyo-network/xl1-protocol-sdk'
18
+ import {
19
+ transactionRequiredGas, tryExtractElevatedHashes, tryExtractElevatedHashesFromScript,
20
+ } from '@xyo-network/xl1-protocol-sdk'
21
+
22
+ import { FeesWrapper } from '../Fees.ts'
23
+ import { createSignatureWrappers } from '../lib/index.ts'
24
+
25
+ const sumTransfers = (payload: Transfer) => {
26
+ let total = 0n
27
+ for (let i of Object.values(payload.transfers)) {
28
+ total += hexToBigInt(i ?? '00')
29
+ }
30
+ return total
31
+ }
32
+
33
+ export class HydratedTransactionWrapper<T extends HydratedTransaction> implements HydratedTransactionInstance<[T[0],
34
+ T[1][number][]]> {
35
+ data: T
36
+ fees: TransactionFeesInstance
37
+
38
+ protected payloadsCache: WithStorageMeta<Payload>[] = []
39
+
40
+ private _signatureCache: SignatureInstance[] = []
41
+
42
+ protected constructor(data: T) {
43
+ this.data = data
44
+ this.fees = new FeesWrapper(
45
+ this.boundWitness.fees,
46
+ )
47
+ }
48
+
49
+ get boundWitness(): T[0] {
50
+ return this.data[0]
51
+ }
52
+
53
+ get elevatedPayloadCount(): number {
54
+ const { script } = this.data[0]
55
+ return script ? tryExtractElevatedHashesFromScript(script).length : 0
56
+ }
57
+
58
+ get elevatedPayloads(): WithStorageMeta<AllowedBlockPayload>[] {
59
+ return tryExtractElevatedHashes(this.data)
60
+ }
61
+
62
+ get externalPayloads(): Record<Hash, Schema | Payload> {
63
+ const allPayloads = this.payloads
64
+ const external: Record<Hash, Schema | Payload> = {}
65
+ for (let i = 0; i < this.boundWitness.payload_hashes.length; i++) {
66
+ const payloadHash = this.boundWitness.payload_hashes[i]
67
+ const payload = allPayloads.find(p => p._hash === payloadHash)
68
+ external[payloadHash] = isDefined(payload) ? payload : this.boundWitness.payload_schemas[i]
69
+ }
70
+ return external
71
+ }
72
+
73
+ get from() {
74
+ return this.data[0].from
75
+ }
76
+
77
+ get payloadCount(): number {
78
+ return this.payloadsCache.length
79
+ }
80
+
81
+ get payloads(): WithHashStorageMeta<WithStorageMeta<T[1][number]>>[] {
82
+ return [...this.payloadsCache]
83
+ }
84
+
85
+ get privateExternalPayloads(): Record<Hash, Schema> {
86
+ const allPayloads = this.payloads
87
+ const missing: Record<Hash, Schema> = {}
88
+ for (let i = 0; i < this.boundWitness.payload_hashes.length; i++) {
89
+ const payloadHash = this.boundWitness.payload_hashes[i]
90
+ if (!allPayloads.some(p => p._hash === payloadHash)) {
91
+ missing[payloadHash] = this.boundWitness.payload_schemas[i]
92
+ }
93
+ }
94
+ return missing
95
+ }
96
+
97
+ // list all the payloads that are included in the transaction and are not elevated and found in the hydration
98
+ get publicExternalPayloads(): Payload[] {
99
+ const allPayloads = this.payloads
100
+ const elevatedPayloads = this.elevatedPayloads
101
+ return allPayloads.filter(p => !elevatedPayloads.some(ep => ep._hash === p._hash))
102
+ }
103
+
104
+ get signatureCount(): number {
105
+ return this._signatureCache.length
106
+ }
107
+
108
+ get signatures(): SignatureInstance[] {
109
+ return [...this._signatureCache]
110
+ }
111
+
112
+ static async parse<T extends HydratedTransaction>(transaction: T, validate = false): Promise<HydratedTransactionInstance<[T[0],
113
+ T[1][number][]]>> {
114
+ const wrapper = new HydratedTransactionWrapper<T>(
115
+ transaction,
116
+ )
117
+ const parsed = await wrapper.parse()
118
+ if (validate) {
119
+ const errors = await wrapper.validate()
120
+ if (errors.length > 0) {
121
+ throw new Error(`Block validation failed: ${errors.join(', ')}`)
122
+ }
123
+ }
124
+ return parsed
125
+ }
126
+
127
+ elevatedPayload(index: number): WithHashStorageMeta<AllowedBlockPayload & T[1][number]> | undefined {
128
+ return this.elevatedPayloads.at(index)
129
+ }
130
+
131
+ gasRequired(): bigint {
132
+ return transactionRequiredGas(this.data)
133
+ }
134
+
135
+ payload(index: number): WithStorageMeta<Payload> | undefined {
136
+ return this.payloads.at(index)
137
+ }
138
+
139
+ reward(): bigint {
140
+ return this.payloadsCache.reduce((acc: bigint, payload) => acc + (
141
+ isTransfer(payload)
142
+ ? payload.from === XYO_ZERO_ADDRESS
143
+ ? sumTransfers(payload)
144
+ : 0n
145
+ : 0n), 0n)
146
+ }
147
+
148
+ signature(index: number): SignatureInstance | undefined {
149
+ return this._signatureCache[index]
150
+ }
151
+
152
+ async validate(): Promise<Error[]> {
153
+ const errors: Error[] = []
154
+ errors.push(
155
+ ...(await Promise.all(this._signatureCache.map(signature => signature.validate()))).flat(),
156
+ ...(await validateTransaction(this.data)),
157
+ )
158
+ return errors
159
+ }
160
+
161
+ protected async parse(validate = false): Promise<HydratedTransactionInstance<[WithHashStorageMeta<T[0]>, WithHashStorageMeta<T[1][number]>[]]>> {
162
+ const transactionPayloads = await PayloadBuilder.addStorageMeta(this.data[1])
163
+ this._signatureCache = await createSignatureWrappers(this.data[0])
164
+ for (const payload of transactionPayloads) {
165
+ this.payloadsCache.push(payload)
166
+ }
167
+ if (validate) {
168
+ await this.validate()
169
+ }
170
+ return this
171
+ }
172
+ }
@@ -0,0 +1 @@
1
+ export * from './HydratedTransaction.ts'