-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TxOutRef.ts
143 lines (124 loc) · 3.8 KB
/
TxOutRef.ts
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
import { ByteString } from "@harmoniclabs/bytestring";
import { ToCbor, CborString, Cbor, CborObj, CborArray, CborUInt, CanBeCborString, forceCborString } from "@harmoniclabs/cbor";
import { forceBigUInt } from "@harmoniclabs/cbor/dist/utils/ints";
import { isObject, hasOwn, defineReadOnlyProperty } from "@harmoniclabs/obj-utils";
import { ToData, DataConstr, DataB, DataI } from "@harmoniclabs/plutus-data";
import { Hash32 } from "../../../hashes";
import { BasePlutsError } from "../../../utils/BasePlutsError";
import { InvalidCborFormatError } from "../../../utils/InvalidCborFormatError";
import { ToJson } from "../../../utils/ToJson";
import { assert } from "../../../utils/assert";
export type TxOutRefStr = `${string}#${number}`;
export interface ITxOutRef {
id: string | Hash32
index: number
}
export function isITxOutRef( stuff: any ): stuff is ITxOutRef
{
return (
isObject( stuff ) &&
hasOwn( stuff, "id" ) && (
(typeof stuff.id === "string" && ByteString.isValidHexValue( stuff.id ) && (stuff.id.length === 64)) ||
(stuff.id instanceof Hash32)
) &&
hasOwn( stuff, "index" ) && (
typeof stuff.index === "number" &&
stuff.index === Math.round( Math.abs( stuff.index ) )
)
)
}
export function ITxOutRefToStr( iRef: ITxOutRef ): TxOutRefStr
{
if( !isITxOutRef( iRef ) )
throw new BasePlutsError(
"'ITxOutRefToStr' works on 'ITxOutRef' like objects"
);
return `${iRef.id.toString()}#${iRef.index.toString()}` as any;
}
export type UTxORefJson = {
id: string;
index: number;
};
export class TxOutRef
implements ITxOutRef, ToData, ToCbor, ToJson
{
readonly id!: Hash32
readonly index!: number
constructor({ id, index }: ITxOutRef)
{
assert(
(typeof id === "string" && ByteString.isValidHexValue( id ) && (id.length === 64)) ||
(id instanceof Hash32),
"tx output id (tx hash) invalid while constructing a 'UTxO'"
);
defineReadOnlyProperty(
this,
"id",
id instanceof Hash32 ? id : new Hash32( id )
);
defineReadOnlyProperty(
this,
"index",
Number( forceBigUInt( index ) )
);
}
toString(): TxOutRefStr
{
return `${this.id.toString()}#${this.index.toString()}` as any;
}
toData(): DataConstr
{
return new DataConstr(
0, // PTxOutRef only constructor
[
new DataConstr(
0, // PTxId only constructor
[ new DataB( this.id.toBuffer() ) ]
),
new DataI( this.index )
]
)
}
toCbor(): CborString
{
return Cbor.encode( this.toCborObj() );
}
toCborObj(): CborObj
{
return new CborArray([
this.id.toCborObj(),
new CborUInt( this.index )
])
}
static fromCbor( cStr: CanBeCborString ): TxOutRef
{
return TxOutRef.fromCborObj( Cbor.parse( forceCborString( cStr ) ) );
}
static fromCborObj( cObj: CborObj ): TxOutRef
{
if(!(cObj instanceof CborArray ))
throw new InvalidCborFormatError("TxOutRef");
const [ _id, _index ] = cObj.array;
const idRes = Hash32.fromCborObj( _id );
if(!(_index instanceof CborUInt))
throw new InvalidCborFormatError("TxOutRef");
return new TxOutRef({
id: idRes,
index: Number( _index.num )
});
}
toJson(): UTxORefJson
{
return {
id: this.id.toString(),
index: this.index
};
}
static get fake(): TxOutRef
{
return new TxOutRef({
id: "ff".repeat(32),
index: 0
});
}
}