/
AssetToken.ts
156 lines (138 loc) · 5.06 KB
/
AssetToken.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
144
145
146
147
148
149
150
151
152
153
154
155
156
import { bytesEqual, modNumber, toHex } from 'chia-bls';
import { Coin, CoinSpend, formatHex, sanitizeHex, toCoinId } from 'chia-rpc';
import { Program } from 'clvm-lib';
import { puzzles } from '../../utils/puzzles';
import { SpendableAssetCoin } from '../coins/SpendableAssetCoin';
export class AssetToken<T extends Program> extends Program {
public readonly assetId: Uint8Array;
public readonly innerPuzzle: T;
constructor(assetId: Uint8Array, innerPuzzle: T) {
super(
puzzles.cat.curry([
Program.fromBytes(puzzles.cat.hash()),
Program.fromBytes(assetId),
innerPuzzle,
]).value
);
this.assetId = assetId;
this.innerPuzzle = innerPuzzle;
}
public static calculateIssuePayment(
tail: Program,
solution: Program,
innerPuzzleHash: Uint8Array,
amount: number
): Program {
return Program.cons(
Program.fromInt(1),
Program.fromList([
Program.fromList([
Program.fromInt(51),
Program.fromInt(0),
Program.fromInt(-113),
tail,
solution,
]),
Program.fromList([
Program.fromInt(51),
Program.fromBytes(innerPuzzleHash),
Program.fromInt(amount),
Program.fromList([Program.fromBytes(innerPuzzleHash)]),
]),
])
);
}
public static calculatePuzzle(
tail: Program,
solution: Program,
innerPuzzleHash: Uint8Array,
amount: number
): AssetToken<Program> {
return puzzles.cat.curry([
Program.fromBytes(puzzles.cat.hash()),
Program.fromBytes(tail.hash()),
this.calculateIssuePayment(tail, solution, innerPuzzleHash, amount),
]) as AssetToken<Program>;
}
public static issue(
originCoinSpend: CoinSpend,
tail: Program,
solution: Program,
innerPuzzleHash: Uint8Array,
amount: number
): CoinSpend {
const payToPuzzle = AssetToken.calculateIssuePayment(
tail,
solution,
innerPuzzleHash,
amount
);
const catPuzzle = AssetToken.calculatePuzzle(
tail,
solution,
innerPuzzleHash,
amount
);
const eveCoin: Coin = {
parent_coin_info: formatHex(toHex(toCoinId(originCoinSpend.coin))),
puzzle_hash: formatHex(catPuzzle.hashHex()),
amount,
};
const spendableEve = new SpendableAssetCoin(
originCoinSpend,
eveCoin,
payToPuzzle,
Program.nil,
undefined,
tail.hash()
);
return AssetToken.spend([spendableEve])[0];
}
public static spend(
spendableAssetCoins: SpendableAssetCoin[]
): Array<CoinSpend> {
if (!spendableAssetCoins.length)
throw new Error('Missing spendable asset coin.');
const assetId = spendableAssetCoins[0].assetId;
for (const item of spendableAssetCoins.slice(1)) {
if (!bytesEqual(item.assetId, assetId))
throw new Error('Mixed asset ids in spend.');
}
const deltas = SpendableAssetCoin.calculateDeltas(spendableAssetCoins);
const subtotals = SpendableAssetCoin.calculateSubtotals(deltas);
return spendableAssetCoins.map((spendableAssetCoin, i) => {
const previous = modNumber(i - 1, spendableAssetCoins.length);
const next = modNumber(i + 1, spendableAssetCoins.length);
const previousCoin = spendableAssetCoins[previous];
const nextCoin = spendableAssetCoins[next];
const solution = Program.fromList([
spendableAssetCoin.innerSolution,
spendableAssetCoin.lineageProof,
Program.fromBytes(toCoinId(previousCoin.coin)),
Program.fromList([
Program.fromHex(
sanitizeHex(spendableAssetCoin.coin.parent_coin_info)
),
Program.fromHex(
sanitizeHex(spendableAssetCoin.coin.puzzle_hash)
),
Program.fromInt(spendableAssetCoin.coin.amount),
]),
Program.fromList([
Program.fromHex(
sanitizeHex(nextCoin.coin.parent_coin_info)
),
Program.fromBytes(nextCoin.innerPuzzle.hash()),
Program.fromInt(nextCoin.coin.amount),
]),
Program.fromInt(subtotals[i]),
Program.fromInt(spendableAssetCoin.extraDelta),
]);
return {
coin: spendableAssetCoin.coin,
puzzle_reveal: spendableAssetCoin.puzzle.serializeHex(),
solution: solution.serializeHex(),
};
});
}
}