Distribution Covenant

The intermediary covenants that distributes the funds from the PAY_COV to its children cashdrop covenants (CD_COVs).

PARAMETERS

NameData TypeDescription
pkpubkeyPurelyPeer’s public key. This is used in ensuring that only PurelyPeer server can call the functions of this covenant.
cdCovAmtintThe BCH amount that will be sent to its children cashdrop covenants (CD_COVs)
questFunderPKHbytes20The quest funder’s public key hash, where the refunded amount gets sent to.
scriptHash1,scriptHash2,….scriptHashNbytes32The script hashes of this covenant’s children CD_COVs. This parameter is dynamic depending on how many cashdrop covenants a DIST_COV has, meaning if the DIST_COV has 3 CD_COVs, it will have three (3) of this parameter in its constructor —- scriptHash1 scriptHash2, scriptHash3 The maximum value of N here is 9, since the contract can only hold up to 9 CD_COV script hashes at the time.

FUNCTIONS

  • refund = used to refund the funds to the quest funder in case of a server or network error during distribution. The amount refunded is the original quest amount subtracted by the transaction fee (dust).

Parameters:

NameTypeDescription
ssigPurelyPeer’s signature used to secure the contract, such that only PurelyPeer server can execute this function.
  • distribute = the main function to distribute the funds to the cashdrop covenants (CD_COVs)

Parameters:

NameData TypeDescription
ssigPurelyPeer’s signature used to secure the contract, such that only PurelyPeer server can execute this function.
cdCovScriptHashbytes32The CD_COV script hash where the funds will be distributed.
isLastScriptHashboolA flag to determine if CD_COV being funded is the last child. This is for the purpose of knowing if the transaction should still construct a change output or not.

CONTRACT SCRIPT

pragma cashscript ^0.8.0;

contract DistributionCovenant (
    pubkey pk,
    int cdCovAmt,
    bytes20 questFunderPKH,
        bytes32 scriptHash1,
        bytes32 scriptHash2,
        bytes32 scriptHash3,
        bytes32 scriptHash4,
        bytes32 scriptHash5,
        ...
        bytes32 scriptHashN
) {
    function refund (sig s) {
        require(checkSig(s, pk));

        bytes25 questFunder = new LockingBytecodeP2PKH(questFunderPKH);
        require(tx.outputs[0].lockingBytecode == questFunder);
        require(tx.outputs.length == 1);
    }

    function distribute (
        sig s,
        bytes32 cdCovScriptHash,
        bool isLastScriptHash
    ) {
        require(checkSig(s, pk));
        require(
                    cdCovScriptHash == scriptHash1 ||
                    cdCovScriptHash == scriptHash2 ||
                    cdCovScriptHash == scriptHash3 ||
                    cdCovScriptHash == scriptHash4 ||
                    cdCovScriptHash == scriptHash5 ||
                    ...
                    cdCovScriptHash == scriptHashN
                );

        bytes35 cdCov = new LockingBytecodeP2SH32(cdCovScriptHash);
        require(tx.outputs[0].lockingBytecode == cdCov);
        require(tx.outputs[0].value == cdCovAmt);

        if (!isLastScriptHash) {
            int minerFee = 1100;
            int change = tx.inputs[this.activeInputIndex].value - cdCovAmt - minerFee;
            bytes changeBytecode = tx.inputs[this.activeInputIndex].lockingBytecode;
            require(tx.outputs[1].lockingBytecode == changeBytecode);
            require(tx.outputs[1].value == change);
        }
    }
}