Skip to content

Commit aa7e892

Browse files
TinyChain --> docs to Secp256k1 added.
1 parent dc92942 commit aa7e892

File tree

4 files changed

+665
-19
lines changed

4 files changed

+665
-19
lines changed

docs/TinyChain/README.md

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 📦 TinyChain: Documentation Overview
22

3-
Welcome to the **TinyChain** documentation!
3+
Welcome to the **TinyChain** documentation!
44
This repository provides detailed information on the core structure and behavior of each major component in the system.
55

66
If you're just getting started or looking to understand specific internals, here's a quick breakdown of the available documentation files:
@@ -9,13 +9,13 @@ If you're just getting started or looking to understand specific internals, here
99

1010
## 🔹 [`Block`](./Block.md)
1111

12-
This file explains everything about individual blocks within the TinyChain.
12+
This file explains everything about individual blocks within the TinyChain.
1313
It covers how blocks are created, mined, validated, and how transaction IDs are generated and managed.
1414

15-
> **Includes:**
16-
> - Mining logic
17-
> - Transaction indexing
18-
> - Data serialization
15+
> **Includes:**
16+
> - Mining logic
17+
> - Transaction indexing
18+
> - Data serialization
1919
> - Cryptographic helpers
2020
2121
Use this file when you're working with block-level operations or modifying how transactions are stored and processed inside a block.
@@ -24,14 +24,14 @@ Use this file when you're working with block-level operations or modifying how t
2424

2525
## 🔸 [`Instance`](./Instance.md)
2626

27-
This is the heart of the chain – the instance that controls everything.
27+
This is the heart of the chain – the instance that controls everything.
2828
The `Instance.md` covers the full lifecycle of the TinyChain instance, including how chains are created, validated, forked, and how consensus is managed.
2929

30-
> **Includes:**
31-
> - Chain state and structure
32-
> - Import/export
33-
> - Validation process
34-
> - Fork handling
30+
> **Includes:**
31+
> - Chain state and structure
32+
> - Import/export
33+
> - Validation process
34+
> - Fork handling
3535
> - Gas and fee behavior
3636
3737
Refer to this file if you're implementing node behavior or syncing with other chains.
@@ -40,25 +40,62 @@ Refer to this file if you're implementing node behavior or syncing with other ch
4040

4141
## 🎯 [`Events`](./Events.md)
4242

43-
Want to listen for events like mining success, forks, or new transactions?
43+
Want to listen for events like mining success, forks, or new transactions?
4444
This file lists all the available **event names** you can use with your chain or block listeners.
4545

46-
> **Includes:**
47-
> - Event naming conventions
48-
> - When events are emitted
46+
> **Includes:**
47+
> - Event naming conventions
48+
> - When events are emitted
4949
> - Typical payloads
5050
5151
---
5252

53+
## 🔐 [`Secp256k1`](./Secp256k1/README.md)
54+
55+
Covers the generic elliptic curve cryptography used in TinyChain.
56+
This is the base implementation shared by other curve wrappers like Bitcoin and Ethereum.
57+
58+
> **Includes:**
59+
> - Key pair management
60+
> - Public/private key access
61+
> - Message signing and recovery
62+
63+
---
64+
65+
## 🪙 [`Secp256k1 - BTC`](./Secp256k1/Btc.md)
66+
67+
Bitcoin-style cryptography and address handling.
68+
Extends the base secp256k1 with functions specific to Bitcoin, including `Hash160` and address checksum logic.
69+
70+
> **Includes:**
71+
> - BTC address generation
72+
> - Signature formatting
73+
> - Base58Check support
74+
75+
---
76+
77+
## 🦊 [`Secp256k1 - ETH`](./Secp256k1/Eth.md)
78+
79+
Ethereum-specific cryptographic logic.
80+
Supports `keccak256`, Ethereum signature prefixing, and checksum validation.
81+
82+
> **Includes:**
83+
> - Ethereum message signing
84+
> - Address derivation
85+
> - Checksum-based validation
86+
87+
---
88+
5389
## 📚 How to Use This
5490

55-
If you're working with block internals → start with [`Block`](./Block.md).
56-
If you're managing or extending the blockchain as a whole → check [`Instance`](./Instance.md).
91+
If you're working with block internals → start with [`Block`](./Block.md).
92+
If you're managing or extending the blockchain as a whole → check [`Instance`](./Instance.md).
5793
And for anything that involves event hooks and listeners → open [`Events`](./Events.md).
94+
To implement or customize cryptography → explore [`Secp256k1`](./Secp256k1/README.md), [`BTC`](./Secp256k1/Btc.md), or [`ETH`](./Secp256k1/Eth.md).
5895

