@btc-vision/btc-runtime 1.10.11 → 1.11.0-alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/README.md +48 -224
  2. package/SECURITY.md +38 -191
  3. package/docs/README.md +28 -0
  4. package/docs/advanced/contract-upgrades.md +537 -0
  5. package/docs/advanced/plugins.md +90 -25
  6. package/docs/api-reference/blockchain.md +48 -14
  7. package/docs/api-reference/storage.md +2 -111
  8. package/docs/contracts/op-net-base.md +22 -0
  9. package/docs/contracts/upgradeable.md +396 -0
  10. package/docs/core-concepts/blockchain-environment.md +0 -2
  11. package/docs/core-concepts/security.md +8 -111
  12. package/docs/core-concepts/storage-system.md +1 -32
  13. package/docs/examples/nft-with-reservations.md +8 -238
  14. package/docs/storage/memory-maps.md +1 -44
  15. package/docs/storage/stored-arrays.md +1 -65
  16. package/docs/storage/stored-maps.md +1 -73
  17. package/docs/storage/stored-primitives.md +2 -49
  18. package/docs/types/bytes-writer-reader.md +76 -0
  19. package/docs/types/safe-math.md +2 -45
  20. package/package.json +5 -5
  21. package/runtime/buffer/BytesReader.ts +90 -3
  22. package/runtime/buffer/BytesWriter.ts +81 -3
  23. package/runtime/contracts/OP721.ts +40 -4
  24. package/runtime/contracts/OP_NET.ts +83 -11
  25. package/runtime/contracts/Upgradeable.ts +242 -0
  26. package/runtime/env/BlockchainEnvironment.ts +124 -27
  27. package/runtime/env/global.ts +24 -0
  28. package/runtime/events/upgradeable/UpgradeableEvents.ts +41 -0
  29. package/runtime/generic/AddressMap.ts +20 -18
  30. package/runtime/generic/ExtendedAddressMap.ts +147 -0
  31. package/runtime/generic/MapUint8Array.ts +20 -18
  32. package/runtime/index.ts +8 -0
  33. package/runtime/plugins/Plugin.ts +34 -0
  34. package/runtime/plugins/UpgradeablePlugin.ts +279 -0
  35. package/runtime/storage/BaseStoredString.ts +1 -1
  36. package/runtime/storage/arrays/StoredPackedArray.ts +4 -0
  37. package/runtime/types/ExtendedAddress.ts +36 -24
  38. package/runtime/types/ExtendedAddressCache.ts +27 -0
  39. package/runtime/types/SafeMath.ts +109 -18
  40. package/runtime/types/SchnorrSignature.ts +44 -0
  41. package/runtime/utils/lengths.ts +2 -0
package/README.md CHANGED
@@ -11,19 +11,13 @@
11
11
 
12
12
  ## Overview
13
13
 
14
- The **OPNet Smart Contract Runtime** is the foundational framework for building decentralized applications directly on
15
- Bitcoin Layer 1 (L1). Written in AssemblyScript and compiled to WebAssembly, btc-runtime enables developers to create,
16
- deploy, and execute smart contracts on the Bitcoin network with the same expressiveness as Ethereum's Solidity.
14
+ The OPNet Smart Contract Runtime is the framework for building decentralized applications on Bitcoin L1. Written in AssemblyScript and compiled to WebAssembly, it enables smart contract development on Bitcoin with similar expressiveness to Solidity.
17
15
 
18
- Unlike Bitcoin Layer 2 solutions, OPNet operates directly on Bitcoin's base layer, inheriting Bitcoin's security
19
- guarantees and decentralization properties while adding programmable smart contract capabilities.
16
+ Unlike Bitcoin Layer 2 solutions, OPNet operates directly on Bitcoin's base layer, inheriting Bitcoin's security guarantees and decentralization properties while adding programmable smart contract capabilities.
20
17
 
21
18
  > **What is OPNet?**
22
19
  >
23
- > OPNet (Open Protocol Network) is a consensus-layer built on Bitcoin L1. It allows developers to write smart
24
- > contracts in AssemblyScript or similar that compile to WebAssembly (WASM) and execute deterministically across all
25
- > network nodes. Think of it as "Solidity for Bitcoin" - you get the programmability of Ethereum with the security of
26
- > Bitcoin.
20
+ > OPNet (Open Protocol Network) is a consensus-layer built on Bitcoin L1. It allows developers to write smart contracts in AssemblyScript or similar that compile to WebAssembly (WASM) and execute deterministically across all network nodes. Think of it as "Solidity for Bitcoin" - you get the programmability of Ethereum with the security of Bitcoin.
27
21
 
