-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathbinance.html
More file actions
129 lines (118 loc) · 5.04 KB
/
Copy pathbinance.html
File metadata and controls
129 lines (118 loc) · 5.04 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Binance BNB Cold Wallet</title>
<link rel="stylesheet" href="css/style2.css">
<!-- Binance BNB addresses use secp256k1 and bech32 (bnb...) -->
<script src="js/elliptic-hash-bech32.min.js"></script>
<script src="js/qrcode.js"></script>
<script src="js/bip39.min.js"></script>
</head>
<body onload="generate()">
<div class="container">
<h1>Binance BNB Cold Wallet</h1>
<div class="button-group">
<button onclick="window.location.href='index.html'">Home</button>
<button onclick="generate()">Generate</button>
<button onclick="window.print()">Print</button>
</div>
<div class="info-box">
<label>Public Address (SHARE)</label>
<span id="public">Click Generate to create wallet</span>
<div id="public_qr" class="qr-code"></div>
</div>
<div class="info-box">
<label>Mnemonic Seed (SECRET)</label>
<span id="secret">Click Generate to create wallet</span>
<div id="secret_qr" class="qr-code"></div>
</div>
</div>
<script>
// Utility: RIPEMD160
async function ripemd160(buffer) {
if (typeof window.crypto.subtle === 'undefined') {
alert('Browser does not support crypto.subtle');
return null;
}
// Use a library for RIPEMD160. Here we use 'hash.js' if available.
if (typeof hash !== 'undefined' && hash.ripemd160) {
return new Uint8Array(hash.ripemd160().update(buffer).digest());
} else {
alert('RIPEMD160 is required (try hash.js)');
return null;
}
}
// Utility: Bech32 encode
function toWords(bytes) {
// from bech32 library
return bech32.bech32.toWords(bytes);
}
function hexToUint8Array(hex) {
if (hex.startsWith('0x')) hex = hex.slice(2);
if (hex.length % 2 !== 0) hex = '0' + hex;
const arr = [];
for (let i = 0; i < hex.length; i += 2) {
arr.push(parseInt(hex.substring(i, i + 2), 16));
}
return new Uint8Array(arr);
}
async function generate() {
if (typeof elliptic === 'undefined' || typeof bech32 === 'undefined' || typeof bip39 === 'undefined') {
alert('A required library is not loaded!');
return;
}
// 1. Generate mnemonic
const mnemonic = bip39.generateMnemonic(256); // 24-word phrase
// 2. Get seed from mnemonic
const seed = bip39.mnemonicToSeedSync(mnemonic); // Buffer
// 3. Use first 32 bytes as private key
const privKeyHex = Array.from(seed.slice(0, 32)).map(x => x.toString(16).padStart(2, '0')).join('');
const ec = new elliptic.ec('secp256k1');
const key = ec.keyFromPrivate(privKeyHex, 'hex');
const pub = key.getPublic(true, 'hex'); // compressed public key hex
const pubBytes = hexToUint8Array(pub);
// Hash: SHA256 then RIPEMD160
const sha256 = await window.crypto.subtle.digest('SHA-256', pubBytes);
const sha256Bytes = new Uint8Array(sha256);
// RIPEMD160 (needs hash.js library)
let ripemdBytes = await ripemd160(sha256Bytes);
if (!ripemdBytes) {
document.getElementById('public').textContent = 'RIPEMD160 is not available. Add hash.js for full support.';
return;
}
// Bech32 encode
const words = toWords(ripemdBytes);
const address = bech32.bech32.encode('bnb', words);
// DOM elements
const publicElem = document.getElementById("public");
const mnemonicElem = document.getElementById("secret");
const publicQRElem = document.getElementById("public_qr");
const mnemonicQRElem = document.getElementById("secret_qr");
if (publicElem && mnemonicElem && publicQRElem && mnemonicQRElem) {
publicElem.textContent = address;
mnemonicElem.textContent = mnemonic;
publicQRElem.innerHTML = "";
mnemonicQRElem.innerHTML = "";
new QRCode(publicQRElem, {
text: address,
width: 100,
height: 100,
colorDark: "#e0e0e0",
colorLight: "#1e1e1e",
correctLevel: QRCode.CorrectLevel.H
});
new QRCode(mnemonicQRElem, {
text: mnemonic,
width: 100,
height: 100,
colorDark: "#e0e0e0",
colorLight: "#1e1e1e",
correctLevel: QRCode.CorrectLevel.H
});
}
}
</script>
</body>
</html>