-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaoCrypto.js
More file actions
161 lines (138 loc) · 5.13 KB
/
Copy pathtaoCrypto.js
File metadata and controls
161 lines (138 loc) · 5.13 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
(function (EXPORTS) {
"use strict";
const taoCrypto = EXPORTS;
// --- Multi-chain Generator (BTC, FLO) ---
taoCrypto.generateMultiChain = async function (inputWif) {
const versions = {
BTC: { pub: 0x00, priv: 0x80 },
FLO: { pub: 0x23, priv: 0xa3 },
};
const origBitjsPub = bitjs.pub;
const origBitjsPriv = bitjs.priv;
const origBitjsCompressed = bitjs.compressed;
const origCoinJsCompressed = coinjs.compressed;
bitjs.compressed = true;
coinjs.compressed = true;
let privKeyHex;
let compressed = true;
// --- Decode input or generate new ---
if (typeof inputWif === "string" && inputWif.trim().length > 0) {
if (inputWif.startsWith('0x')) inputWif = inputWif.slice(2);
const hexOnly = /^[0-9a-fA-F]+$/.test(inputWif.trim());
if (hexOnly && (inputWif.length === 64 || inputWif.length === 128)) {
privKeyHex =
inputWif.length === 128 ? inputWif.substring(0, 64) : inputWif;
} else {
try {
const decode = Bitcoin.Base58.decode(inputWif);
const keyWithVersion = decode.slice(0, decode.length - 4);
let key = keyWithVersion.slice(1);
if (key.length >= 33 && key[key.length - 1] === 0x01) {
key = key.slice(0, key.length - 1);
compressed = true;
}
privKeyHex = Crypto.util.bytesToHex(key);
} catch (e) {
throw new Error("Invalid WIF format");
}
}
} else {
throw new Error("Private key is required");
}
// --- Derive addresses for each chain ---
const result = { BTC: {}, FLO: {} };
// BTC
bitjs.pub = versions.BTC.pub;
bitjs.priv = versions.BTC.priv;
const pubKeyBTC = bitjs.newPubkey(privKeyHex);
result.BTC.address = coinjs.bech32Address(pubKeyBTC).address;
result.BTC.privateKey = bitjs.privkey2wif(privKeyHex);
// FLO
bitjs.pub = versions.FLO.pub;
bitjs.priv = versions.FLO.priv;
const pubKeyFLO = bitjs.newPubkey(privKeyHex);
result.FLO.address = bitjs.pubkey2address(pubKeyFLO);
result.FLO.privateKey = bitjs.privkey2wif(privKeyHex);
// restore
bitjs.pub = origBitjsPub;
bitjs.priv = origBitjsPriv;
bitjs.compressed = origBitjsCompressed;
coinjs.compressed = origCoinJsCompressed;
return result;
};
// Helper to extract raw 32-byte private key hex from WIF (FLO or BTC private keys)
function parseWifToHex(wif) {
try {
const { base58Decode, u8aToHex } = window.polkadotCrypto;
const decoded = base58Decode(wif);
// WIF format: 1 byte version + 32 bytes private key + [1 byte 0x01 if compressed] + 4 bytes checksum
if (decoded.length >= 37) {
const rawKey = decoded.slice(1, 33);
return u8aToHex(rawKey);
}
return null;
} catch (e) {
return null;
}
}
/**
* Generate a new TAO wallet
*/
taoCrypto.generateWallet = () => {
const { Keyring, mnemonicGenerate, u8aToHex, mnemonicToMiniSecret } = window.polkadotCrypto;
const keyring = new Keyring({ type: 'sr25519', ss58Format: 42 });
const mnemonic = mnemonicGenerate(12);
// Create a keypair from the mnemonic
const pair = keyring.addFromUri(mnemonic);
return {
address: pair.address,
privateKey: u8aToHex(mnemonicToMiniSecret(mnemonic)),
mnemonic: mnemonic
};
};
/**
* Import an existing TAO wallet from a mnemonic
*/
taoCrypto.importWallet = (mnemonic) => {
try {
const { Keyring, u8aToHex, mnemonicToMiniSecret } = window.polkadotCrypto;
const keyring = new Keyring({ type: 'sr25519', ss58Format: 42 });
const pair = keyring.addFromUri(mnemonic.trim());
return {
address: pair.address,
privateKey: u8aToHex(mnemonicToMiniSecret(mnemonic.trim())),
mnemonic: mnemonic.trim()
};
} catch (err) {
console.error("Import error:", err);
return null;
}
};
/**
* Import an existing TAO wallet from a raw private key hex or WIF (FLO/BTC)
*/
taoCrypto.importWalletFromPrivateKey = (privateKeyInput) => {
try {
const { Keyring } = window.polkadotCrypto;
let hex = privateKeyInput.trim();
// If it looks like a WIF string (Base58, starts with 5, K, L, R, c, etc.)
if (!hex.startsWith('0x') && hex.length > 50) {
const parsedHex = parseWifToHex(hex);
if (parsedHex) {
hex = parsedHex;
}
}
const keyring = new Keyring({ type: 'sr25519', ss58Format: 42 });
hex = hex.startsWith('0x') ? hex : '0x' + hex;
const pair = keyring.addFromUri(hex);
return {
address: pair.address,
privateKey: hex,
mnemonic: null // Cannot derive mnemonic from private key
};
} catch (err) {
console.error("Import error:", err);
return null;
}
};
})("object" === typeof module ? module.exports : (window.taoCrypto = window.taoCrypto || {}));