5996
---
6097

61-
Feel free to explore, extend, and fork the system!
98+
Feel free to explore, extend, and fork the system!
6299
Happy hacking 🛠️
63100

64101
— TinyChain Core Docs

docs/TinyChain/Secp256k1/Btc.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# 📘 `TinyBtcSecp256k1` – Bitcoin Key & Address Toolkit Built on `TinySecp256k1`
2+
3+
`TinyBtcSecp256k1` is an advanced Bitcoin-oriented wrapper class built on top of the generic `TinySecp256k1`. It enables easy management of private/public keys and address generation for Bitcoin (`p2pkh`, `bech32`) using the `secp256k1` elliptic curve. It supports lazy-loaded modules like `bech32` and `bs58check`, ensuring lightweight usage.
4+
5+
---
6+
7+
## 🛠 Constructor
8+
9+
```js
10+
new TinyBtcSecp256k1(options)
11+
```
12+
13+
**Options:**
14+
15+
| Name | Type | Default | Description | |
16+
| -------------------- | ----------------------- | ----------------------------- | -------------------------------------------------- | ------------------------------------------------ |
17+
| `type` | `'bech32'` or `'p2pkh'` | `'bech32'` | Type of address to generate. | |
18+
| `prefix` | `string` | `'bc'` | Network prefix (e.g., `'bc'` for Bitcoin mainnet). | |
19+
| `msgPrefix` | `string` | `'Bitcoin Signed Message:\n'` | Prefix used when signing messages. | |
20+
| `p2pkhPrefix` | `number` | `0x00` | Prefix used for legacy base58 addresses. | |
21+
| `privateKey` | \`string | null\` | `null` | Optional private key to initialize the key pair. |
22+
| `privateKeyEncoding` | `BufferEncoding` | `'hex'` | Encoding used for the private key string. | |
23+
24+
---
25+
26+
## 🔑 Key & Address Utilities
27+
28+
### `getP2pkhPrefix() → number`
29+
30+
Returns the current P2PKH prefix.
31+
32+
---
33+
34+
### `init() → Promise<KeyPair>`
35+
36+
Initializes the key pair using the provided private key.
37+
38+
---
39+
40+
### `getPublicKeyHex(compressed = true) → string`
41+
42+
Returns the public key in hexadecimal format.
43+
44+
---
45+
46+
### `getAddress(pubKey?, type?) → string`
47+
48+
Derives the public address from a given public key.
49+
50+
---
51+
52+
### `addressToVanilla(address, type?) → Buffer`
53+
54+
Returns the `hash160` (vanilla) representation of a Bitcoin address.
55+
56+
---
57+
58+
## 📦 Module Lazy Loaders
59+
60+
### `fetchBech32() → Promise<{ base32: Bech32, base32m: Bech32 }>`
61+
62+
Dynamically loads and caches the `bech32` module.
63+
64+
---
65+
66+
### `fetchBs58check() → Promise<Bs58check>`
67+
68+
Dynamically loads and caches the `bs58check` module.
69+
70+
---
71+
72+
### `getBech32() → Bech32`
73+
74+
Returns the initialized Bech32 encoder instance.
75+
76+
---
77+
78+
### `getBech32m() → Bech32`
79+
80+
Returns the initialized Bech32m encoder instance.
81+
82+
---
83+
84+
### `getBs58check() → Bs58check`
85+
86+
Returns the initialized Bs58check encoder instance.
87+
88+
---
89+
90+
## 📬 Address Converters
91+
92+
These methods are stored internally in private mappings (`#pubKeyTo`, `#toHash160`):
93+
94+
### From Public Key → Address:
95+
96+
* `p2pkh(pubKey: Buffer) → string`
97+
* `bech32(pubKey: Buffer) → string`
98+
99+
---
100+
101+
### From Address → hash160:
102+
103+
* `p2pkh(address: string) → Buffer`
104+
* `bech32(address: string) → Buffer`
105+
106+
---
107+
108+
## 📦 Dependencies (expected to be installed):
109+
110+
```bash
111+
npm install bech32 bs58check elliptic
112+
```
113+
114+
---
115+
116+
## ✅ Example
117+
118+
```js
119+
const keyUtil = new TinyBtcSecp256k1({ privateKey: '...', type: 'p2pkh' });
120+
await keyUtil.init();
121+
console.log(keyUtil.getPublicKeyHex());
122+
console.log(keyUtil.getAddress());
123+
```

0 commit comments

Comments
 (0)