BSV Smart Contracts
Write, compile, test, and deploy Bitcoin SV smart contracts using high-level languages that compile to Bitcoin Script.
When to Use
- Author smart contracts in TypeScript, Go, Rust, Python, Solidity, or Move
- Compile high-level code to Bitcoin Script
- Build stateful contracts (counters, auctions, tokens) using OP_PUSH_TX
- Create spending conditions (escrow, multisig, timelocks, covenants)
- Test contracts locally before deployment
- Deploy contracts to BSV mainnet
For raw @bsv/sdk ScriptTemplate work (BitCom protocol templates like AIP, MAP, SIGMA), use the create-script-template skill instead. This skill is for contract logic — spending conditions, covenants, and stateful on-chain programs.
Framework Selection
Two frameworks compile high-level languages to Bitcoin Script:
|
Runar |
sCrypt |
| Languages |
TypeScript, Go, Rust, Python, Solidity, Move |
TypeScript (eDSL) |
| Model |
Multi-compiler with conformance suite |
Single compiler |
| Stateful |
StatefulSmartContract + OP_PUSH_TX |
SmartContract with @prop(true) |
| Testing |
TestContract API with Script VM |
Built-in test framework |
| Deployment |
runar-sdk providers + signers |
scrypt-ts deploy API |
| IDE |
Full IDE support (valid TS/Go/Rust) |
VS Code extension |
| Playground |
https://runar.run |
https://playground.scrypt.io |
| Install |
pnpm add runar-lang runar-compiler runar-cli |
npx scrypt-cli project my-app |
| Builtins |
53+ functions (crypto, math, EC, post-quantum, BLAKE3) |
Standard Bitcoin Script ops |
| Codegen |
SDK wrappers in TS, Go, Rust, Python from one compile |
N/A |
| Maturity |
v0.3.0 (23 examples, 28 conformance tests) |
Production |
| Source |
https://github.com/icellan/runar |
https://github.com/sCrypt-Inc/scrypt-ts |
Decision guide:
- Multiple language teams or cross-compiler verification needed → Runar
- Production deployment with mature tooling → sCrypt
- Post-quantum or advanced cryptographic primitives → Runar (has WOTS+, SLH-DSA, Schnorr ZKP examples)
- Quick prototyping with TypeScript only → either works
Contract Types
Stateless (Spending Conditions)
All properties are immutable. The contract is satisfied in a single transaction. Both frameworks compile P2PKH to the same Bitcoin Script: OP_DUP OP_HASH160 <hash> OP_EQUALVERIFY OP_CHECKSIG.
Stateful (On-Chain State Machines)
Mutable properties persist across transactions via OP_PUSH_TX. Runar handles preimage verification automatically in StatefulSmartContract. sCrypt requires explicit buildStateOutput + hashOutputs verification in each public method.
In Runar, this.addOutput(satoshis, ...mutableFields) creates continuation outputs — pass only MUTABLE state values (readonly props are embedded in the script). Access this.txPreimage for BIP-143 preimage data (e.g., substr(this.txPreimage, 68n, 32n) for the outpoint txid). The compiler auto-adds _changePKH, _changeAmount, and txPreimage params to the ABI.
Key Syntax Differences
| Feature |
Runar |
sCrypt |
| Immutable prop |
readonly keyword |
@prop() decorator |
| Mutable prop |
No keyword |
@prop(true) decorator |
| Public method |
public keyword |
@method() decorator |
| Constructor super |
super(prop1, prop2) |
super(...arguments) |
| Sig check |
checkSig(sig, pk) |
this.checkSig(sig, pk) |
| State persistence |
Automatic |
Manual buildStateOutput |
| File extension |
.runar.ts |
.ts |
See references/contract-patterns.md for complete side-by-side code examples of P2PKH, Escrow, Counter, Covenant, and more.
Common Patterns
| Pattern |
Use Case |
Stateful |
| P2PKH |
Standard payments |
No |
| Escrow |
Multi-party authorization |
No |
| Counter |
On-chain state machine |
Yes |
| Auction |
Bidding with deadline |
Yes |
| Covenant Vault |
Spending constraints |
No |
| Fungible Token |
Split/merge token supply |
Yes |
| NFT |
Transfer/burn ownership |
Yes |
| Oracle Price Feed |
Rabin signature verification |
No |
| Hash Time Lock |
Atomic swaps |
No |
| Multi-Sig (M-of-N) |
Corporate treasury, shared custody |
No |
| BLAKE3 Hash Lock |
Modern hash verification |
No |
| Post-Quantum Wallet |
WOTS+, SLH-DSA signatures |
No |
| TicTacToe |
On-chain game logic |
Yes |
See references/runar-guide.md and references/scrypt-guide.md for complete examples of each pattern.
Development Workflow
1. Write
Create a contract file:
- Runar:
MyContract.runar.ts (or .runar.go, .runar.rs, .runar.py)
- sCrypt:
MyContract.ts with @method() decorators
2. Compile
# Runar
npx runar compile MyContract.runar.ts
# sCrypt
npx scrypt-cli compile
3. Test
// Runar — TestContract API with pre-generated test keys
import { TestContract, ALICE, BOB } from 'runar-testing';
// Addr type = 20-byte hex pubKeyHash, NOT Base58Check address
const contract = TestContract.fromSource(source, { pubKeyHash: ALICE.pubKeyHash });
// Test keys have: privKey, pubKey, pubKeyHash (hex), address (base58), wif, testSig
const result = contract.call('unlock', { sig: ALICE.testSig, pubKey: ALICE.pubKey });
expect(result.success).toBe(true);
// sCrypt — built-in testing
const instance = new P2PKH(pubKeyHash);
await instance.connect(getDefaultSigner());
const deployTx = await instance.deploy(1000);
const callTx = await instance.methods.unlock(sig, pubKey);
4. Deploy
// Runar — SDK deployment
import { Contract, WocProvider, LocalSigner } from 'runar-sdk';
const provider = new WocProvider('mainnet');
const signer = new LocalSigner(privateKey);
const contract = new Contract(artifact, provider, signer);
const deployTx = await contract.deploy([constructorArg], { satoshis: 1000 });
// sCrypt — deploy API
await instance.connect(new TestWallet(privateKey, provider));
const tx = await instance.deploy(satoshis);
Integration with @bsv/sdk
Both frameworks produce standard Bitcoin Script. The compiled output integrates with @bsv/sdk transaction building:
import { Transaction, P2PKH } from '@bsv/sdk';
// Use compiled contract script as locking script in any @bsv/sdk transaction
For raw script template authoring (BitCom protocols), see the create-script-template skill.
For script validation and opcode analysis, see the validate-bsv-script skill.
Additional Resources
Reference Files
For detailed framework-specific guides, consult:
references/runar-guide.md — Runar installation, language reference, compilation pipeline, all contract patterns, deployment SDK, multi-language support
references/scrypt-guide.md — sCrypt installation, decorators, testing, deployment, advanced patterns
references/contract-patterns.md — Side-by-side implementations of common patterns in both frameworks
External Resources
1---2name: smart-contracts3description: This skill should be used when the user asks about "BSV smart contract", "Bitcoin smart contract", "write a smart contract", "create a BSV contract", "use sCrypt", "use Runar", "compile to Bitcoin Script", "stateful contract", "covenant", "OP_PUSH_TX", "escrow contract", "auction contract", "token contract", "deploy smart contract", "test smart contract", or needs to author, compile, test, or deploy Bitcoin SV smart contracts using high-level languages.4---56# BSV Smart Contracts78Write, compile, test, and deploy Bitcoin SV smart contracts using high-level languages that compile to Bitcoin Script.910## When to Use1112- Author smart contracts in TypeScript, Go, Rust, Python, Solidity, or Move13- Compile high-level code to Bitcoin Script14- Build stateful contracts (counters, auctions, tokens) using OP_PUSH_TX15- Create spending conditions (escrow, multisig, timelocks, covenants)16- Test contracts locally before deployment17- Deploy contracts to BSV mainnet1819For raw `@bsv/sdk` ScriptTemplate work (BitCom protocol templates like AIP, MAP, SIGMA), use the `create-script-template` skill instead. This skill is for contract logic — spending conditions, covenants, and stateful on-chain programs.2021## Framework Selection2223Two frameworks compile high-level languages to Bitcoin Script:2425| | **Runar** | **sCrypt** |26|---|---|---|27| **Languages** | TypeScript, Go, Rust, Python, Solidity, Move | TypeScript (eDSL) |28| **Model** | Multi-compiler with conformance suite | Single compiler |29| **Stateful** | `StatefulSmartContract` + OP_PUSH_TX | `SmartContract` with `@prop(true)` |30| **Testing** | `TestContract` API with Script VM | Built-in test framework |31| **Deployment** | `runar-sdk` providers + signers | `scrypt-ts` deploy API |32| **IDE** | Full IDE support (valid TS/Go/Rust) | VS Code extension |33| **Playground** | https://runar.run | https://playground.scrypt.io |34| **Install** | `pnpm add runar-lang runar-compiler runar-cli` | `npx scrypt-cli project my-app` |35| **Builtins** | 53+ functions (crypto, math, EC, post-quantum, BLAKE3) | Standard Bitcoin Script ops |36| **Codegen** | SDK wrappers in TS, Go, Rust, Python from one compile | N/A |37| **Maturity** | v0.3.0 (23 examples, 28 conformance tests) | Production |38| **Source** | https://github.com/icellan/runar | https://github.com/sCrypt-Inc/scrypt-ts |3940**Decision guide:**41- Multiple language teams or cross-compiler verification needed → **Runar**42- Production deployment with mature tooling → **sCrypt**43- Post-quantum or advanced cryptographic primitives → **Runar** (has WOTS+, SLH-DSA, Schnorr ZKP examples)44- Quick prototyping with TypeScript only → **either works**4546## Contract Types4748### Stateless (Spending Conditions)4950All properties are immutable. The contract is satisfied in a single transaction. Both frameworks compile P2PKH to the same Bitcoin Script: `OP_DUP OP_HASH160 <hash> OP_EQUALVERIFY OP_CHECKSIG`.5152### Stateful (On-Chain State Machines)5354Mutable properties persist across transactions via OP_PUSH_TX. Runar handles preimage verification automatically in `StatefulSmartContract`. sCrypt requires explicit `buildStateOutput` + `hashOutputs` verification in each public method.5556In Runar, `this.addOutput(satoshis, ...mutableFields)` creates continuation outputs — pass only MUTABLE state values (readonly props are embedded in the script). Access `this.txPreimage` for BIP-143 preimage data (e.g., `substr(this.txPreimage, 68n, 32n)` for the outpoint txid). The compiler auto-adds `_changePKH`, `_changeAmount`, and `txPreimage` params to the ABI.5758### Key Syntax Differences5960| Feature | Runar | sCrypt |61|---------|-------|--------|62| Immutable prop | `readonly` keyword | `@prop()` decorator |63| Mutable prop | No keyword | `@prop(true)` decorator |64| Public method | `public` keyword | `@method()` decorator |65| Constructor super | `super(prop1, prop2)` | `super(...arguments)` |66| Sig check | `checkSig(sig, pk)` | `this.checkSig(sig, pk)` |67| State persistence | Automatic | Manual `buildStateOutput` |68| File extension | `.runar.ts` | `.ts` |6970See `references/contract-patterns.md` for complete side-by-side code examples of P2PKH, Escrow, Counter, Covenant, and more.7172## Common Patterns7374| Pattern | Use Case | Stateful |75|---------|----------|----------|76| P2PKH | Standard payments | No |77| Escrow | Multi-party authorization | No |78| Counter | On-chain state machine | Yes |79| Auction | Bidding with deadline | Yes |80| Covenant Vault | Spending constraints | No |81| Fungible Token | Split/merge token supply | Yes |82| NFT | Transfer/burn ownership | Yes |83| Oracle Price Feed | Rabin signature verification | No |84| Hash Time Lock | Atomic swaps | No |85| Multi-Sig (M-of-N) | Corporate treasury, shared custody | No |86| BLAKE3 Hash Lock | Modern hash verification | No |87| Post-Quantum Wallet | WOTS+, SLH-DSA signatures | No |88| TicTacToe | On-chain game logic | Yes |8990See `references/runar-guide.md` and `references/scrypt-guide.md` for complete examples of each pattern.9192## Development Workflow9394### 1. Write9596Create a contract file:97- Runar: `MyContract.runar.ts` (or `.runar.go`, `.runar.rs`, `.runar.py`)98- sCrypt: `MyContract.ts` with `@method()` decorators99100### 2. Compile101102```bash103# Runar104npx runar compile MyContract.runar.ts105106# sCrypt107npx scrypt-cli compile108```109110### 3. Test111112```typescript113// Runar — TestContract API with pre-generated test keys114import { TestContract, ALICE, BOB } from 'runar-testing';115// Addr type = 20-byte hex pubKeyHash, NOT Base58Check address116const contract = TestContract.fromSource(source, { pubKeyHash: ALICE.pubKeyHash });117// Test keys have: privKey, pubKey, pubKeyHash (hex), address (base58), wif, testSig118const result = contract.call('unlock', { sig: ALICE.testSig, pubKey: ALICE.pubKey });119expect(result.success).toBe(true);120```121122```typescript123// sCrypt — built-in testing124const instance = new P2PKH(pubKeyHash);125await instance.connect(getDefaultSigner());126const deployTx = await instance.deploy(1000);127const callTx = await instance.methods.unlock(sig, pubKey);128```129130### 4. Deploy131132```typescript133// Runar — SDK deployment134import { Contract, WocProvider, LocalSigner } from 'runar-sdk';135const provider = new WocProvider('mainnet');136const signer = new LocalSigner(privateKey);137const contract = new Contract(artifact, provider, signer);138const deployTx = await contract.deploy([constructorArg], { satoshis: 1000 });139```140141```typescript142// sCrypt — deploy API143await instance.connect(new TestWallet(privateKey, provider));144const tx = await instance.deploy(satoshis);145```146147## Integration with @bsv/sdk148149Both frameworks produce standard Bitcoin Script. The compiled output integrates with `@bsv/sdk` transaction building:150151```typescript152import { Transaction, P2PKH } from '@bsv/sdk';153// Use compiled contract script as locking script in any @bsv/sdk transaction154```155156For raw script template authoring (BitCom protocols), see the `create-script-template` skill.157For script validation and opcode analysis, see the `validate-bsv-script` skill.158159## Additional Resources160161### Reference Files162163For detailed framework-specific guides, consult:164- **`references/runar-guide.md`** — Runar installation, language reference, compilation pipeline, all contract patterns, deployment SDK, multi-language support165- **`references/scrypt-guide.md`** — sCrypt installation, decorators, testing, deployment, advanced patterns166- **`references/contract-patterns.md`** — Side-by-side implementations of common patterns in both frameworks167168### External Resources169170- **Runar Playground**: https://runar.run171- **Runar Source**: https://github.com/icellan/runar172- **Runar Docs**: https://runar.build173- **sCrypt Docs**: https://docs.scrypt.io174- **sCrypt Playground**: https://playground.scrypt.io