-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomTransactionId.ts
More file actions
453 lines (397 loc) · 18.1 KB
/
customTransactionId.ts
File metadata and controls
453 lines (397 loc) · 18.1 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
Off-chain version of the logic for the `custom_transaction_id` as defined in
/lamport-cardano/lamport-validator/lib/custom_transaction_id.ak
*/
import { fromHex, toHex } from "npm:@blaze-cardano/core";
import { sha256 } from "./sha256.ts";
import {
assertEquals,
assertExists,
assertRejects,
assert,
} from "@std/assert";
import { Constr, Data, getInputIndices, TxSignBuilder, UTxO, LucidEvolution, CML, getAddressDetails, Credential, sortUTxOs, validatorToScriptHash, Validator, ScriptType} from "npm:@lucid-evolution/lucid@0.4.29";
import { Value, ValidityRange, ReferenceInputs, OutputReference, OutputReferenceList, HashBlake2b224Schema, ListExtraSignatories, Certificates, CredentialSchema, Credential as CredentialType , Output as OutputType, OutputList, Address} from "./datatypes/index.ts";
import { getInput } from "./utils.ts";
/*
https://github.com/leobel/janus-wallet/blob/main/frontend/janus/src/utils/hashing.ts#L169
*/
export type CustomTransactionId = Uint8Array
export class CustomTransactionIdBuilder {
private inputs: Uint8Array | undefined
private reference_inputs: Uint8Array | undefined
private outputs: Uint8Array | undefined
private fee: Uint8Array | undefined
private mint: Uint8Array | undefined
private certificates: Uint8Array | undefined
private withdrawals: Uint8Array | undefined
private validity_range: Uint8Array | undefined
private extra_signatories: Uint8Array | undefined
private redeemers: Uint8Array | undefined
private datums: Uint8Array | undefined
private votes: Uint8Array | undefined
private proposal_procedures: Uint8Array | undefined
private current_treasury_amount: Uint8Array | undefined
private treasury_donation: Uint8Array | undefined
constructor() {}
/*
parameters:
tx
lucid
additionalSigners
extraInputs --> use to inject inputs missing from your draft tx
*/
public static async customTransactionId(tx: TxSignBuilder, lucid: LucidEvolution, additionalSigners: string[] = [], extraInputs : UTxO[] = []) {
const txObj = tx.toJSON() as any
// console.log(txObj)
// console.log(`json tx outputs --> `, txObj.body.outputs)
const cmlTxBody = tx.toTransaction().body()
const utxosFromTx : UTxO[] = await lucid.utxosByOutRef(
txObj.body.inputs
.map((input : {transaction_id: string, index: number}) => ({
txHash: input.transaction_id,
outputIndex: input.index,
})))
return await new CustomTransactionIdBuilder()
.withMint(txObj.body.mint)
.withTreasuryDonation(txObj.body.treasury_donation)
.withCurrentTreasuryAmount(txObj.body.current_treasury_amount)
.withReferenceInputs(txObj.body.reference_inputs ?? [])
.withExtraSignatories(additionalSigners)
.withWithdrawals(txObj.body.withdrawals ?? {})
.withInputs([...utxosFromTx, ...extraInputs])
.withOutputs(txObj.body.outputs)
// .withVotes(txObj.body.voting_procedures ?? [])
// .withInputs([...(txObj.body.inputs ?? []), ...extraInputRefs])
// .withCertificates(txObj.body.certs ?? [])
.build()
}
/*
for now we will assume that the both bounds are specified
-- what if its not incorrectly formatted? its just the wrong data?
*/
withValidityRange(validity_interval_start: number, ttl: number) {
console.log(`%cvalidity_interval_start: ${validity_interval_start}`, "color: green")
console.log(`%cttl: ${ttl}`, "color: green")
const range = Data.to<ValidityRange>( {
lower_bound: {
bound_type: {
Finite: {
value: BigInt(validity_interval_start)
}
},
is_inclusive: true,
},
upper_bound: {
bound_type: {
Finite: {
value: BigInt(ttl)
}
},
is_inclusive: true,
}
}, ValidityRange,
// {canonical: true}
)
console.log(`%cValidity range bytes: ${range}`, "color: cyan")
this.validity_range = fromHex(range)
console.log(`%crange: ${range}`, "color: green")
console.log(`%cvalidity_range: ${this.validity_range}`, "color: green")
return this
}
// TODO: remove if only used once
private encodeInputList(inputs: any[]){
// const sorted = sortUTxOs();
const references = inputs.map((input: any) => ({transaction_id: input.transaction_id, output_index: BigInt(input.index)}))
// const references = sorted.map((input: any) => ({transaction_id: input.transaction_id, output_index: BigInt(input.index)}))
const encoded : string = Data.to(references, OutputReferenceList)
return fromHex(encoded)
}
/*
withInputs
@in list of objects with a transaction id (hex string) and an index
Given the utxo objects you need to construct an object to mirror aikens "Output" type
The output type is defined as:
```
Output {
address: Address,
value: Value,
datum: Datum,
reference_script: Option<ScriptHash>,
}
```
After a list of these object is constructed you need to serialize it using the mirror of builtin.serialise_data
This function is the off-chain mirror of the `with_inputs` function in the `custom_transaction_id.ak` file
*/
withInputs(inputs: UTxO[]) {
// console.log(`withInputs --> `, inputs)
const sorted = sortUTxOs(inputs, "Canonical")
.map((input : UTxO) => ({
transaction_id: input.txHash,
output_index: BigInt(input.outputIndex)
}))
const encoded : string = Data.to(sorted, OutputReferenceList)
// console.log(`Input bytes are ${encoded}`)
this.inputs = fromHex(encoded)
return this
}
// rework to use UTxOs
withReferenceInputs(reference_inputs: any[]) {
this.reference_inputs = this.encodeInputList(reference_inputs)
return this
}
/*
Aiken definition of output
Output {
address: Address,
value: Value,
datum: Datum,
reference_script: Option<ScriptHash>,
}
*/
withOutputs(outputs: any[]) {
// console.log(`Outputs --> `, outputs)
// const formated : any[] = outputs.map((output: any) => {
const encoded : any = outputs.map((output: any) => {
assert(1 === Object.keys(output).length, "Should only be one key on outputs root object")
const a = output[Object.keys(output)[0]];
// console.log("a is ", a)
const addressDetails = getAddressDetails(a.address)
// console.log(addressDetails)
const processCredential = (credential: any ) => {
if ("Key" === credential.type){
return {
VerificationKey: [credential.hash as string] as [string]
}
}
return {
Script: [credential.hash as string] as [string]
}
}
const processStakeCredential = (credential: any) => {
if ("Key" === credential.type){
return {
Inline: [
{
VerificationKey: [credential.hash as string] as [string]
}
] as [{ VerificationKey: [string]; } | { Script: [string]; }]
}
}
return {
Inline: [
{
Script: [credential.hash as string] as [string]
}
] as [{ VerificationKey: [string]; } | { Script: [string]; }]
}
}
const address = {
payment_credential: processCredential(addressDetails.paymentCredential),
stake_credential: addressDetails.stakeCredential ? processStakeCredential(addressDetails.stakeCredential) : null
}
// console.log(`STUB:withOutputs::outputs.map have address`)
const value : Map<string, Map<string, bigint>> = new Map<string, Map<string, bigint>>();
// add lovelace
const lovelace = new Map<string, bigint>()
lovelace.set("", BigInt(a.amount.coin))
value.set("", lovelace)
console.log("STUB: have set lovelace in value")
// add everything else
for (const policyId of Object.keys(a.amount.multiasset)) {
// console.log(`%cpolicy id of assets is ${policyId}`, "color: red")
const assets = new Map<string, bigint>();
const tokens : any = a.amount.multiasset[policyId];
for (const assetName of Object.keys(tokens)) {
console.log(`%cAsset name is ${assetName}`, "color: blue")
assets.set(assetName, BigInt(tokens[assetName]));
}
console.log("assets --> ", assets)
// console.log("assets --> ", JSON.stringify(assets, null, 2))
value.set(policyId, assets);
}
console.log("STUB:withOutputs --> have value")
console.log(value)
console.log(JSON.stringify(value, null, 2))
// add datum (convert to one of three constructors)
// start with InlineDatum because its all I ever use
// const datum = a.datum_option
const datum = a.datum_option ? {InlineDatum: a.datum_option.Datum.datum.bytes} : {NoDatum: "NoDatum"}
// add reference script
// get script hash / id
const reference_script = (() => {
if ([undefined, null].includes(a?.script_reference)) {
return null;
}
const key = Object.keys(a?.script_reference)[0]
const script = a?.script_reference[key].script
const validator : Validator = {
type: key as ScriptType,
script
}
const reference_script = a?.script_reference ?? null
// console.log("reference script ", reference_script)
const reference_script_hash = validatorToScriptHash(reference_script)
// console.log("reference script hash ", reference_script_hash)
return reference_script_hash
})()
const addressBytes = Data.to(address, Address)
const valueBytes = Data.to(value, Value, {canonical: true})
console.log("value bytes ", valueBytes)
// const datumBytes = Data.to(datum, Datum)
// const referenceScriptBytes = Data.to(reference_script, ScriptHash)
const bytes = addressBytes + valueBytes
return bytes
// const wholeOutput = {
// address,
// value,
// // datum,
// datum: { NoDatum: "NoDatum" as "NoDatum" },
// // reference_script,
// reference_script: null
// }
// const encodedOutput = Data.to<OutputType>(wholeOutput, OutputType)
// console.log(`Encoded output is ${encodedOutput}`)
// return encodedOutput
// return wholeOutput
})
// .reduce((acc : Uint8Array, curr : string) => {
// const element = fromHex(curr)
// const bytes = new Uint8Array(acc.length + element.length)
// bytes.set(acc, 0)
// bytes.set(element, acc.length)
// return bytes
// }, new Uint8Array())
.reduce((acc, curr) => acc + curr, "")
// const encoded = Data.to<OutputList>(formated, OutputList)
console.log(`Encoded outputs: ${encoded}`)
// TODO: process outputs before adding them to the builder
// this.outputs = new Uint8Array()
// show as hex
this.outputs = fromHex(encoded)
return this
}
withFee(fee: number) {
const serializedFee = Data.to(new Constr(0, [BigInt(fee)]))
this.fee = fromHex(serializedFee)
return this
}
withTreasuryDonation(treasury_donation: number | undefined) {
if (undefined === treasury_donation) {
console.log("%ctreasury donation is undefined, giving None", "color: purple")
this.treasury_donation = fromHex(Data.to(new Constr(1, [])))
return this
}
console.log(`treasury donation is ${treasury_donation}`)
const serializedTreasuryDonation = Data.to(new Constr(0, [BigInt(treasury_donation)]))
this.treasury_donation = fromHex(serializedTreasuryDonation)
return this
}
withMint(mint: any) {
const mintObj = Object.keys(mint)
.reduce((acc, key) => {
acc.set(key, new Map(Object.entries(mint[key]).map(([k, v] : [string, any]) => [k, BigInt(v)])))
return acc
}, new Map<string, Map<string, bigint>>())
const preimage = Data.to<Value>(mintObj, Value, {canonical: true})
this.mint = fromHex(preimage)
return this
}
withCurrentTreasuryAmount(current_treasury_amount: number | undefined) {
if (undefined === current_treasury_amount) {
console.log("%ccurrent treasury amount is undefined, giving None", "color: purple")
this.current_treasury_amount = fromHex(Data.to(new Constr(1, [])))
return this
}
const serializedCurrentTreasuryAmount = Data.to(new Constr(0, [BigInt(current_treasury_amount)]))
this.current_treasury_amount = fromHex(serializedCurrentTreasuryAmount)
return this
}
withExtraSignatories(additionalSigners: string[]) {
const extra_signatories = Data.to(additionalSigners, ListExtraSignatories)
this.extra_signatories = fromHex(extra_signatories)
return this
}
withCertificates(certificates: any[]) {
console.log("Certificates: ", certificates)
assertExists(certificates, "Certificates must be defined in the build step")
const formated = certificates.map((cert: any) => {
if (Object.keys(cert).includes("RegDrepCert")) {
return {
RegisterCredential: {
delegate_representative: {
VerificationKey: cert.RegDrepCert.drep_credential.PubKey.hash
},
deposit: cert.RegDrepCert.deposit,
}
}
}
return cert
}).filter((cert: any) => cert !== null)
console.log("Formated: ", formated)
const encoded = Data.to(formated, Certificates)
this.certificates = fromHex(encoded)
return this
}
withWithdrawals(withdrawals: any) {
// going to change how I do this on chain this time
const bytes = Object.keys(withdrawals)
.map((key: string) => {
const amount = BigInt(withdrawals[key])
console.log("Amount: ", amount)
const credential : Credential | undefined = getAddressDetails(key).stakeCredential
console.log("Credential: ", credential)
assertExists(credential, "Stake credential must exist")
const credential_bytes = Data.to({
VerificationKey: [credential.hash]
}, CredentialType)
console.log("Credential bytes: ", credential_bytes)
const amount_bytes = Data.to(amount)
const entry = new Uint8Array(fromHex(credential_bytes).length + fromHex(amount_bytes).length)
entry.set(fromHex(credential_bytes), 0)
entry.set(fromHex(amount_bytes), fromHex(credential_bytes).length)
return entry
})
.reduce((acc, element) => {
const new_acc = new Uint8Array(acc.length + element.length)
new_acc.set(acc, 0)
new_acc.set(element, acc.length)
return new_acc
}, new Uint8Array())
this.withdrawals = bytes
return this
}
withVotes(votes: any[]) {
console.log("Votes: ", votes)
this.votes = fromHex(Data.to(votes))
return this
}
async build() : Promise<CustomTransactionId> {
// todo: actually serialize the transaction builder
assertExists(this.mint, "Mint must be defined in the build step")
assertExists(this.treasury_donation, "Treasury donation must be defined in the build step")
assertExists(this.current_treasury_amount, "Current treasury amount must be defined in the build step")
assertExists(this.reference_inputs, "Reference inputs must be defined in the build step")
assertExists(this.extra_signatories, "Extra signatories must be defined in the build step")
assertExists(this.withdrawals, "Withdrawals must be defined in the build step")
assertExists(this.inputs, "Inputs must be defined in the build step")
assertExists(this.outputs, "Outputs must exist when building")
const components : Uint8Array[] = [
this.mint,
this.treasury_donation,
this.current_treasury_amount,
this.reference_inputs,
this.extra_signatories,
this.withdrawals,
this.inputs,
this.outputs,
]
const combinedLengthUpTo = (n: number) => components.slice(0, n).reduce((acc : number, el: Uint8Array) => acc + el.length, 0 )
const fullLength = combinedLengthUpTo(components.length)
const blob = new Uint8Array(fullLength)
for (let i = 0; i < components.length; i++) {
blob.set(components[i], i === 0 ? 0 : combinedLengthUpTo(i))
}
return await sha256(blob)
}
}