-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.js
146 lines (126 loc) · 3.39 KB
/
types.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
import { blockchain } from "@ckb-lumos/base";
import { bytes, molecule, number } from "@ckb-lumos/codec";
const { Uint32LE, Uint32 } = number;
const { option, table, vector, union } = molecule;
const { WitnessArgs } = blockchain;
export { blockchain };
export const Uint32Opt = option(Uint32);
export const String = blockchain.Bytes;
export const Action = table(
{
scriptInfoHash: blockchain.Byte32,
scriptHash: blockchain.Byte32,
data: blockchain.Bytes,
},
["scriptInfoHash", "scriptHash", "data"],
);
export const ActionVec = vector(Action);
export const Message = table({ actions: ActionVec }, ["actions"]);
export const ScriptInfo = table(
{
name: String,
url: String,
scriptHash: blockchain.Byte32,
schema: String,
messageType: String,
},
["name", "url", "scriptHash", "schema", "messageType"],
);
export const ScriptInfoVec = vector(ScriptInfo);
export const ResolvedInputs = table(
{
outputs: blockchain.CellOutputVec,
outputsData: blockchain.BytesVec,
},
["outputs", "outputsData"],
);
export const BuildingPacketV1 = table(
{
message: Message,
payload: blockchain.Transaction,
resolvedInputs: ResolvedInputs,
changeOutput: Uint32Opt,
scriptInfos: ScriptInfoVec,
lockActions: ActionVec,
},
[
"message",
"payload",
"resolvedInputs",
"changeOutput",
"scriptInfos",
"lockActions",
],
);
export const BuildingPacket = union({ BuildingPacketV1 }, ["BuildingPacketV1"]);
export const SighashAll = table({ seal: blockchain.Bytes, message: Message }, [
"seal",
"message",
]);
export const SighashAllOnly = table({ seal: blockchain.Bytes }, ["seal"]);
export const OtxStart = table(
{
startInputCell: Uint32,
startOutputCell: Uint32,
startCellDeps: Uint32,
startHeaderDeps: Uint32,
},
["startInputCell", "startOutputCell", "startCellDeps", "startHeaderDeps"],
);
export const Otx = table(
{
lock: blockchain.Bytes,
inputCells: Uint32,
outputCells: Uint32,
cellDeps: Uint32,
headerDeps: Uint32,
message: Message,
},
["lock", "inputCells", "outputCells", "cellDeps", "headerDeps", "message"],
);
export const WitnessLayoutFieldTags = {
SighashAll: 4278190081,
SighashAllOnly: 4278190082,
Otx: 4278190083,
OtxStart: 4278190084,
};
export const MinWitnessLayoutFieldTag = WitnessLayoutFieldTags.SighashAll;
export const WitnessLayout = union(
{ SighashAll, SighashAllOnly, Otx, OtxStart },
WitnessLayoutFieldTags,
);
export function parseWitnessType(witness) {
const buf = bytes.bytify(witness ?? []);
if (buf.length > 4) {
const typeIndex = Uint32LE.unpack(buf.slice(0, 4));
if (typeIndex >= MinWitnessLayoutFieldTag) {
for (const [name, index] of Object.entries(WitnessLayoutFieldTags)) {
if (index === typeIndex) {
return name;
}
}
} else {
return "WitnessArgs";
}
}
throw new Error("Unknown witness format");
}
export function tryParseWitness(witness) {
const buf = bytes.bytify(witness ?? []);
if (buf.length > 4) {
const typeIndex = Uint32LE.unpack(buf.slice(0, 4));
try {
if (typeIndex >= MinWitnessLayoutFieldTag) {
return WitnessLayout.unpack(buf);
} else {
return {
type: "WitnessArgs",
value: WitnessArgs.unpack(buf),
};
}
} catch (_err) {
// passthrough
}
}
throw new Error("Unknown witness format");
}