Skip to content

Commit 0e59795

Browse files
committed
script for CB StrKeys
gets started inetgrating f0655fc
1 parent 61ff004 commit 0e59795

5 files changed

Lines changed: 209 additions & 4 deletions

File tree

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/**
2+
* StrKey examples for ALL supported types in js-stellar-base:
3+
* G: ed25519PublicKey
4+
* S: ed25519SecretSeed
5+
* M: med25519PublicKey (muxed account, 32-byte ed25519 + 8-byte ID)
6+
* T: preAuthTx (32-byte hash)
7+
* X: sha256Hash (32-byte hash)
8+
* P: signedPayload (32-byte ed25519 + 4-byte len + payload[4..64])
9+
* C: contract (32-byte)
10+
* L: liquidityPool (32-byte)
11+
* B: claimableBalance (1-byte discriminant + 32-byte hash)
12+
*
13+
* Run:
14+
* npm i stellar-base
15+
* node strkey-examples.mjs
16+
*/
17+
18+
import crypto from 'node:crypto';
19+
import { StrKey, Keypair } from 'stellar-base';
20+
21+
function rb(n) {
22+
return crypto.randomBytes(n);
23+
}
24+
25+
function u32be(n) {
26+
const b = Buffer.alloc(4);
27+
b.writeUInt32BE(n, 0);
28+
return b;
29+
}
30+
31+
function roundTrip(label, encodeFn, decodeFn, isValidFn, raw) {
32+
const enc = encodeFn(raw);
33+
const ok = isValidFn(enc);
34+
const dec = decodeFn(enc);
35+
36+
console.log(`\n== ${label} ==`);
37+
console.log(`encoded: ${enc}`);
38+
console.log(`isValid: ${ok}`);
39+
console.log(`rawLen : ${raw.length}`);
40+
console.log(`decLen : ${dec.length}`);
41+
console.log(`match : ${Buffer.compare(Buffer.from(raw), Buffer.from(dec)) === 0}`);
42+
}
43+
44+
/* ---------------------------
45+
* G / S via Keypair
46+
* -------------------------- */
47+
{
48+
const kp = Keypair.random();
49+
50+
// G: ed25519 public key (32 bytes)
51+
const rawPub = kp.rawPublicKey(); // Buffer(32)
52+
roundTrip(
53+
'G (ed25519PublicKey)',
54+
StrKey.encodeEd25519PublicKey,
55+
StrKey.decodeEd25519PublicKey,
56+
StrKey.isValidEd25519PublicKey,
57+
rawPub
58+
);
59+
60+
// S: ed25519 secret seed (32 bytes)
61+
const rawSeed = kp.rawSecretKey(); // Buffer(32)
62+
roundTrip(
63+
'S (ed25519SecretSeed)',
64+
StrKey.encodeEd25519SecretSeed,
65+
StrKey.decodeEd25519SecretSeed,
66+
StrKey.isValidEd25519SecretSeed,
67+
rawSeed
68+
);
69+
70+
// (Bonus) Show the familiar string forms too
71+
console.log('\nKeypair string forms:');
72+
console.log(`G...: ${kp.publicKey()}`);
73+
console.log(`S...: ${kp.secret()}`);
74+
}
75+
76+
/* ---------------------------
77+
* M: med25519PublicKey
78+
* - 32 bytes ed25519 + 8 bytes ID
79+
* -------------------------- */
80+
{
81+
const rawM = Buffer.concat([rb(32), rb(8)]); // 40 bytes
82+
roundTrip(
83+
'M (med25519PublicKey)',
84+
StrKey.encodeMed25519PublicKey,
85+
StrKey.decodeMed25519PublicKey,
86+
StrKey.isValidMed25519PublicKey,
87+
rawM
88+
);
89+
}
90+
91+
/* ---------------------------
92+
* T: preAuthTx (32 bytes)
93+
* -------------------------- */
94+
{
95+
const rawT = rb(32);
96+
// Note: js-stellar-base doesn’t expose isValidPreAuthTx in the snippet you pasted,
97+
// so we just encode/decode and then validate by decoding.
98+
const enc = StrKey.encodePreAuthTx(rawT);
99+
const dec = StrKey.decodePreAuthTx(enc);
100+
101+
console.log('\n== T (preAuthTx) ==');
102+
console.log(`encoded: ${enc}`);
103+
console.log(`rawLen : ${rawT.length}`);
104+
console.log(`decLen : ${dec.length}`);
105+
console.log(`match : ${Buffer.compare(rawT, dec) === 0}`);
106+
}
107+
108+
/* ---------------------------
109+
* X: sha256Hash (32 bytes)
110+
* -------------------------- */
111+
{
112+
const rawX = rb(32);
113+
const enc = StrKey.encodeSha256Hash(rawX);
114+
const dec = StrKey.decodeSha256Hash(enc);
115+
116+
console.log('\n== X (sha256Hash) ==');
117+
console.log(`encoded: ${enc}`);
118+
console.log(`rawLen : ${rawX.length}`);
119+
console.log(`decLen : ${dec.length}`);
120+
console.log(`match : ${Buffer.compare(rawX, dec) === 0}`);
121+
}
122+
123+
/* ---------------------------
124+
* P: signedPayload
125+
* - 32 bytes signer + 4 bytes payload length + payload (4..64 bytes)
126+
* -------------------------- */
127+
{
128+
const signer = rb(32);
129+
const payload = rb(32); // choose any length 4..64
130+
const rawP = Buffer.concat([signer, u32be(payload.length), payload]);
131+
132+
roundTrip(
133+
'P (signedPayload)',
134+
StrKey.encodeSignedPayload,
135+
StrKey.decodeSignedPayload,
136+
StrKey.isValidSignedPayload,
137+
rawP
138+
);
139+
}
140+
141+
/* ---------------------------
142+
* C: contract (32 bytes)
143+
* -------------------------- */
144+
{
145+
const rawC = rb(32);
146+
roundTrip(
147+
'C (contract)',
148+
StrKey.encodeContract,
149+
StrKey.decodeContract,
150+
StrKey.isValidContract,
151+
rawC
152+
);
153+
}
154+
155+
/* ---------------------------
156+
* L: liquidityPool (32 bytes)
157+
* -------------------------- */
158+
{
159+
const rawL = rb(32);
160+
roundTrip(
161+
'L (liquidityPool)',
162+
StrKey.encodeLiquidityPool,
163+
StrKey.decodeLiquidityPool,
164+
StrKey.isValidLiquidityPool,
165+
rawL
166+
);
167+
}
168+
169+
/* ---------------------------
170+
* B: claimableBalance
171+
* - 1 byte discriminant + 32 bytes hash
172+
* - discriminant is usually 0 for the V0 type
173+
* -------------------------- */
174+
{
175+
const discriminant = Buffer.from([0x00]);
176+
const cbHash = rb(32);
177+
const rawB = Buffer.concat([discriminant, cbHash]); // 33 bytes
178+
179+
roundTrip(
180+
'B (claimableBalance)',
181+
StrKey.encodeClaimableBalance,
182+
StrKey.decodeClaimableBalance,
183+
StrKey.isValidClaimableBalance,
184+
rawB
185+
);
186+
}
187+
188+
/* ---------------------------
189+
* Introspection helpers (from your snippet)
190+
* -------------------------- */
191+
{
192+
const examples = [
193+
Keypair.random().publicKey(), // G...
194+
Keypair.random().secret(), // S...
195+
StrKey.encodeContract(rb(32)), // C...
196+
StrKey.encodeLiquidityPool(rb(32)), // L...
197+
StrKey.encodeClaimableBalance(Buffer.concat([Buffer.from([0]), rb(32)])) // B...
198+
];
199+
200+
console.log('\n== getVersionByteForPrefix examples ==');
201+
for (const s of examples) {
202+
const vb = StrKey.getVersionByteForPrefix(s);
203+
console.log(`${s.slice(0, 1)}... -> versionByte: ${vb}`);
204+
}
205+
}

