From 08526b9fb0a51489d6f79c30b98e9b6f7c24d560 Mon Sep 17 00:00:00 2001 From: Aaron Date: Mon, 25 Mar 2024 05:07:58 +0800 Subject: [PATCH] Add built-in contracts P2PKH and P2PK (#244) * Add built-in contracts --------- Co-authored-by: Xiaohui --- docs/how-to-write-a-contract/built-ins.md | 33 +++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/docs/how-to-write-a-contract/built-ins.md b/docs/how-to-write-a-contract/built-ins.md index 8d6c01ec4..d268ce7a1 100644 --- a/docs/how-to-write-a-contract/built-ins.md +++ b/docs/how-to-write-a-contract/built-ins.md @@ -785,3 +785,36 @@ class Constants { static readonly OutpointLen: bigint = BigInt(36); } ``` + +## Standard Contracts + +The following popular smart contracts come with `sCrypt`, so users do not have to write from scratch [as we did before](../how-to-deploy-and-call-a-contract/how-to-deploy-and-call-a-contract.md#method-with-signatures). + +- `P2PKH`: [Pay To PubKey Hash (P2PKH)](https://learnmeabitcoin.com/technical/p2pkh) +- `P2PK`: [Pay To PubKey (P2PK)](https://learnmeabitcoin.com/technical/p2pk) + +They compile to the same Bitcoin Script as in a standard transaction, created using raw Script. + +You can use them like any other user-defined smart contracts, as below. + +```ts +import { P2PKH } from 'scrypt-ts' + +const privateKey = bsv.PrivateKey.fromRandom(bsv.Networks.testnet) +const publicKey = privateKey.toPublicKey() +const pubKey = PubKey(toHex(publicKey)) +// create an P2PKH instance +const instance = new P2PKH(pubKey2Addr(pubKey)) +// connect the contract instance to a signer +await instance.connect(getDefaultSigner(privateKey)) +// deploy the contract +await instance.deploy() +// call the P2PKH contract +await instance.methods.unlock( + (sigResps) => findSig(sigResps, publicKey), + pubKey, + { + pubKeyOrAddrToSign: publicKey, + } as MethodCallOptions +) +```