28
22
  > **Why AssemblyScript?**
29
23
  >
@@ -33,51 +27,32 @@ guarantees and decentralization properties while adding programmable smart contr
33
27
  > - **Memory safety** through WASM's sandboxed environment
34
28
  > - **Familiar syntax** for TypeScript/JavaScript developers
35
29
 
36
- > **IMPORTANT: Floating-Point Arithmetic is Prohibited**
30
+ > **Floating-Point Arithmetic is Prohibited**
37
31
  >
38
- > Floating-point arithmetic (`f32`, `f64`) is **strictly prohibited** in blockchain and smart contract environments.
39
- > Floating-point operations are **non-deterministic** across different CPU architectures, compilers, and platforms due
40
- > to differences in rounding, precision, and IEEE 754 implementation details.
32
+ > Floating-point arithmetic (`f32`, `f64`) is **strictly prohibited** in blockchain and smart contract environments. Floating-point operations are non-deterministic across different CPU architectures, compilers, and platforms due to differences in rounding, precision, and IEEE 754 implementation details.
41
33
  >
42
- > **Always use integer arithmetic** (`u128`, `u256`) for all blockchain computations. For decimal values, use
43
- > fixed-point representation (e.g., store currency as smallest units like satoshis). This library provides full support
44
- > for 128-bit and 256-bit integer operations through [@btc-vision/as-bignum](https://github.com/btc-vision/as-bignum).
34
+ > **Always use integer arithmetic** (`u128`, `u256`) for all blockchain computations. For decimal values, use fixed-point representation (e.g., store currency as smallest units like satoshis). This library provides full support for 128-bit and 256-bit integer operations through [@btc-vision/as-bignum](https://github.com/btc-vision/as-bignum).
45
35
 
46
36
  ## Security Audit
47
37
 
48
38
  <p align="center">
49
39
  <a href="https://verichains.io">
50
- <img src="https://raw.githubusercontent.com/btc-vision/contract-logo/refs/heads/main/public-assets/verichains.png" alt="Verichains" width="300"/>
40
+ <img src="https://raw.githubusercontent.com/btc-vision/contract-logo/refs/heads/main/public-assets/verichains.png" alt="Verichains" width="100"/>
51
41
  </a>
52
42
  </p>
53
43
 
54
44
  <p align="center">
55
45
  <a href="https://verichains.io">
56
- <img src="https://img.shields.io/badge/Security%20Audit-Verichains-4C35E0?style=for-the-badge&logo=data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAyNCAyNCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cGF0aCBkPSJNMTIgMkw0IDV2Ni41YzAgNS4yNSAzLjQgMTAuMiA4IDExLjUgNC42LTEuMyA4LTYuMjUgOC0xMS41VjVsLTgtM3ptMCAxMC45OVYxOS41Yy0zLjQ1LTEuMTctNS45My00LjgtNi02LjVWNi4zTDEyIDRsMCA4Ljk5eiIgZmlsbD0id2hpdGUiLz48L3N2Zz4=" alt="Audited by Verichains"/>
46
+ <img src="https://img.shields.io/badge/Security%20Audit-Verichains-4C35E0?style=for-the-badge" alt="Audited by Verichains"/>
57
47
  </a>
58
48
  <a href="./SECURITY.md">
59
- <img src="https://img.shields.io/badge/Security-Report-22C55E?style=for-the-badge&logo=data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAyNCAyNCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cGF0aCBkPSJNOSAxNi4xN0w0LjgzIDEybC0xLjQyIDEuNDFMOSAxOSAyMSA3bC0xLjQxLTEuNDFMOSAxNi4xN3oiIGZpbGw9IndoaXRlIi8+PC9zdmc+" alt="Security Report"/>
49
+ <img src="https://img.shields.io/badge/Security-Report-22C55E?style=for-the-badge" alt="Security Report"/>
60
50
  </a>
61
51
  </p>
62
52
 
63
- This runtime has been professionally audited by [**Verichains**](https://verichains.io), a leading blockchain security
64
- firm. The audit covered all core components including contract standards (OP20, OP721), storage systems, cryptographic
65
- operations, and security mechanisms.
53
+ This runtime has been professionally audited by [Verichains](https://verichains.io).
66
54
 
67
- For full details, see [SECURITY.md](./SECURITY.md).
68
-
69
- ## Features
70
-
71
- | Feature | Description |
72
- |------------------------------|------------------------------------------------------------------|
73
- | **Contract Standards** | OP20 (fungible tokens), OP721 (NFTs), OP20S (gasless signatures) |
74
- | **Storage System** | Pointer-based persistent storage with SHA256 key hashing |
75
- | **SafeMath** | Overflow/underflow protection for all arithmetic operations |
76
- | **Reentrancy Protection** | Built-in guards with STANDARD and CALLBACK modes |
77
- | **Cryptographic Operations** | Schnorr signatures, ML-DSA (quantum-resistant), SHA256 |
78
- | **Bitcoin Integration** | Transaction parsing, address validation, script building |
79
- | **Event System** | 352-byte events for state change notifications |
80
- | **Cross-Contract Calls** | Inter-contract communication with configurable failure handling |
55
+ See [SECURITY.md](./SECURITY.md) for details.
81
56
 
82
57
  ## Installation
83
58
 
@@ -87,10 +62,6 @@ npm install @btc-vision/btc-runtime
87
62
 
88
63
  ## Quick Start
89
64
 
90
- ### Your First Contract
91
-
92
- Here's a minimal OP20 token contract to get you started:
93
-
94
65
  ```typescript
95
66
  import { u256 } from '@btc-vision/as-bignum/assembly';
96
67
  import {
@@ -103,219 +74,72 @@ import {
103
74
 
104
75
  @final
105
76
  export class MyToken extends OP20 {
106
- public constructor() {
107
- super();
108
- // NOTE: Constructor runs on EVERY interaction, not just deployment!
109
- }
110
-
111
- // This runs ONCE when the contract is deployed (like Solidity's constructor)
112
77
  public override onDeployment(_calldata: Calldata): void {
113
- const maxSupply: u256 = u256.fromString('1000000000000000000000000'); // 1 million tokens
78
+ const maxSupply: u256 = u256.fromString('1000000000000000000000000');
114
79
  const decimals: u8 = 18;
115
80
  const name: string = 'MyToken';
116
81
  const symbol: string = 'MTK';
117
82
 
118
83
  this.instantiate(new OP20InitParameters(maxSupply, decimals, name, symbol));
119
-
120
- // Mint initial supply to deployer
121
84
  this._mint(Blockchain.tx.origin, maxSupply);
122
85
  }
123
-
124
- // Custom mint function (deployer only)
125
- public mint(calldata: Calldata): BytesWriter {
126
- this.onlyDeployer(Blockchain.tx.sender);
127
-
128
- const to = calldata.readAddress();
129
- const amount = calldata.readU256();
130
- this._mint(to, amount);
131
-
132
- return new BytesWriter(0);
133
- }
134
86
  }
135
87
  ```
136
88
 
137
- ### Solidity Comparison
138
-
139
- If you're coming from Solidity/EVM development, here's how OPNet concepts map:
140
-
141
- | Solidity/EVM | OPNet/btc-runtime | Notes |
142
- |-----------------------------|-------------------------------------------|-----------------------------|
143
- | `contract MyContract` | `class MyContract extends OP_NET` | Base class inheritance |
144
- | `constructor()` | `onDeployment(calldata)` | Runs once at deployment |
145
- | `msg.sender` | `Blockchain.tx.sender` | Immediate caller |
146
- | `tx.origin` | `Blockchain.tx.origin` | Original transaction signer |
147
- | `block.number` | `Blockchain.block.number` | Current block height |
148
- | `mapping(address => uint)` | `AddressMemoryMap` + `StoredU256` | Pointer-based storage |
149
- | `emit Transfer(...)` | `this.emitEvent(new TransferEvent(...))` | Event emission |
150
- | `ERC20` | `OP20` | Fungible token standard |
151
- | `ERC721` | `OP721` | Non-fungible token standard |
152
- | `uint256` | `u256` | 256-bit unsigned integer |
153
- | `require(condition, "msg")` | `if (!condition) throw new Revert("msg")` | Error handling |
154
- | `modifier onlyOwner` | `this.onlyDeployer(sender)` | Access control |
89
+ ## Solidity Comparison
90
+
91
+ | Solidity/EVM | OPNet/btc-runtime |
92
+ |-----------------------------|-------------------------------------------|
93
+ | `contract MyContract` | `class MyContract extends OP_NET` |
94
+ | `constructor()` | `onDeployment(calldata)` |
95
+ | `msg.sender` | `Blockchain.tx.sender` |
96
+ | `tx.origin` | `Blockchain.tx.origin` |
97
+ | `block.number` | `Blockchain.block.number` |
98
+ | `mapping(address => uint)` | `AddressMemoryMap` + `StoredU256` |
99
+ | `emit Transfer(...)` | `this.emitEvent(new TransferEvent(...))` |
100
+ | `ERC20` | `OP20` |
101
+ | `ERC721` | `OP721` |
102
+ | `require(condition, "msg")` | `if (!condition) throw new Revert("msg")` |
155
103
 
156
104
  ## Documentation
157
105
 
158
- Comprehensive documentation is available in the [docs/](./docs/) directory:
159
-
160
- ### Getting Started
161
-
162
- - [Installation](./docs/getting-started/installation.md) - Setup and configuration
163
- - [First Contract](./docs/getting-started/first-contract.md) - Step-by-step tutorial
164
- - [Project Structure](./docs/getting-started/project-structure.md) - Directory layout
165
-
166
- ### Core Concepts
167
-
168
- - [Blockchain Environment](./docs/core-concepts/blockchain-environment.md) - Runtime context
169
- - [Storage System](./docs/core-concepts/storage-system.md) - How data persistence works
170
- - [Pointers](./docs/core-concepts/pointers.md) - Storage key management
171
- - [Events](./docs/core-concepts/events.md) - State change notifications
172
- - [Security](./docs/core-concepts/security.md) - Protection mechanisms
106
+ Documentation is available in [docs/](./docs/):
173
107
 
174
- ### Contract Standards
175
-
176
- - [OP_NET Base](./docs/contracts/op-net-base.md) - Abstract contract class
177
- - [OP20 Token](./docs/contracts/op20-token.md) - Fungible token standard
178
- - [OP20S Signatures](./docs/contracts/op20s-signatures.md) - Gasless approvals
179
- - [OP721 NFT](./docs/contracts/op721-nft.md) - Non-fungible tokens
180
- - [ReentrancyGuard](./docs/contracts/reentrancy-guard.md) - Reentrancy protection
181
-
182
- ### Types & Utilities
183
-
184
- - [Address](./docs/types/address.md) - 32-byte address handling
185
- - [SafeMath](./docs/types/safe-math.md) - Overflow-safe arithmetic
186
- - [Calldata](./docs/types/calldata.md) - Input parsing
187
- - [BytesWriter/Reader](./docs/types/bytes-writer-reader.md) - Serialization
188
-
189
- ### Storage Types
190
-
191
- - [Stored Primitives](./docs/storage/stored-primitives.md) - Basic value storage
192
- - [Stored Arrays](./docs/storage/stored-arrays.md) - Array storage
193
- - [Stored Maps](./docs/storage/stored-maps.md) - Key-value storage
194
- - [Memory Maps](./docs/storage/memory-maps.md) - In-memory mappings
195
-
196
- ### Advanced Topics
197
-
198
- - [Cross-Contract Calls](./docs/advanced/cross-contract-calls.md) - Inter-contract communication
199
- - [Signature Verification](./docs/advanced/signature-verification.md) - Cryptographic operations
200
- - [Quantum Resistance](./docs/advanced/quantum-resistance.md) - ML-DSA support
201
- - [Bitcoin Scripts](./docs/advanced/bitcoin-scripts.md) - Script building
202
- - [Plugins](./docs/advanced/plugins.md) - Extending functionality
203
-
204
- ### Examples
205
-
206
- - [Basic Token](./docs/examples/basic-token.md) - Simple OP20 implementation
207
- - [NFT with Reservations](./docs/examples/nft-with-reservations.md) - Advanced NFT
208
- - [Stablecoin](./docs/examples/stablecoin.md) - Role-based token
209
- - [Oracle Integration](./docs/examples/oracle-integration.md) - Price feeds
210
-
211
- ### API Reference
212
-
213
- - [Blockchain](./docs/api-reference/blockchain.md) - Environment methods
214
- - [OP20](./docs/api-reference/op20.md) - Token standard API
215
- - [OP721](./docs/api-reference/op721.md) - NFT standard API
216
- - [SafeMath](./docs/api-reference/safe-math.md) - Math operations
217
- - [Storage](./docs/api-reference/storage.md) - Storage classes
218
- - [Events](./docs/api-reference/events.md) - Event classes
108
+ - **Getting Started**: [Installation](./docs/getting-started/installation.md), [First Contract](./docs/getting-started/first-contract.md), [Project Structure](./docs/getting-started/project-structure.md)
109
+ - **Core Concepts**: [Blockchain Environment](./docs/core-concepts/blockchain-environment.md), [Storage System](./docs/core-concepts/storage-system.md), [Pointers](./docs/core-concepts/pointers.md), [Events](./docs/core-concepts/events.md), [Security](./docs/core-concepts/security.md)
110
+ - **Contract Standards**: [OP_NET Base](./docs/contracts/op-net-base.md), [OP20](./docs/contracts/op20-token.md), [OP20S](./docs/contracts/op20s-signatures.md), [OP721](./docs/contracts/op721-nft.md)
111
+ - **Storage Types**: [Stored Primitives](./docs/storage/stored-primitives.md), [Stored Arrays](./docs/storage/stored-arrays.md), [Stored Maps](./docs/storage/stored-maps.md), [Memory Maps](./docs/storage/memory-maps.md)
112
+ - **Advanced**: [Cross-Contract Calls](./docs/advanced/cross-contract-calls.md), [Signature Verification](./docs/advanced/signature-verification.md), [Quantum Resistance](./docs/advanced/quantum-resistance.md)
219
113
 
220
114
  ## Running Tests
221
115
 
222
116
  ```bash
223
- # Run all tests with verbose output
224
117
  npm test
225
-
226
- # Run tests with summary only
227
- npm run test:ci
228
118
  ```
229
119
 
230
- ## Project Structure
231
-
232
- ```
233
- btc-runtime/
234
- ├── runtime/ # Core runtime library
235
- │ ├── contracts/ # Contract base classes (OP_NET, OP20, OP721)
236
- │ ├── storage/ # Storage types (Stored*, Maps)
237
- │ ├── math/ # SafeMath operations
238
- │ ├── types/ # Core types (Address, Calldata)
239
- │ ├── events/ # Event system
240
- │ └── env/ # Blockchain environment
241
- ├── docs/ # Documentation
242
- └── tests/ # Test suite
243
- ```
244
-
245
- ## Key Differences from Solidity
246
-
247
- ### 1. Constructor Behavior
248
-
249
- ```typescript
250
- // OPNet: Constructor runs EVERY time
251
- export class MyContract extends OP_NET {
252
- constructor() {
253
- super();
254
- // DON'T put initialization here - it runs on every call!
255
- }
256
-
257
- // Use this for one-time initialization (like Solidity constructor)
258
- public override onDeployment(calldata: Calldata): void {
259
- // Initialize storage here
260
- }
261
- }
262
- ```
263
-
264
- ### 2. Storage is Explicit
265
-
266
- ```typescript
267
- // Solidity: Implicit storage
268
- // mapping(address => uint256) balances;
269
-
270
- // OPNet: Explicit pointer allocation
271
- class Test {
272
- private balancePointer: u16 = Blockchain.nextPointer;
273
- private balances: AddressMemoryMap<Address, StoredU256> = new AddressMemoryMap(
274
- this.balancePointer,
275
- u256.Zero
276
- );
277
- }
278
- ```
279
-
280
- ### 3. Integer Types
281
-
282
- ```typescript
283
- // OPNet uses u256 from as-bignum (NOT native BigInt)
284
- import { u256 } from '@btc-vision/as-bignum/assembly';
285
-
286
- const a = u256.from(100);
287
- const b = u256.from(50);
288
- const sum = SafeMath.add(a, b); // Always use SafeMath!
289
- ```
290
-
291
- ### 4. No Floating Point
292
-
293
- ```typescript
294
- // WRONG - Non-deterministic!
295
- // const price: f64 = 1.5;
120
+ ## Contributing
296
121
 
297
- // CORRECT - Fixed-point with integers
298
- const PRECISION: u256 = u256.fromU64(1_000_000); // 6 decimals
299
- const price: u256 = SafeMath.mul(amount, PRECISION);
300
- ```
122
+ 1. Fork the repository
123
+ 2. Create a feature branch
124
+ 3. Make your changes
125
+ 4. Run tests: `npm test`
126
+ 5. Submit a pull request
301
127
 
302
- ## Contributing
128
+ See the [pull request template](./.github/PULL_REQUEST_TEMPLATE.md) for requirements.
303
129
 
304
- Contributions are welcome! Please ensure all tests pass before submitting a pull request.
130
+ ## Reporting Issues
305
131
 
306
- ```bash
307
- npm test
308
- ```
132
+ - **Bugs**: Use the [bug report template](https://github.com/btc-vision/btc-runtime/issues/new?template=bug_report.yml)
133
+ - **Security**: See [SECURITY.md](./SECURITY.md) - do not open public issues for vulnerabilities
309
134
 
310
135
  ## License
311
136
 
312
- This project is licensed under the Apache-2.0 License. See [LICENSE](./LICENSE) for details.
137
+ [Apache-2.0](./LICENSE)
313
138
 
314
139
  ## Links
315
140
 
316
- - **Website**: [OPNet](https://opnet.org)
317
- - **Documentation**: [docs/](./docs/)
318
- - **Security**: [SECURITY.md](./SECURITY.md)
319
- - **GitHub**: [btc-vision/btc-runtime](https://github.com/btc-vision/btc-runtime)
320
- - **Issues**: [GitHub Issues](https://github.com/btc-vision/btc-runtime/issues)
321
- - **Auditor**: [Verichains](https://verichains.io)
141
+ - [OPNet](https://opnet.org)
142
+ - [Documentation](./docs/)
143
+ - [GitHub](https://github.com/btc-vision/btc-runtime)
144
+ - [npm](https://www.npmjs.com/package/@btc-vision/btc-runtime)
145
+ - [Verichains](https://verichains.io)
package/SECURITY.md CHANGED
@@ -2,225 +2,72 @@
2
2
 
3
3
  <p align="center">
4
4
  <a href="https://verichains.io">
5
- <img src="https://raw.githubusercontent.com/btc-vision/contract-logo/refs/heads/main/public-assets/verichains.png" alt="Verichains" width="300"/>
5
+ <img src="https://raw.githubusercontent.com/btc-vision/contract-logo/refs/heads/main/public-assets/verichains.png" alt="Verichains" width="150"/>
6
6
  </a>
7
7
  </p>
8
8
 
9
9
  <p align="center">
10
10
  <a href="https://verichains.io">
11
- <img src="https://img.shields.io/badge/Security%20Audit-Verichains-4C35E0?style=for-the-badge&logo=data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAyNCAyNCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cGF0aCBkPSJNMTIgMkw0IDV2Ni41YzAgNS4yNSAzLjQgMTAuMiA4IDExLjUgNC42LTEuMyA4LTYuMjUgOC0xMS41VjVsLTgtM3ptMCAxMC45OVYxOS41Yy0zLjQ1LTEuMTctNS45My00LjgtNi02LjVWNi4zTDEyIDRsMCA4Ljk5eiIgZmlsbD0id2hpdGUiLz48L3N2Zz4=" alt="Audited by Verichains"/>
11
+ <img src="https://img.shields.io/badge/Security%20Audit-Verichains-4C35E0?style=for-the-badge" alt="Audited by Verichains"/>
12
12
  </a>
13
13
  </p>
14
14
 
15
- <p align="center">
16
- <strong>Professionally Audited by <a href="https://verichains.io">Verichains</a></strong>
17
- </p>
18
-
19
15
  ## Audit Status
20
16
 
21
- | Aspect | Status |
22
- |---------------------------|-------------------------------------|
23
- | **Auditor** | [Verichains](https://verichains.io) |
24
- | **Audit Date** | 2025 |
25
- | **Report Status** | Pending Publication |
26
- | **Severity Issues Found** | All resolved |
27
-
28
- ## About the Audit
29
-
30
- The OPNet Smart Contract Runtime has undergone a comprehensive security audit by [Verichains](https://verichains.io), a
31
- leading blockchain security firm with extensive experience in:
32
-
33
- - Smart contract security audits
34
- - Blockchain protocol assessments
35
- - Cryptographic implementation reviews
36
- - WebAssembly security analysis
37
-
38
- ## Audit Scope
39
-
40
- The security audit covered all core components of the btc-runtime:
41
-
42
- ### Contract Standards
43
-
44
- - [x] **OP_NET Base Contract** - Abstract contract class, lifecycle hooks, method dispatching
45
- - [x] **OP20 Token Standard** - Fungible token implementation, transfers, approvals, minting/burning
46
- - [x] **OP20S Signatures** - Gasless approvals, EIP-712 typed signatures, nonce management
47
- - [x] **OP721 NFT Standard** - Non-fungible tokens, ownership, enumeration, metadata
48
- - [x] **ReentrancyGuard** - Reentrancy protection mechanisms (STANDARD and CALLBACK modes)
49
-
50
- ### Storage System
51
-
52
- - [x] **Pointer Architecture** - u16 primary pointers, u256 sub-pointers, SHA256 key hashing
53
- - [x] **Persistent Storage** - StoredU256, StoredString, StoredAddress, StoredBoolean
54
- - [x] **Array Storage** - StoredU256Array through StoredU8Array, bounds checking
55
- - [x] **Map Storage** - StoredMapU256, AddressMemoryMap, MapOfMap nested structures
56
-
57
- ### Cryptographic Operations
58
-
59
- - [x] **Signature Verification** - Schnorr signatures, ML-DSA quantum-resistant signatures
60
- - [x] **Hash Functions** - SHA256, double SHA256 (hash256)
61
- - [x] **EIP-712 Domain Separator** - Typed data signing, replay protection
62
- - [x] **Address Derivation** - P2TR, P2WSH, P2WPKH address generation
63
-
64
- ### Security Mechanisms
65
-
66
- - [x] **SafeMath Operations** - Overflow/underflow protection for u256, u128, u64
67
- - [x] **Access Control** - onlyDeployer patterns, role-based authorization
68
- - [x] **Input Validation** - Calldata parsing, bounds checking, type verification
69
- - [x] **Event System** - 352-byte limit enforcement, proper encoding
70
-
71
- ### Bitcoin Integration
72
-
73
- - [x] **Transaction Parsing** - Input/output decoding, script parsing
74
- - [x] **Address Validation** - Bitcoin address format verification
75
- - [x] **Script Building** - Opcodes, CSV timelocks, witness structures
76
- - [x] **Network Configuration** - Mainnet/testnet handling
17
+ | Component | Status | Auditor |
18
+ |--------------------|---------|---------------------------------------|
19
+ | btc-runtime | Audited | [Verichains](https://verichains.io) |
77
20
 
78
21
  ## Supported Versions
79
22
 
80
- | Version | Supported |
81
- |---------|------------------------|
82
- | 1.10.x | ✅ Current |
83
- | 1.9.x | ⚠️ Upgrade recommended |
84
- | < 1.9.0 | ❌ Not supported |
85
-
86
- ## Security Best Practices
87
-
88
- When developing contracts with btc-runtime, follow these guidelines:
89
-
90
- ### Use SafeMath for All Arithmetic
91
-
92
- ```typescript
93
- import { SafeMath } from '@btc-vision/btc-runtime/runtime';
94
-
95
- // CORRECT: Use SafeMath
96
- const total = SafeMath.add(balance, amount);
97
- const remaining = SafeMath.sub(balance, amount);
98
-
99
- // WRONG: Direct arithmetic can overflow silently
100
- // const total = balance + amount; // DON'T DO THIS
101
- ```
102
-
103
- ### Always Validate Inputs
104
-
105
- ```typescript
106
- class Test extends OP_NET {
107
- public transfer(calldata: Calldata): BytesWriter {
108
- const to = calldata.readAddress();
109
- const amount = calldata.readU256();
110
-
111
- // Validate recipient is not zero address
112
- if (to.equals(Address.zero())) {
113
- throw new Revert('Cannot transfer to zero address');
114
- }
115
-
116
- // Validate amount is positive
117
- if (amount.isZero()) {
118
- throw new Revert('Amount must be greater than zero');
119
- }
120
-
121
- // ... proceed with transfer
122
- }
123
- }
124
- ```
125
-
126
- ### Use Reentrancy Guards
127
-
128
- ```typescript
129
- import { ReentrancyGuard, ReentrancyGuardMode } from '@btc-vision/btc-runtime/runtime';
130
-
131
- @final
132
- export class MyContract extends ReentrancyGuard {
133
- constructor() {
134
- // Use CALLBACK mode for contracts with safe transfer callbacks
135
- super(ReentrancyGuardMode.CALLBACK);
136
- }
137
- }
138
- ```
139
-
140
- ### Implement Access Control
141
-
142
- ```typescript
143
- // Check deployer authorization
144
- this.onlyDeployer(Blockchain.tx.sender);
145
-
146
- // Custom role checks
147
- class Test {
148
- private onlyAdmin(): void {
149
- if (!this.isAdmin(Blockchain.tx.sender)) {
150
- throw new Revert('Caller is not admin');
151
- }
152
- }
153
- }
154
- ```
155
-
156
- ### Handle Cross-Contract Calls Safely
157
-
158
- ```typescript
159
- const result = Blockchain.call(targetContract, calldata, true);
160
-
161
- if (!result.success) {
162
- throw new Revert('External call failed');
163
- }
164
-
165
- // Parse and validate response
166
- const response = result.data;
167
- ```
168
-
169
- ### Never Use Floating-Point Arithmetic
170
-
171
- ```typescript
172
- // WRONG: Floating-point is non-deterministic
173
- // const price = 1.5; // DON'T USE FLOATS
174
-
175
- // CORRECT: Use fixed-point with integers
176
- const PRECISION = u256.fromU64(1_000_000); // 6 decimals
177
- const price = SafeMath.mul(amount, PRECISION);
178
- ```
23
+ | Version | Status |
24
+ |---------|--------------------|
25
+ | 1.11.x | Supported |
26
+ | < 1.10 | Not supported |
179
27
 
180
28
  ## Reporting a Vulnerability
181
29
 
182
- We take security vulnerabilities seriously. If you discover a security issue, please report it responsibly.
183
-
184
- ### How to Report
185
-
186
- 1. **DO NOT** open a public GitHub issue for security vulnerabilities
187
- 2. Report via [GitHub Security Advisories](https://github.com/btc-vision/btc-runtime/security/advisories)
188
- 3. Include detailed steps to reproduce the vulnerability
189
- 4. Allow reasonable time for a fix before public disclosure
30
+ **DO NOT** open a public GitHub issue for security vulnerabilities.
190
31
 
191
- ### What to Include
32
+ Report vulnerabilities through [GitHub Security Advisories](https://github.com/btc-vision/btc-runtime/security/advisories/new).
192
33
 
34
+ Include:
193
35
  - Description of the vulnerability
194
- - Affected component(s) and version(s)
36
+ - Affected version(s)
195
37
  - Steps to reproduce
196
- - Potential impact assessment
38
+ - Potential impact
197
39
  - Suggested fix (if any)
198
- - Proof of concept (if applicable)
199
40
 
200
41
  ### Response Timeline
201
42
 
202
- | Action | Timeframe |
203
- |----------------------------|---------------------|
204
- | Initial response | 48 hours |
205
- | Vulnerability confirmation | 7 days |
206
- | Patch development | 14-30 days |
207
- | Public disclosure | After patch release |
43
+ | Action | Timeframe |
44
+ |--------------------------|-------------------|
45
+ | Initial response | 48 hours |
46
+ | Vulnerability assessment | 7 days |
47
+ | Patch development | 14-30 days |
48
+ | Public disclosure | After patch |
208
49
 
209
- ## Audit Report
50
+ ## Security Scope
210
51
 
211
- The full audit report from Verichains will be published here upon completion of the disclosure process.
52
+ ### In Scope
212
53
 
213
- 📄 **[Audit Report - Coming Soon]**
54
+ - Contract standards (OP_NET, OP20, OP721, OP20S)
55
+ - Storage system (pointers, maps, arrays)
56
+ - Cryptographic operations (Schnorr, ML-DSA, SHA256)
57
+ - SafeMath operations
58
+ - Reentrancy guards
59
+ - Access control mechanisms
60
+ - Event system
61
+ - Cross-contract calls
214
62
 
215
- ## Contact
63
+ ### Out of Scope
216
64
 
217
- - **Security Issues**: [GitHub Security Advisories](https://github.com/btc-vision/btc-runtime/security/advisories)
218
- - **General Questions**: [GitHub Issues](https://github.com/btc-vision/btc-runtime/issues)
219
- - **Website**: [OPNet](https://opnet.org)
220
- - **Auditor**: [Verichains](https://verichains.io)
65
+ - Third-party dependencies (report to respective maintainers)
66
+ - User contract logic errors
67
+ - Issues in development/test environments only
221
68
 
222
- ---
69
+ ## Contact
223
70
 
224
- <p align="center">
225
- <sub>Security is a continuous process. This document will be updated as new audits are completed.</sub>
226
- </p>
71
+ - **Security Issues**: [GitHub Security Advisories](https://github.com/btc-vision/btc-runtime/security/advisories)
72
+ - **General Issues**: [GitHub Issues](https://github.com/btc-vision/btc-runtime/issues)
73
+ - **Website**: [opnet.org](https://opnet.org)