docs/data/apis/horizon/README.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Running Horizon on your own infrastructure lets you:
2828
- Have full operational control `without dependency on the Stellar Development Foundation` for network data and transaction submission to networks;
2929
- Run multiple instances for redundancy and scalability.
3030

31-
The [SDF](../../../learn/glossary.mdx#the-sdf) runs two instances of Horizon:
31+
The [SDF](../../../learn/glossary.mdx#sdf) runs two instances of Horizon:
3232

3333
- [horizon-testnet.stellar.org](https://horizon-testnet.stellar.org/) for interacting with the [testnet](../../../networks/README.mdx)
3434
- [horizon-futurenet.stellar.org](https://horizon-futurenet.stellar.org/) for interacting with the [futurenet](../../..//README.mdx)

docs/learn/fundamentals/lumens.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ While any address with no signers is counted here, the vast majority of the lume
9797

9898
**`feePool` -** The Fee Pool is where network fees collect. The lumens do not belong to any particular account. No one has access to the fee pool, so these lumens are non-circulating. Network validators could theoretically vote for a protocol change that would affect the fee pool, so we include it in the total supply. Stellar’s transaction fees are extremely low so the fee pool grows very slowly. The Fee Pool is tracked by the protocol itself, and the current number is visible on the [List All Ledgers](../../data/apis/horizon/api-reference/list-all-ledgers.api.mdx) Horizon API endpoint as `_embedded.records.fee_pool`. See all Stellar Mainnet Horizon data providers [here](../../data/apis/horizon/providers.mdx).
9999

100-
** `circulatingSupply` -** The Circulating Supply is lumens in the hands of individuals and independent companies, assumed to be all active balances outside of the SDF Mandate, Upgrade Reserve, or Fee Pool. These lumens transact out in the world, used natively to pay [network fees] and fund Stellar accounts. They are also used as a general medium of exchange. We expect Stellar’s Circulating Supply to grow steadily as [the SDF](../glossary.mdx#the-sdf) spends and distributes lumens according to its Mandate.
100+
** `circulatingSupply` -** The Circulating Supply is lumens in the hands of individuals and independent companies, assumed to be all active balances outside of the SDF Mandate, Upgrade Reserve, or Fee Pool. These lumens transact out in the world, used natively to pay [network fees] and fund Stellar accounts. They are also used as a general medium of exchange. We expect Stellar’s Circulating Supply to grow steadily as [the SDF](../glossary.mdx#sdf) spends and distributes lumens according to its Mandate.
101101

102102
Lumens in the Total Supply, but not in the SDF Mandate, Upgrade Reserve, or Fee Pool
103103

docs/learn/fundamentals/stellar-stack.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ SDF does not provide a publicly available RPC endpoint for Mainnet. Developers s
3737

3838
:::warning
3939

40-
Horizon is nearing end-of-life and will eventually be deprecated in favor of Stellar RPC and [Portfolio APIs](../../data/indexers/README.mdx#portfolio-apis). While it will continue to receive updates to maintain compatibility with upcoming protocol releases, it won't receive new feature development from [the SDF](../glossary.mdx#the-sdf).
40+
Horizon is nearing end-of-life and will eventually be deprecated in favor of Stellar RPC and [Portfolio APIs](../../data/indexers/README.mdx#portfolio-apis). While it will continue to receive updates to maintain compatibility with upcoming protocol releases, it won't receive new feature development from [the SDF](../glossary.mdx#sdf).
4141

4242
:::
4343

docs/learn/glossary.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ An always-on native marketplace for asset conversions. Pay what you want for som
343343

344344
Learn more in our [Liquidity on Stellar section](./fundamentals/liquidity-on-stellar-sdex-liquidity-pools.mdx).
345345

346-
### Stellar Development Foundation (SDF) {#the-sdf}
346+
### Stellar Development Foundation (SDF) {#sdf}
347347

348348
A private taxable nonprofit organization founded to support the development and growth of the Stellar network.
349349

0 commit comments

Comments
 (0)