-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
282 lines (233 loc) · 6.41 KB
/
index.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* Import modules. */
import { OP } from '@nexajs/script'
import { binToHex } from '@nexajs/utils'
/* Import (local) modules. */
import _createTransaction from './src/createTransaction.js'
/* Export (local) modules. */
export const createTransaction = _createTransaction
/* Initialize default script bytecode. */
const SCRIPT_TEMPLATE_1 = new Uint8Array([
OP.FROMALTSTACK,
OP.CHECKSIGVERIFY,
])
/* Set constants. */
const MAXINT = 0xffffffff
const DEFAULT_SEQNUMBER = MAXINT - 1 // NOTE: Enables nLocktime
// TODO Add support for ALL signature types.
// (source: https://spec.nexa.org/nexa/sighashtype.md)
const SIGHASH_ALL = 0x0
/**
* Transaction Class
*
* Manage the construction of a new or existing Transaction; as well as
* handle the unlocking (by use of redeem scripts) and authorize the
* transaction for on-chain broadcast.
*/
export class Transaction {
constructor(_params) {
/* Initialize flags. */
this._isSigned = false
/* Initialize data. */
this._data = []
/* Initialize inputs. */
this._inputs = []
/* Initialize outputs. */
this._outputs = []
/* Initialize scripts. */
this._scripts = []
/* Initialize raw (hex) output. */
this._raw = null
/* Validate fee rate. */
if (_params?.feeRate) {
this._feeRate = _params.feeRate
} else {
// NOTE: This is the new Qt default (1.0/byte is for consolidation).
this._feeRate = 2.0 // 2.0 satoshis per byte
}
/* Validate lock time. */
// NOTE: A Unix timestamp or block number. (4 bytes)
// It is very common for this value to be set to the
// current block height.
if (_params?.lockTime) {
this._lockTime = _params.lockTime
} else {
this._lockTime = 0
}
/* Validate sequence (number). */
if (_params?.sequence) {
this._sequence = _params.sequence
} else {
this._sequence = DEFAULT_SEQNUMBER
}
/* Validate locking (script). */
if (_params?.locking) {
this._locking = _params.locking
} else {
this._locking = SCRIPT_TEMPLATE_1
}
/* Validate unlocking (script). */
if (_params?.unlocking === null || _params?.unlocking === false) {
// NOTE: `null` disables "automatic" transaction signing.
this._unlocking = null
} else {
// NOTE: Expect `undefined` for "standard" pubkey+sig procedure.
this._unlocking = _params?.unlocking
}
/* Validate hash type. */
if (_params?.hashType) {
this._hashType = _params.hashType
} else {
this._hashType = SIGHASH_ALL
}
}
test() {
return 'Transaction (Instance) is working!'
}
static test() {
return 'Transaction (Static) is working!'
}
get data() {
return this._data
}
get hex() {
/* Return (hex-formatted) raw transaction data. */
return binToHex(this.raw)
}
get inputs() {
return this._inputs
}
get isSigned() {
return this._isSigned
}
get lockTime() {
return this._lockTime
}
get outputs() {
return this._outputs
}
get scripts() {
return this._scripts
}
get json() {
/* Validate raw transaction data. */
if (!this._raw) {
return {}
}
return {
hash: this._raw,
tbd: 'TODO',
}
}
get raw() {
/* Validate raw transaction data. */
if (!this._raw) {
return ''
}
/* Return raw transaction data. */
return this._raw
}
get locking() {
return this._locking
}
get sequence() {
return this._sequence
}
get unlocking() {
return this._unlocking
}
/***************************************************************************
* BEGIN METHODS
*/
addData() {
// TODO
}
addInput({
outpoint,
satoshis,
sequence,
locking,
unlocking,
hashType,
}) {
/* Validate hash type. */
if (!hashType) {
hashType = SIGHASH_ALL
}
// TODO Validate EACH param.
this._inputs.push({
outpoint,
satoshis,
sequence,
locking,
unlocking,
hashType,
})
}
addOutput({
address,
satoshis,
tokenid,
tokens,
}) {
if (satoshis === null || typeof satoshis === 'undefined') {
// TODO Validate output.
this._outputs.push({
data: address,
})
} else {
// TODO Validate output.
this._outputs.push({
address,
satoshis,
tokenid,
tokens,
})
}
}
addScript() {
// TODO
}
/**
* Sign
*
* Sign a hash of the serialized transaction (using Schnorr).
*/
async sign(_locksmith) {
/* Initialize unspent holder. */
const unspents = []
/* Handle inputs. */
this._inputs.forEach(_input => {
unspents.push({
outpoint: _input.outpoint,
satoshis: _input.satoshis,
sequence: _input.sequence || this.sequence,
locking: _input.locking,
unlocking: _input.unlocking,
hashType: _input.hashType,
})
})
/* Generate raw transaction. */
this._raw = await createTransaction(
_locksmith,
unspents, // FIXME Why not use `this.inputs`??
this.outputs,
this.lockTime,
this.locking,
this.unlocking,
).catch(err => console.error(err))
/* Set flag. */
this._isSigned = true
}
}
/* Initialize (globalThis) Nexa class. */
const Nexa = {}
/* Initialize Transaction class. */
Nexa.Transaction = Transaction
/* Initialize Transaction modules. */
Nexa.createTransaction = createTransaction
/* Export Nexa to globalThis. */
// NOTE: We merge to avoid conflict with other libraries.
globalThis.Nexa = {
...globalThis.Nexa, // preserve Nexa object
...Nexa, // extend Nexa object
}