-
Notifications
You must be signed in to change notification settings - Fork 84
/
caller.scrypt
40 lines (34 loc) · 1.7 KB
/
caller.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
/**
* a contract calling quadratic equation contract
*/
import "txUtil.scrypt";
import "callee.scrypt";
contract Caller {
static const int N = 2;
static const int calleeContractInputIndex = 1;
static const int selfContractInputIndex = 0;
// hash of the callee contract, i.e., its locking script
PubKeyHash calleeContractHash;
public function call(Coeff co, bytes prevouts, bytes calleeContractTx,
bytes outputScript, int amount, SigHashPreimage txPreimage) {
require(Tx.checkPreimage(txPreimage));
require(hash256(prevouts) == SigHash.hashPrevouts(txPreimage));
// validate the tx containing the callee contract
bytes prevScriptTxId = TxUtil.getPrevoutTxid(prevouts, calleeContractInputIndex);
require(hash256(calleeContractTx) == prevScriptTxId);
// validate the callee contract, i.e., its locking script
int lockContractTxOutIndex = TxUtil.getPrevoutOutputIdx(prevouts, selfContractInputIndex);
bytes prevScriptCode = TxUtil.readOutput(calleeContractTx, lockContractTxOutIndex).script;
require(hash160(prevScriptCode) == this.calleeContractHash);
int l = len(outputScript);
int a = unpack(outputScript[l - 4 * N : l - 3 * N]);
int b = unpack(outputScript[l - 3 * N : l - 2 * N]);
int c = unpack(outputScript[l - 2 * N : l - N]);
require(co == { a, b, c });
int x = unpack(outputScript[l - N :]);
// ------>> x must be a root for the following quadatic equition: no need to double check
// require(a * x * x + b * x + c == 0);
bytes output = Utils.buildOutput(outputScript, amount);
require(hash256(output) == SigHash.hashOutputs(txPreimage));
}
}