sCrypt-Inc / scryptTS-examples

Examples of using scryptTS
https://scrypt.io/scryptTS
11 stars 5 forks source link

Add crowdfund contract and initial tests #8

Closed iangregsondev closed 1 year ago

xhliu commented 1 year ago
    // pledge fund
    @method
    public pledge(int raisedAmount) {
        // reach target
        asssert(raisedAmount >= this.target);

        // fund goes to the recipient
        bytes lockingScript = Utils.buildPublicKeyHashScript(this.recepient);

        bytes output = Utils.buildOutput(lockingScript, raisedAmount);
        assert(hash256(output) == this.ctx.hashOutputs);

    }

    // contributor can take the fund back after deadline
   @method
    public refund(Sig sig) {
        // fundraising expired
        assert(this.ctx.locktime >= this.deadline);

        assert(this.checkSig(sig, this.contributor));
    }
iangregsondev commented 1 year ago
    // pledge fund
    @method
    public pledge(int raisedAmount) {
        // reach target
        asssert(raisedAmount >= this.target);

        // fund goes to the recipient
        bytes lockingScript = Utils.buildPublicKeyHashScript(this.recepient);

        bytes output = Utils.buildOutput(lockingScript, raisedAmount);
        assert(hash256(output) == this.ctx.hashOutputs);

    }

    // contributor can take the fund back after deadline
   @method
    public refund(Sig sig) {
        // fundraising expired
        assert(this.ctx.locktime >= this.deadline);

        assert(this.checkSig(sig, this.contributor));
    }

Thanks, I have updated it.

Seems logical now :-)

But if I don't pass in sighash types, then the hashed output is different, if I pass in the sighash types then it fails silently.

iangregsondev commented 1 year ago

You will notice that I am passing the following, but this does fail silently, in fact on a debug session the "pledge" method is not even entered,

      .setInputScript(
        {
          inputIndex,
          sigtype:
            bsv.crypto.Signature.SIGHASH_ANYONECANPAY |
            bsv.crypto.Signature.SIGHASH_SINGLE |
            bsv.crypto.Signature.SIGHASH_FORKID,
        },
        (tx) => {
          this.unlockFrom = { tx, inputIndex };
          return this.getUnlockingScript((self) => {
            self.pledge(1000n);
          });
        }
      );
iangregsondev commented 1 year ago

I did also try SIGHASH_ALL in place of SINGLE - but the same result

iangregsondev commented 1 year ago

If my understanding is correct, the SIGHASH_ANYONECANPAY must be passed, because otherwise the inputs can't be changed.

iangregsondev commented 1 year ago
      sigtype:
        bsv.crypto.Signature.SIGHASH_NONE |
        bsv.crypto.Signature.SIGHASH_ANYONECANPAY |
        bsv.crypto.Signature.SIGHASH_FORKID,
xhliu commented 1 year ago

U do not need ANYONECANPAY here, since there is no checkSig in pledge.

The default should work. ALL | FORKID

iangregsondev commented 1 year ago

Ah, ok, thanks.

I have removed it and it does work but the tests fail because the hash is different

I.e.

Test SmartContract `Crowdfund`
compiling contract /home/ian/Development/scrypt/forks/scryptTS-examples/scrypts/src/contracts/crowdfund.scrypt ...
here is the lockingscript  76a914528223920ada5975f41c217061801b558b508ce288ac
here is the output  e8030000000000001976a914528223920ada5975f41c217061801b558b508ce288ac
here is the hashoutputs  6cbad773d0124aa4481b141bab4e1ac31aa970a1b8e9e1f40240920dd699d2b0
here is the hash256(output)  93f43d9b2c54ec1455190b7d40136ab5b0cb07e38800aa778549046802d9216f
    1) should pass the public method unit test successfully.