Skip to content

Instantly share code, notes, and snippets.

@xhliu

xhliu/Pyramid.js Secret

Last active March 11, 2022 19:07
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 xhliu/859ef64d1605f85cfe8c3cd6e80fad2d to your computer and use it in GitHub Desktop.
Save xhliu/859ef64d1605f85cfe8c3cd6e80fad2d to your computer and use it in GitHub Desktop.
Pyramid
// A transparent Pyramid scheme
contract Pyramid {
@state
PubKeyHash schemer;
// satoshi amount required to enter the scheme
int entryFee;
// dust limit
static const int DUST = 1;
// recruite two members to get double payout
public function recruit(PubKeyHash recruit0, PubKeyHash recruit1, SigHashPreimage txPreimage) {
// use ANYONECANPAY so recruits can deposit
SigHashType sigHashType = SigHash.ANYONECANPAY | SigHash.ALL | SigHash.FORKID;
require(Tx.checkPreimageSigHashType(txPreimage, sigHashType));
// commission payout: double the original entry fee
bytes commissionScript = Utils.buildPublicKeyHashScript(this.schemer);
bytes commissionOutput = Utils.buildOutput(commissionScript, 2 * this.entryFee);
// keep the scheme going from recruit0
this.schemer = recruit0;
bytes recruitScript0 = this.getStateScript();
bytes recruitOutput0 = Utils.buildOutput(recruitScript0, DUST);
// keep the scheme going from recruit1
this.schemer = recruit1;
bytes recruitScript1 = this.getStateScript();
bytes recruitOutput1 = Utils.buildOutput(recruitScript1, DUST);
bytes output = commissionOutput + recruitOutput0 + recruitOutput1;
require(hash256(output) == SigHash.hashOutputs(txPreimage));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment