forked from sCrypt-Inc/boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
improvedTokenUtxo.scrypt
291 lines (219 loc) · 12.9 KB
/
improvedTokenUtxo.scrypt
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
import "util.scrypt";
/**
* A improved token protocol based on UTXO model
*/
contract Token {
PubKey issuerPubKey;
int maxSupply;
constructor(PubKey issuerPubKey, int maxSupply) {
this.issuerPubKey = issuerPubKey;
this.maxSupply = maxSupply;
}
function notifySatoshiBytes(): bytes {
return num2bin(Util.NotifySatoshi, Util.OutputValueLen);
}
public function initiate(Sig issuerSig, Ripemd160 ownerPKH, Ripemd160 changePKH, int changeSatoshi, bytes txPreimage, int outValue) {
// this ensures the preimage is for the current tx
SigHashType sigHashType = SigHash.ALL | SigHash.FORKID;
//SigHashType sigHashType = SigHash.ANYONECANPAY | SigHash.ALL | SigHash.FORKID;
require(Util.checkPreimageSigHashType(txPreimage, sigHashType));
// authorize
require(checkSig(issuerSig, this.issuerPubKey));
//outpoint (32-byte hash + 4-byte little endian)
bytes prevOutpoint = Util.outpoint(txPreimage);
bytes contractId = prevOutpoint[ : 32];
// read genesis locking script:
// codePart + OP_RETURN + name(64bytes) + symbol(16bytes) + issuer(64bytes) + rule(1byte) + decimals(1byte) + initialSupply(8bytes) = 154bytes
bytes lockingScript = Util.scriptCode(txPreimage);
int scriptLen = len(lockingScript);
int supplyStart = scriptLen - 8;
int decimalsStart = supplyStart - 1;
int ruleStart = decimalsStart - 1;
int issuerStart = ruleStart - 64;
int symbolStart = issuerStart - 16;
int nameStart = symbolStart - 64;
bytes initialSupply = lockingScript[supplyStart : ];
int supply = unpack(initialSupply) + 1;
bytes lockingScriptBody = lockingScript[ : supplyStart];
bytes valueBytes = num2bin(outValue, Util.OutputValueLen);
// write Token UTXO locking script
// codePart + OP_RETURN + contractId(32bytes) + prevOutpoint(36bytes) + ownerPkh(20bytes) + tokenAmount(8bytes) = 96bytes
bytes tokenData = contractId + initialSupply;
//contractId + prevOutpoint + ownerPKH + initialSupply;
bytes tokenLockingScript = lockingScriptBody + tokenData;
bytes output0 = valueBytes + Util.writeVarint(tokenLockingScript);
// // write Baton UTXO locking script
// // codePart + OP_RETURN + contractId(32bytes) + prevOutpoint(36bytes) + totalSupply(8bytes) = 76bytes
// bytes batonData = contractId + prevOutpoint + initialSupply;
// //bytes batonData = initialSupply;
// bytes batonLockingScript = lockingScriptBody + batonData;
// bytes output1 = valueBytes + Util.writeVarint(batonLockingScript);
// // Expect the notify output
// bytes notifyScript = Util.buildPublicKeyHashScript(ownerPKH);
// bytes output2 = this.notifySatoshiBytes() + Util.writeVarint(notifyScript);
// the change output
bytes output3 = changeSatoshi != 0 ? num2bin(changeSatoshi, Util.OutputValueLen) + Util.writeVarint(Util.buildPublicKeyHashScript(changePKH)) : b'';
Sha256 hashOutputs = hash256(output0 + output3);
require(hashOutputs == Util.hashOutputs(txPreimage));
}
public function issue(Sig issuerSig, Ripemd160 ownerPKH, int supply, Ripemd160 changePKH, int changeSatoshi, bytes txPreimage, int outValue) {
require(supply > 0);
// this ensures the preimage is for the current tx
SigHashType sigHashType = SigHash.ALL | SigHash.FORKID;
// SigHashType sigHashType = SigHash.ANYONECANPAY | SigHash.ALL | SigHash.FORKID;
require(Util.checkPreimageSigHashType(txPreimage, sigHashType));
// authorize
require(checkSig(issuerSig, this.issuerPubKey));
//outpoint (32-byte hash + 4-byte little endian)
bytes prevOutpoint = Util.outpoint(txPreimage);
// read previous Baton locking script:
// codePart + OP_RETURN + contractId(32bytes) + prevOutpoint(36bytes) + totalSupply(8bytes) = 96bytes
bytes lockingScript = Util.scriptCode(txPreimage);
int scriptLen = len(lockingScript);
int totalSupplyStart = scriptLen - 8;
int prevOutpointStart = totalSupplyStart - 36;
bytes lockingScriptBody = lockingScript[: scriptLen - prevOutpointStart];
int totalSupply = unpack(lockingScript[totalSupplyStart : ]) + supply;
require( this.maxSupply == 0 || totalSupply < this.maxSupply );
bytes valueBytes = num2bin(outValue, Util.OutputValueLen);
// write Token UTXO locking script
// codePart + OP_RETURN + contractId(32bytes) + prevOutpoint(36bytes) + ownerPkh(20bytes) + tokenAmount(8bytes) = 96bytes
bytes tokenData = lockingScriptBody + prevOutpoint + ownerPKH + num2bin(supply, Util.OutputValueLen);
bytes tokenLockingScript = lockingScriptBody + tokenData;
bytes output0 = valueBytes + Util.writeVarint(tokenLockingScript);
// write Baton UTXO locking script
// codePart + OP_RETURN + contractId(32bytes) + prevOutpoint(36bytes) + totalSupply(8bytes) = 76bytes
bytes batonData = lockingScriptBody + prevOutpoint + num2bin(totalSupply, Util.OutputValueLen);
bytes batonLockingScript = lockingScriptBody + batonData;
bytes output1 = valueBytes + Util.writeVarint(batonLockingScript);
// Expect the notify output
bytes notifyScript = Util.buildPublicKeyHashScript(ownerPKH);
bytes output2 = this.notifySatoshiBytes() + Util.writeVarint(notifyScript);
bytes output = output0 + output1 + output2;
if(changeSatoshi != 0 ) {
// the change output
bytes changeScript = Util.buildPublicKeyHashScript(changePKH);
output = output + num2bin(changeSatoshi, Util.OutputValueLen) + Util.writeVarint(changeScript);
}
require(hash256(output) == Util.hashOutputs(txPreimage));
}
// public function punish(bytes txPreimage, bytes prevTx, bytes prevPrevTx) {
// // this ensures the preimage is for the current tx
// SigHashType sigHashType = SigHash.ALL | SigHash.FORKID;
// require(Util.checkPreimageSigHashType(txPreimage, sigHashType));
// //check value
// if(outValue != Util.value(txPreimage)) {
// exit(true);
// }
// // bytes outpoint = Util.outpoint(txPreimage);
// // bytes prevTxId = outpoint[ : 32];
// // require(hash256(prevTx) == prevTxId);
// // //outpoint (32-byte hash + 4-byte little endian)
// // bytes lockingScript = Util.scriptCode(txPreimage);
// // // codePart + OP_RETURN + contractId(32bytes) + prevOutpoint(36bytes) + ownerPkh(20bytes) + tokenAmount(8bytes) = 96bytes
// // int scriptLen = len(lockingScript);
// // //prevOutpoint(36bytes) + ownerPkh(20bytes) + tokenAmount(8bytes) = 64bytes
// // //ownerPkh(20bytes) + tokenAmount(8bytes) = 28bytes
// // bytes prevOutpoint = lockingScript[scriptLen - 64 : scriptLen - 28 ];
// // bytes prevPrevTxId = prevOutpoint[ : 32]
// // if(hash256(prevPrevTx) != prevPrevTxId) {
// // exit(true);
// // }
// // //check tx size
// // if(len(prevPrevTx) <= len(txPreimage)) {
// // exit(true);
// // }
// require(false);
// }
public function transfer(Sig senderSig, PubKey senderPubKey, Ripemd160 receiverPKH, bytes txPreimage, int outValue) {
// this ensures the preimage is for the current tx
SigHashType sigHashType = SigHash.SINGLE | SigHash.FORKID;
require(Util.checkPreimageSigHashType(txPreimage, sigHashType));
// read UTXO Token locking script:
// codePart + OP_RETURN + contractId(32bytes) + prevOutpoint(36bytes) + ownerPkh(20bytes) + tokenAmount(8bytes) = 96bytes
bytes lockingScript = Util.scriptCode(txPreimage);
int scriptLen = len(lockingScript);
int tokenAmountStart = scriptLen - 8;
int ownerPkhStart = tokenAmountStart - 20;
int prevOutpointStart = ownerPkhStart - 36;
bytes ownerPkhBytes = lockingScript[ownerPkhStart : tokenAmountStart];
Ripemd160 ownerPKH = Ripemd160(ownerPkhBytes);
// authorize
require(hash160(senderPubKey) == ownerPKH);
require(checkSig(senderSig, senderPubKey));
require(ownerPKH != receiverPKH );
int tokenAmount = unpack(lockingScript[tokenAmountStart : ]);
bytes lockingScriptBody = lockingScript[ : prevOutpointStart];
//outpoint (32-byte hash + 4-byte little endian)
bytes prevOutpoint = Util.outpoint(txPreimage);
bytes valueBytes = num2bin(outValue, Util.OutputValueLen);
// write new locking script
bytes tokenData = prevOutpoint + receiverPKH + num2bin(tokenAmount, Util.OutputValueLen);
bytes tokenLockingScript = lockingScriptBody + tokenData;
bytes output = valueBytes + Util.writeVarint(tokenLockingScript);
require(hash256(output) == Util.hashOutputs(txPreimage));
}
public function split(Sig senderSig, PubKey senderPubKey, Ripemd160 receiverPKH, int receiverTokenAmount, int changeTokenAmount, Ripemd160 changeSatoshiPKH, int changeSatoshi, bytes txPreimage, int outValue) {
require(receiverTokenAmount > 0);
require(changeTokenAmount > 0);
// this ensures the preimage is for the current tx
SigHashType sigHashType = SigHash.ALL | SigHash.FORKID;
require(Util.checkPreimageSigHashType(txPreimage, sigHashType));
// read UTXO Token locking script:
// codePart + OP_RETURN + contractId(32bytes) + prevOutpoint(36bytes) + ownerPkh(20bytes) + tokenAmount(8bytes) = 96bytes
bytes lockingScript = Util.scriptCode(txPreimage);
int scriptLen = len(lockingScript);
int tokenAmountStart = scriptLen - 8;
int ownerPkhStart = tokenAmountStart - 20;
int prevOutpointStart = ownerPkhStart - 36;
bytes ownerPkhBytes = lockingScript[ownerPkhStart : tokenAmountStart];
Ripemd160 ownerPKH = Ripemd160(ownerPkhBytes);
// authorize
require(hash160(senderPubKey) == ownerPKH);
require(checkSig(senderSig, senderPubKey));
require(ownerPKH != receiverPKH );
int tokenAmount = unpack(lockingScript[tokenAmountStart : ]);
require(tokenAmount == receiverTokenAmount + changeTokenAmount );
bytes lockingScriptBody = lockingScript[ : prevOutpointStart];
//outpoint (32-byte hash + 4-byte little endian)
bytes prevOutpoint = Util.outpoint(txPreimage);
bytes lockingScript0 = lockingScriptBody + prevOutpoint + receiverPKH + num2bin(receiverTokenAmount, Util.OutputValueLen);
bytes lockingScript1 = lockingScriptBody + prevOutpoint + ownerPKH + num2bin(changeTokenAmount, Util.OutputValueLen);
bytes valueBytes = num2bin(outValue, Util.OutputValueLen);
//concatenating all ouputs
bytes output0 = valueBytes + Util.writeVarint(lockingScript0);
bytes output1 = valueBytes + Util.writeVarint(lockingScript1);
// Expect the notify output
bytes notifyScript = Util.buildPublicKeyHashScript(receiverPKH);
bytes output2 = this.notifySatoshiBytes() + Util.writeVarint(notifyScript);
bytes output = output0 + output1 + output2;
if(changeSatoshi != 0 ) {
// the change output
bytes changeScript = Util.buildPublicKeyHashScript(changeSatoshiPKH);
output = output + num2bin(changeSatoshi, Util.OutputValueLen) + Util.writeVarint(changeScript);
}
require(hash256(output) == Util.hashOutputs(txPreimage));
}
// burn a token utxo back to normal bitcoins
public function burn(Sig ownerSig, PubKey ownerPK, Ripemd160 changePKH, int changeSatoshi, bytes txPreimage, int outValue) {
SigHashType sigHashType = SigHash.ALL | SigHash.FORKID;
// this ensures the preimage is for the current tx
require(Util.checkPreimageSigHashType(txPreimage, sigHashType));
//check value
require(outValue == Util.value(txPreimage));
// read previous locking script:
// codePart + OP_RETURN + contractId(32bytes) + prevOutpoint(36bytes) + ownerPkh(20bytes) + tokenAmount(8bytes) = 96bytes
bytes lockingScript = Util.scriptCode(txPreimage);
int scriptLen = len(lockingScript);
int tokenAmountStart = scriptLen - 8;
int ownerPkhStart = tokenAmountStart - 20;
Ripemd160 ownerPKH = Ripemd160(lockingScript[ownerPkhStart: tokenAmountStart]);
// authorize
require(hash160(ownerPK) == ownerPKH);
require(checkSig(ownerSig, ownerPK));
// send to a P2PKH script
bytes lockingScript_ = Util.buildPublicKeyHashScript(changePKH);
bytes output = num2bin(changeSatoshi, Util.OutputValueLen) + Util.writeVarint(lockingScript_);
require(hash256(output) == Util.hashOutputs(txPreimage));
}
}