Skip to content

Instantly share code, notes, and snippets.

@ccoincash
Last active November 6, 2020 07:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccoincash/dc8c7996dfc212f8ebc88e068eaf2c4a to your computer and use it in GitHub Desktop.
Save ccoincash/dc8c7996dfc212f8ebc88e068eaf2c4a to your computer and use it in GitHub Desktop.
implement binary option contract using oracle data in bsv blockchain
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