Last active
November 6, 2020 07:41
-
-
Save ccoincash/dc8c7996dfc212f8ebc88e068eaf2c4a to your computer and use it in GitHub Desktop.
implement binary option contract using oracle data in bsv blockchain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "util.scrypt"; | |
contract BinaryOption { | |
int betPrice; | |
int rabinPubKey; | |
int timestamp; | |
Ripemd160 pubKeyHashA; | |
Ripemd160 pubKeyHashB; | |
function hash(bytes x): bytes { | |
// expand into 512 bit hash | |
bytes hx = sha256(x); | |
int idx = len(hx) / 2; | |
return sha256(hx[:idx]) + sha256(hx[idx:]); | |
} | |
public function unlock(SigHashPreimage txPreimage, int sig, bytes msg, bytes padding, int outAmount) { | |
// check preimage | |
require(Tx.checkPreimage(txPreimage)); | |
// verify rabin signature | |
int h = Util.fromLEUnsigned(this.hash(msg + padding)); | |
require((sig * sig) % this.rabinPubKey == h % this.rabinPubKey); | |
// first 8 bytes | |
int price = Util.fromLEUnsigned(msg[0:8]); | |
// the next 8 bytes | |
int timestamp = Util.fromLEUnsigned(msg[8:16]); | |
// check the time | |
require(timestamp == this.timestamp); | |
// check price | |
bytes outputScript = b''; | |
if (price >= this.betPrice) { | |
outputScript = Util.buildPublicKeyHashScript(this.pubKeyHashA); | |
} | |
else { | |
outputScript = Util.buildPublicKeyHashScript(this.pubKeyHashB); | |
} | |
// check the output is the right winner | |
bytes outputHash = hash256(Util.buildOutput(outputScript, outAmount)); | |
bytes outputHash2 = Util.hashOutputs(txPreimage); | |
require(outputHash == outputHash2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment