Skip to content

Commit

Permalink
Add built-in contracts P2PKH and P2PK (#244)
Browse files Browse the repository at this point in the history
* Add built-in contracts

---------

Co-authored-by: Xiaohui <[email protected]>
  • Loading branch information
gitzhou and xhliu authored Mar 24, 2024
1 parent d051140 commit 08526b9
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions docs/how-to-write-a-contract/built-ins.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<P2PKH>
)
```

0 comments on commit 08526b9

Please sign in to comment.