Skip to content

Instantly share code, notes, and snippets.

@yusufidimaina9989
Last active March 14, 2024 09:12
Show Gist options
  • Save yusufidimaina9989/1bd66f41ed42e896862be51e245a8d59 to your computer and use it in GitHub Desktop.
Save yusufidimaina9989/1bd66f41ed42e896862be51e245a8d59 to your computer and use it in GitHub Desktop.
Some use cases of sCrypt
1 - P2PKH
Pay-to-PubKey-Hash (P2PKH) contract is used to send bitcoins to a bitcoin address.
`class P2PKH extends SmartContract {
@prop()
readonly address: Addr
constructor(address: Addr) {
super(address)
this.address = address
}
@method()
public unlock(sig: Sig, pubkey: PubKey) {
assert(
pubKey2Addr(pubkey) == this.address,
"pub key does not correspond to address"
)
assert(this.checkSig(sig, pubkey), "signature check failed")
}
}
`
2 - Stateful Contracts
UTXO blockchains are stateless by default, sCrypt makes it possible to write stateful contracys on bitcoin (bsv)
`export class Counter extends SmartContract {
// stateful
@prop(true)
count: bigint
constructor(count: bigint) {
super(...arguments)
this.count = count
}
@method()
public incrementOnChain() {
this.increment()
// make sure balance in the contract does not change
const amount: bigint = this.ctx.utxo.value
// outputs containing the latest state and an optional change output
const outputs: ByteString = this.buildStateOutput(amount) + this.buildChangeOutput()
// verify unlocking tx has the same outputs
assert(this.ctx.hashOutputs == hash256(outputs), 'hashOutputs mismatch')
}
@method()
increment(): void {
this.count++
}
}`
3 - ScriptContext
A public @method can get additional information beside its function parameters by accessing `this.ctx` in public methods.
` const amount: bigint = this.ctx.utxo.value
// outputs containing the latest state and an optional change output
const outputs: ByteString = this.buildStateOutput(amount) + this.buildChangeOutput()
// verify unlocking tx has the same outputs
assert(this.ctx.hashOutputs == hash256(outputs), 'hashOutputs mismatch')
`
4 - NFT (Ordinals)
By using sCrypt-ord (Ordinal SDK) you can simply inscribe and transfer ordinals NFTs.
`import { method, prop, assert, ByteString, sha256, Sha256 } from "scrypt-ts";
import { OrdinalNFT } from "scrypt-ord";
export class HashLockNFT extends OrdinalNFT {
@prop()
hash: Sha256;
constructor(hash: Sha256) {
super();
// Important: Call `init` after the `super()` statement.
this.init(...arguments);
this.hash = hash;
}
@method()
public unlock(message: ByteString) {
assert(this.hash === sha256(message), "hashes are not equal");
}
}`
5 - Fungile Tokens (BSV20)
The sCrypt-ord (SDK) also support protocol for creating fungible tokens (V1 and V2) on the Bitcoin SV blockchain.
`
class HashLockFTV2 extends BSV20V2 {
@prop()
hash: Sha256
constructor(id: ByteString, sym: ByteString, max: bigint, dec: bigint, hash: Sha256) {
super(id, sym, max, dec)
this.init(...arguments)
this.hash = hash
}
@method()
public unlock(message: ByteString) {
assert(this.hash == sha256(message), 'hashes are not equal')
}
}
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment