r/btc•
capistor

How would someone claim cashscript p2sh funds with their authorized address?

Could be for any simple example on the cashscript.org website.

Posted Feb 22, 2023, 10:11 PM UTC

74
2q_x•

This is the example from p2pkh.cash showing a simple authorized address example:

pragma cashscript ^0.7.3;

contract P2PKH(bytes20 pkh) {
    // Require pk to match stored pkh and signature to match
    function spend(pubkey pk, sig s) {
        require(hash160(pk) == pkh);
        require(checkSig(s, pk));
    }
}

This contract only has one function. It checks the hash of the public key provided to spend matches the public key hash specified for the contract, then it checks that the signature is valid for that public key.

You can input it here:

https://playground.cashscript.org/

And funds the contract (through funds sent via your testnet wallet). To switch between wallet view and contract view, there is an ⇆ icon in the upper right of the playground.

There are free coins from here:

https://tbch4.googol.cash/


The op codes in the redeem script actually being generated under the hood look like this:

<the authorized public key hash bytes20> OP_OVER OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG


If you get an error submitting the transaction, you can debug it with the meep command, if you install the meep debugger.


If you wanted to call the spend from javascirpt, it would be something like:

const tx = await contract.functions
  .spend(myPubicKey, new SignatureTemplate(myPrivateKey))
  .to(<someaddress>, <someAmmount>)
  .send();

If you wanted to use it from another language, you could use mainnet.cash. The documentation to use it from REST is here

4
capistorOP•

Thank you. I’m aware of the playground and the wallet there and the test net redemption process into a playground test net wallet. I’d like to redeem from p2sk on main net to a wallet outside of the playground.

Another language is ok. I’ll look at mainnet.cash.

2
capistorOP•

Where would that JavaScript code be deployed? Would I have to set up a node or connect to an api just to claim a p2sh address?

Is there no way yet to do this from cashscript playground on main net to an external wallet?

1
2q_x•

You should be able to run the p2pkh.js example if you checkout the CashScript project and do:

node p2pkh.js

So if you copied that example and put your private key in, it would submit the signed transaction from nodejs.

Obviously, you have to update the script to be your script, and the ElectrumNetworkProvider should be mainnet.

The example is using Alice's wallet, but you can change it to keys you got from Electron Cash.

1
[deleted]•

[deleted]

1