@leather.io/bitcoin 0.8.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.
@@ -0,0 +1,64 @@
1
+ import { ripemd160 } from '@noble/hashes/ripemd160';
2
+ import { sha256 } from '@noble/hashes/sha256';
3
+ import { base58check } from '@scure/base';
4
+
5
+ import { deriveBip39MnemonicFromSeed, deriveRootBip32Keychain } from '@leather.io/crypto';
6
+ import { NetworkModes } from '@leather.io/models';
7
+
8
+ /**
9
+ * @deprecated
10
+ * Use `deriveBip39MnemonicFromSeed` from `@leather.io/crypto`
11
+ */
12
+ export const deriveBtcBip49SeedFromMnemonic = deriveBip39MnemonicFromSeed;
13
+
14
+ /**
15
+ * @deprecated
16
+ * Use `deriveRootBip32Keychain` from `@leather.io/crypto`
17
+ */
18
+ export const deriveRootBtcKeychain = deriveRootBip32Keychain;
19
+
20
+ export function decodeCompressedWifPrivateKey(key: string) {
21
+ // https://en.bitcoinwiki.org/wiki/Wallet_import_format
22
+ // Decode Compressed WIF format private key
23
+ const compressedWifFormatPrivateKey = base58check(sha256).decode(key);
24
+ // Drop leading network byte, trailing public key SEC format byte
25
+ return compressedWifFormatPrivateKey.slice(1, compressedWifFormatPrivateKey.length - 1);
26
+ }
27
+
28
+ // https://en.bitcoin.it/wiki/List_of_address_prefixes
29
+ const payToScriptHashMainnetPrefix = 0x05;
30
+ export const payToScriptHashTestnetPrefix = 0xc4;
31
+
32
+ const payToScriptHashPrefixMap: Record<NetworkModes, number> = {
33
+ mainnet: payToScriptHashMainnetPrefix,
34
+ testnet: payToScriptHashTestnetPrefix,
35
+ };
36
+
37
+ function hash160(input: Uint8Array) {
38
+ return ripemd160(sha256(input));
39
+ }
40
+
41
+ export function makePayToScriptHashKeyHash(publicKey: Uint8Array) {
42
+ return hash160(publicKey);
43
+ }
44
+
45
+ export function makePayToScriptHashAddressBytes(keyHash: Uint8Array) {
46
+ const redeemScript = Uint8Array.from([
47
+ ...Uint8Array.of(0x00),
48
+ ...Uint8Array.of(keyHash.length),
49
+ ...keyHash,
50
+ ]);
51
+ return hash160(redeemScript);
52
+ }
53
+
54
+ export function makePayToScriptHashAddress(addressBytes: Uint8Array, network: NetworkModes) {
55
+ const networkByte = payToScriptHashPrefixMap[network];
56
+ const addressWithPrefix = Uint8Array.from([networkByte, ...addressBytes]);
57
+ return base58check(sha256).encode(addressWithPrefix);
58
+ }
59
+
60
+ export function publicKeyToPayToScriptHashAddress(publicKey: Uint8Array, network: NetworkModes) {
61
+ const hash = makePayToScriptHashKeyHash(publicKey);
62
+ const addrBytes = makePayToScriptHashAddressBytes(hash);
63
+ return makePayToScriptHashAddress(addrBytes, network);
64
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": ["@leather.io/tsconfig-config/tsconfig.base.json"],
3
+ "include": ["**/*"],
4
+ "exclude": ["./dist"],
5
+ "compilerOptions": {
6
+ "baseUrl": "src",
7
+ "types": ["vitest/globals"],
8
+ "outDir": "./dist"
9
+ }
10
+ }
package/tsup.config.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { defineConfig } from 'tsup';
2
+
3
+ export default defineConfig({
4
+ entry: ['src/index.ts'],
5
+ sourcemap: true,
6
+ clean: true,
7
+ dts: true,
8
+ format: 'esm',
9
+ });
@@ -0,0 +1,5 @@
1
+ import { defineConfig } from 'vitest/config';
2
+
3
+ import { defaultVitestUnitTestingConfig } from '../../config/vitest-configs';
4
+
5
+ export default defineConfig({ ...defaultVitestUnitTestingConfig });