@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.
- package/.eslintrc.yml +4 -0
- package/.turbo/turbo-build.log +17 -0
- package/CHANGELOG.md +283 -0
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/index.d.ts +134 -0
- package/dist/index.js +516 -0
- package/dist/index.js.map +1 -0
- package/package.json +70 -0
- package/src/bip322/bip322-utils.spec.ts +18 -0
- package/src/bip322/bip322-utils.ts +85 -0
- package/src/bip322/sign-message-bip322-bitcoinjs.ts +93 -0
- package/src/bip322/sign-message-bip322.spec.ts +168 -0
- package/src/bip322/sign-message-bip322.ts +54 -0
- package/src/bitcoin-signer.ts +19 -0
- package/src/bitcoin.network.ts +57 -0
- package/src/bitcoin.utils.ts +277 -0
- package/src/index.ts +8 -0
- package/src/p2tr-address-gen.spec.ts +37 -0
- package/src/p2tr-address-gen.ts +57 -0
- package/src/p2wpkh-address-gen.spec.ts +45 -0
- package/src/p2wpkh-address-gen.ts +68 -0
- package/src/p2wsh-p2sh-address-gen.spec.ts +180 -0
- package/src/p2wsh-p2sh-address-gen.ts +64 -0
- package/tsconfig.json +10 -0
- package/tsup.config.ts +9 -0
- package/vitest.config.js +5 -0
|
@@ -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
package/tsup.config.ts
ADDED