-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.php
132 lines (100 loc) · 3.92 KB
/
utils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
require 'index.php';
class BtcUtils
{
public static BitcoinTOOL $btc;
static public function pubKeyHexToAddress(string $pubKeyHex)
{
$address = $pubKeyHex;
$address = SELF::$btc->getNetworkPrefix() . SELF::$btc->hash160(hex2bin($address));
//checksum
$address = $address . substr(SELF::$btc->hash256d(hex2bin($address)), 0, 8);
$address = Base58::encode($address);
if (SELF::$btc->validateAddress($address))
return $address;
else
throw new \Exception('the generated address seems not to be valid.');
}
static public function pubKeyToAddress(string $pubKey)
{
$address = $pubKey;
$address = SELF::$btc->getNetworkPrefix() . SELF::$btc->hash160($address);
//checksum
$address = $address . substr(SELF::$btc->hash256d(hex2bin($address)), 0, 8);
$address = Base58::encode($address);
if (SELF::$btc->validateAddress($address))
return $address;
else
throw new \Exception('the generated address seems not to be valid.');
}
static public function wifToHex(string $wif)
{
// Decodificar o WIF usando Base58
$decoded = Base58::decode($wif);
if ($decoded === null) {
return null;
}
// O comprimento esperado de uma chave WIF decodificada é de 37 bytes
// Prefixo (1 byte) + Chave privada (32 bytes) + Checksum (4 bytes)
if (strlen($decoded) !== 74) { // 37 bytes em hexadecimal
return null;
}
// Remover o checksum (últimos 4 bytes)
$withoutChecksum = substr($decoded, 0, -8);
// Remover o prefixo da rede (primeiros 2 caracteres)
$privateKeyHex = substr($withoutChecksum, 2);
return $privateKeyHex;
}
static public function hexToWif(string $privateKeyHex, $isCompressed = false, $isMainnet = true)
{
// Passo 1: Prefixo da rede
$prefix = $isMainnet ? '80' : 'ef'; // 80 para mainnet, ef para testnet
$keyWithPrefix = $prefix . $privateKeyHex;
// Passo 2: Adicionar byte de compressão se necessário
if ($isCompressed) {
$keyWithPrefix .= '01';
}
// Passo 3: Calcular o checksum (hash duplo SHA256)
$hash1 = hash('sha256', hex2bin($keyWithPrefix));
$hash2 = hash('sha256', hex2bin($hash1));
$checksum = substr($hash2, 0, 8); // Pegando os primeiros 4 bytes (8 caracteres hexadecimais)
// Passo 4: Adicionar o checksum ao final da chave
$keyWithChecksum = $keyWithPrefix . $checksum;
// Passo 5: Converter de hexadecimal para Base58
$wif = Base58::encode(hex2bin($keyWithChecksum));
return $wif;
}
static public function ripmd160ToAddress(string $ripmd160)
{
$address = SELF::$btc->getNetworkPrefix() . $ripmd160;
//checksum
$address = $address . substr(SELF::$btc->hash256d(hex2bin($address)), 0, 8);
$address = Base58::encode($address);
if (SELF::$btc->validateAddress($address))
return $address;
else
throw new \Exception('the generated address seems not to be valid.');
}
static public function addressToRipemd160(string $address)
{
$address = Base58::decode($address);
$address = substr($address,2,40);
$len =strlen($address);
if ($len ==40)
return $address;
else
throw new \Exception('the address seems not to be valid.');
}
static public function addressToRipmd160native(string $address)
{
$address = base58_decode($address);
$address = bin2hex(substr($address,1,20));
$len =strlen($address);
if ($len ==40)
return $address;
else
return '-----'.$address;
// throw new \Exception('the address seems not to be valid.');
}
}
BtcUtils::$btc = new BitcoinTOOL();