@wopr-network/platform-core 1.32.0 → 1.33.0
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.
- package/dist/billing/crypto/btc/address-gen.d.ts +15 -4
- package/dist/billing/crypto/btc/address-gen.js +31 -8
- package/dist/billing/crypto/btc/index.d.ts +2 -1
- package/dist/billing/crypto/btc/index.js +1 -1
- package/package.json +1 -1
- package/src/billing/crypto/btc/address-gen.ts +42 -10
- package/src/billing/crypto/btc/index.ts +2 -1
|
@@ -1,8 +1,19 @@
|
|
|
1
|
+
/** Supported UTXO chains for bech32 address derivation. */
|
|
2
|
+
export type UtxoChain = "bitcoin" | "litecoin";
|
|
3
|
+
/** Supported network types. */
|
|
4
|
+
export type UtxoNetwork = "mainnet" | "testnet" | "regtest";
|
|
1
5
|
/**
|
|
2
|
-
* Derive a native segwit (bech32
|
|
6
|
+
* Derive a native segwit (bech32) deposit address from an xpub at a given index.
|
|
7
|
+
* Works for BTC (bc1q...) and LTC (ltc1q...) — same HASH160 + bech32 encoding.
|
|
3
8
|
* Path: xpub / 0 / index (external chain).
|
|
4
9
|
* No private keys involved.
|
|
5
10
|
*/
|
|
6
|
-
export declare function
|
|
7
|
-
/** Derive the
|
|
8
|
-
export declare function
|
|
11
|
+
export declare function deriveAddress(xpub: string, index: number, network?: UtxoNetwork, chain?: UtxoChain): string;
|
|
12
|
+
/** Derive the treasury address (internal chain, index 0). */
|
|
13
|
+
export declare function deriveTreasury(xpub: string, network?: UtxoNetwork, chain?: UtxoChain): string;
|
|
14
|
+
/** @deprecated Use `deriveAddress` instead. */
|
|
15
|
+
export declare const deriveBtcAddress: typeof deriveAddress;
|
|
16
|
+
/** @deprecated Use `deriveTreasury` instead. */
|
|
17
|
+
export declare const deriveBtcTreasury: typeof deriveTreasury;
|
|
18
|
+
/** Validate that a string is an xpub (not xprv). */
|
|
19
|
+
export declare function isValidXpub(key: string): boolean;
|
|
@@ -2,33 +2,56 @@ import { ripemd160 } from "@noble/hashes/legacy.js";
|
|
|
2
2
|
import { sha256 } from "@noble/hashes/sha2.js";
|
|
3
3
|
import { bech32 } from "@scure/base";
|
|
4
4
|
import { HDKey } from "@scure/bip32";
|
|
5
|
+
/** Bech32 HRP (human-readable part) by chain and network. */
|
|
6
|
+
const BECH32_PREFIX = {
|
|
7
|
+
bitcoin: { mainnet: "bc", testnet: "tb", regtest: "bcrt" },
|
|
8
|
+
litecoin: { mainnet: "ltc", testnet: "tltc", regtest: "rltc" },
|
|
9
|
+
};
|
|
10
|
+
function getBech32Prefix(chain, network) {
|
|
11
|
+
return BECH32_PREFIX[chain][network];
|
|
12
|
+
}
|
|
5
13
|
/**
|
|
6
|
-
* Derive a native segwit (bech32
|
|
14
|
+
* Derive a native segwit (bech32) deposit address from an xpub at a given index.
|
|
15
|
+
* Works for BTC (bc1q...) and LTC (ltc1q...) — same HASH160 + bech32 encoding.
|
|
7
16
|
* Path: xpub / 0 / index (external chain).
|
|
8
17
|
* No private keys involved.
|
|
9
18
|
*/
|
|
10
|
-
export function
|
|
19
|
+
export function deriveAddress(xpub, index, network = "mainnet", chain = "bitcoin") {
|
|
11
20
|
if (!Number.isInteger(index) || index < 0)
|
|
12
21
|
throw new Error(`Invalid derivation index: ${index}`);
|
|
13
22
|
const master = HDKey.fromExtendedKey(xpub);
|
|
14
23
|
const child = master.deriveChild(0).deriveChild(index);
|
|
15
24
|
if (!child.publicKey)
|
|
16
25
|
throw new Error("Failed to derive public key");
|
|
17
|
-
// HASH160 = RIPEMD160(SHA256(compressedPubKey))
|
|
18
26
|
const hash160 = ripemd160(sha256(child.publicKey));
|
|
19
|
-
|
|
20
|
-
const prefix = network === "mainnet" ? "bc" : "tb";
|
|
27
|
+
const prefix = getBech32Prefix(chain, network);
|
|
21
28
|
const words = bech32.toWords(hash160);
|
|
22
29
|
return bech32.encode(prefix, [0, ...words]);
|
|
23
30
|
}
|
|
24
|
-
/** Derive the
|
|
25
|
-
export function
|
|
31
|
+
/** Derive the treasury address (internal chain, index 0). */
|
|
32
|
+
export function deriveTreasury(xpub, network = "mainnet", chain = "bitcoin") {
|
|
26
33
|
const master = HDKey.fromExtendedKey(xpub);
|
|
27
34
|
const child = master.deriveChild(1).deriveChild(0); // internal chain
|
|
28
35
|
if (!child.publicKey)
|
|
29
36
|
throw new Error("Failed to derive public key");
|
|
30
37
|
const hash160 = ripemd160(sha256(child.publicKey));
|
|
31
|
-
const prefix = network
|
|
38
|
+
const prefix = getBech32Prefix(chain, network);
|
|
32
39
|
const words = bech32.toWords(hash160);
|
|
33
40
|
return bech32.encode(prefix, [0, ...words]);
|
|
34
41
|
}
|
|
42
|
+
/** @deprecated Use `deriveAddress` instead. */
|
|
43
|
+
export const deriveBtcAddress = deriveAddress;
|
|
44
|
+
/** @deprecated Use `deriveTreasury` instead. */
|
|
45
|
+
export const deriveBtcTreasury = deriveTreasury;
|
|
46
|
+
/** Validate that a string is an xpub (not xprv). */
|
|
47
|
+
export function isValidXpub(key) {
|
|
48
|
+
if (!key.startsWith("xpub"))
|
|
49
|
+
return false;
|
|
50
|
+
try {
|
|
51
|
+
HDKey.fromExtendedKey(key);
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export type { UtxoChain, UtxoNetwork } from "./address-gen.js";
|
|
2
|
+
export { deriveAddress, deriveBtcAddress, deriveBtcTreasury, deriveTreasury } from "./address-gen.js";
|
|
2
3
|
export type { BtcCheckoutDeps, BtcCheckoutResult } from "./checkout.js";
|
|
3
4
|
export { createBtcCheckout, MIN_BTC_USD } from "./checkout.js";
|
|
4
5
|
export { centsToSats, loadBitcoindConfig, satsToCents } from "./config.js";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { deriveBtcAddress, deriveBtcTreasury } from "./address-gen.js";
|
|
1
|
+
export { deriveAddress, deriveBtcAddress, deriveBtcTreasury, deriveTreasury } from "./address-gen.js";
|
|
2
2
|
export { createBtcCheckout, MIN_BTC_USD } from "./checkout.js";
|
|
3
3
|
export { centsToSats, loadBitcoindConfig, satsToCents } from "./config.js";
|
|
4
4
|
export { settleBtcPayment } from "./settler.js";
|
package/package.json
CHANGED
|
@@ -3,15 +3,33 @@ import { sha256 } from "@noble/hashes/sha2.js";
|
|
|
3
3
|
import { bech32 } from "@scure/base";
|
|
4
4
|
import { HDKey } from "@scure/bip32";
|
|
5
5
|
|
|
6
|
+
/** Supported UTXO chains for bech32 address derivation. */
|
|
7
|
+
export type UtxoChain = "bitcoin" | "litecoin";
|
|
8
|
+
|
|
9
|
+
/** Supported network types. */
|
|
10
|
+
export type UtxoNetwork = "mainnet" | "testnet" | "regtest";
|
|
11
|
+
|
|
12
|
+
/** Bech32 HRP (human-readable part) by chain and network. */
|
|
13
|
+
const BECH32_PREFIX = {
|
|
14
|
+
bitcoin: { mainnet: "bc", testnet: "tb", regtest: "bcrt" },
|
|
15
|
+
litecoin: { mainnet: "ltc", testnet: "tltc", regtest: "rltc" },
|
|
16
|
+
} as const;
|
|
17
|
+
|
|
18
|
+
function getBech32Prefix(chain: UtxoChain, network: UtxoNetwork): string {
|
|
19
|
+
return BECH32_PREFIX[chain][network];
|
|
20
|
+
}
|
|
21
|
+
|
|
6
22
|
/**
|
|
7
|
-
* Derive a native segwit (bech32
|
|
23
|
+
* Derive a native segwit (bech32) deposit address from an xpub at a given index.
|
|
24
|
+
* Works for BTC (bc1q...) and LTC (ltc1q...) — same HASH160 + bech32 encoding.
|
|
8
25
|
* Path: xpub / 0 / index (external chain).
|
|
9
26
|
* No private keys involved.
|
|
10
27
|
*/
|
|
11
|
-
export function
|
|
28
|
+
export function deriveAddress(
|
|
12
29
|
xpub: string,
|
|
13
30
|
index: number,
|
|
14
|
-
network:
|
|
31
|
+
network: UtxoNetwork = "mainnet",
|
|
32
|
+
chain: UtxoChain = "bitcoin",
|
|
15
33
|
): string {
|
|
16
34
|
if (!Number.isInteger(index) || index < 0) throw new Error(`Invalid derivation index: ${index}`);
|
|
17
35
|
|
|
@@ -19,23 +37,37 @@ export function deriveBtcAddress(
|
|
|
19
37
|
const child = master.deriveChild(0).deriveChild(index);
|
|
20
38
|
if (!child.publicKey) throw new Error("Failed to derive public key");
|
|
21
39
|
|
|
22
|
-
// HASH160 = RIPEMD160(SHA256(compressedPubKey))
|
|
23
40
|
const hash160 = ripemd160(sha256(child.publicKey));
|
|
24
|
-
|
|
25
|
-
// Bech32 encode: witness version 0 + 20-byte hash
|
|
26
|
-
const prefix = network === "mainnet" ? "bc" : "tb";
|
|
41
|
+
const prefix = getBech32Prefix(chain, network);
|
|
27
42
|
const words = bech32.toWords(hash160);
|
|
28
43
|
return bech32.encode(prefix, [0, ...words]);
|
|
29
44
|
}
|
|
30
45
|
|
|
31
|
-
/** Derive the
|
|
32
|
-
export function
|
|
46
|
+
/** Derive the treasury address (internal chain, index 0). */
|
|
47
|
+
export function deriveTreasury(xpub: string, network: UtxoNetwork = "mainnet", chain: UtxoChain = "bitcoin"): string {
|
|
33
48
|
const master = HDKey.fromExtendedKey(xpub);
|
|
34
49
|
const child = master.deriveChild(1).deriveChild(0); // internal chain
|
|
35
50
|
if (!child.publicKey) throw new Error("Failed to derive public key");
|
|
36
51
|
|
|
37
52
|
const hash160 = ripemd160(sha256(child.publicKey));
|
|
38
|
-
const prefix = network
|
|
53
|
+
const prefix = getBech32Prefix(chain, network);
|
|
39
54
|
const words = bech32.toWords(hash160);
|
|
40
55
|
return bech32.encode(prefix, [0, ...words]);
|
|
41
56
|
}
|
|
57
|
+
|
|
58
|
+
/** @deprecated Use `deriveAddress` instead. */
|
|
59
|
+
export const deriveBtcAddress = deriveAddress;
|
|
60
|
+
|
|
61
|
+
/** @deprecated Use `deriveTreasury` instead. */
|
|
62
|
+
export const deriveBtcTreasury = deriveTreasury;
|
|
63
|
+
|
|
64
|
+
/** Validate that a string is an xpub (not xprv). */
|
|
65
|
+
export function isValidXpub(key: string): boolean {
|
|
66
|
+
if (!key.startsWith("xpub")) return false;
|
|
67
|
+
try {
|
|
68
|
+
HDKey.fromExtendedKey(key);
|
|
69
|
+
return true;
|
|
70
|
+
} catch {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export type { UtxoChain, UtxoNetwork } from "./address-gen.js";
|
|
2
|
+
export { deriveAddress, deriveBtcAddress, deriveBtcTreasury, deriveTreasury } from "./address-gen.js";
|
|
2
3
|
export type { BtcCheckoutDeps, BtcCheckoutResult } from "./checkout.js";
|
|
3
4
|
export { createBtcCheckout, MIN_BTC_USD } from "./checkout.js";
|
|
4
5
|
export { centsToSats, loadBitcoindConfig, satsToCents } from "./config.js";
|