kaspa-sdk 0.2.0

A fully functional implementation SDK for the Kaspa Network, covering most of the functionality that interoperates with the Kaspa Network.
Documentation
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/// This module contains the wallet interoperability module related functions of the kaspa network.
use crate::types::{KaspaError, NetworkType, Result};
use crate::types::{
    Outpoint, ScriptPublicKey, Transaction, TransactionInput, TransactionOutput, UtxoEntry,
};
use bech32::{self, ToBase32, Variant};
use ripemd::Ripemd160;
use secp256k1::rand;
use secp256k1::{Message, PublicKey, Secp256k1, SecretKey, ecdsa::Signature};
use sha2::{Digest, Sha256};
use std::collections::{HashMap, HashSet};

const MAIN_NET_ID: &str = "0000000000000000000000000000000000000000";

pub struct KaspaKeypair {
    pub secret_key: SecretKey,
    pub public_key: PublicKey,
}

pub struct Wallet {
    keys: HashMap<String, KaspaKeypair>,    // address -> keypair
    utxos: HashMap<String, Vec<UtxoEntry>>, // address -> utxos
    addresses: HashSet<String>,
    network: NetworkType,
}

impl Wallet {
    pub fn new(network: NetworkType) -> Self {
        Self {
            keys: HashMap::new(),
            utxos: HashMap::new(),
            addresses: HashSet::new(),
            network,
        }
    }

    // create new address
    pub fn create_new_address(&mut self) -> Result<String> {
        let secp = Secp256k1::new();
        let (secret_key, public_key) = secp.generate_keypair(&mut rand::thread_rng());
        let keypair = KaspaKeypair {
            secret_key,
            public_key,
        };
        let address = self.public_key_to_kaspa_address(&public_key)?;
        self.keys.insert(address.clone(), keypair);
        self.addresses.insert(address.clone());
        Ok(address)
    }

    /// public key to kaspa address
    fn public_key_to_kaspa_address(&self, public_key: &PublicKey) -> Result<String> {
        let public_key_bytes = public_key.serialize();
        let sha256_hash = Sha256::digest(&public_key_bytes);
        let ripemd160_hash = Ripemd160::digest(&sha256_hash);
        // byte version
        let byte_version = match self.network {
            NetworkType::Mainnet => 0x00,
            NetworkType::Testnet => 0x6f,
            NetworkType::Devnet => 0x6f,
            NetworkType::Simnet => 0x6f,
        };
        let mut payload = Vec::with_capacity(21);
        payload.push(byte_version);
        payload.extend_from_slice(&ripemd160_hash);
        let checksum = Self::calculate_checksum(&payload);
        payload.extend_from_slice(&checksum[..4]);
        // Base58
        let address = bs58::encode(&payload).into_string();
        Ok(address)
    }

    // calculate Base58 check sum
    fn calculate_checksum(data: &[u8]) -> [u8; 4] {
        let first_sha = Sha256::digest(data);
        let second_sha = Sha256::digest(&first_sha);
        let mut checksum = [0u8; 4];
        checksum.copy_from_slice(&second_sha[..4]);
        checksum
    }

    // create Bech32 address
    fn public_key_to_bech32_address(&self, public_key: &PublicKey) -> Result<String> {
        let public_key_bytes = public_key.serialize();
        // SHA256 + RIPEMD160
        let sha256_hash = Sha256::digest(&public_key_bytes);
        let ripemd160_hash = Ripemd160::digest(&sha256_hash);
        // bech32
        let hrp = match self.network {
            NetworkType::Mainnet => "kaspa",
            NetworkType::Testnet => "kaspatest",
            NetworkType::Devnet => "kaspadev",
            NetworkType::Simnet => "kaspasim",
        };
        let encoded = bech32::encode(hrp, ripemd160_hash.to_base32(), Variant::Bech32)
            .map_err(|e| KaspaError::Custom(format!("Bech32 encoding failed: {}", e)))?;
        Ok(encoded)
    }

    // import private key
    pub fn import_private_key(&mut self, private_key_hex: &str) -> Result<String> {
        let private_key_bytes = hex::decode(private_key_hex)
            .map_err(|e| KaspaError::Custom(format!("Invalid hex: {}", e)))?;
        let secret_key = SecretKey::from_slice(&private_key_bytes)
            .map_err(|e| KaspaError::Custom(format!("Invalid private key: {}", e)))?;
        let secp = Secp256k1::new();
        let public_key = PublicKey::from_secret_key(&secp, &secret_key);
        let keypair = KaspaKeypair {
            secret_key,
            public_key,
        };
        let address = self.public_key_to_kaspa_address(&public_key)?;
        self.keys.insert(address.clone(), keypair);
        self.addresses.insert(address.clone());
        Ok(address)
    }

    /// export private key
    pub fn export_private_key(&self, address: &str) -> Result<String> {
        let keypair = self
            .keys
            .get(address)
            .ok_or_else(|| KaspaError::Custom("Address not found in wallet".to_string()))?;
        Ok(hex::encode(keypair.secret_key.secret_bytes()))
    }

    /// get balance
    pub fn get_balance(&self, address: &str) -> u64 {
        self.utxos
            .get(address)
            .map(|utxos| utxos.iter().map(|utxo| utxo.amount).sum())
            .unwrap_or(0)
    }

    // get total balance
    pub fn get_total_balance(&self) -> u64 {
        self.addresses
            .iter()
            .map(|addr| self.get_balance(addr))
            .sum()
    }

    // add utxo
    pub fn add_utxo(&mut self, address: &str, utxo: UtxoEntry) {
        self.utxos
            .entry(address.to_string())
            .or_insert_with(Vec::new)
            .push(utxo);
    }

    /// remove utxo
    pub fn remove_utxo(&mut self, address: &str, outpoint: &Outpoint) -> Result<()> {
        if let Some(utxos) = self.utxos.get_mut(address) {
            utxos.retain(|utxo| true);
        }
        Ok(())
    }

    /// build transaction
    pub fn build_transaction(
        &self,
        from_address: &str,
        to_address: &str,
        amount: u64,
        fee_per_input: u64,
    ) -> Result<Transaction> {
        let keypair = self
            .keys
            .get(from_address)
            .ok_or_else(|| KaspaError::Custom("From address not found".to_string()))?;
        let utxos = self
            .utxos
            .get(from_address)
            .ok_or_else(|| KaspaError::Custom("No UTXOs for address".to_string()))?;
        let (selected_indices, total_input) = self.select_utxos(utxos, amount, fee_per_input)?;
        let selected_utxos: Vec<&UtxoEntry> = selected_indices
            .iter()
            .map(|&index| &utxos[index])
            .collect();
        let total_fee = selected_utxos.len() as u64 * fee_per_input;
        let change = total_input
            .checked_sub(amount + total_fee)
            .ok_or_else(|| KaspaError::Custom("Insufficient balance".to_string()))?;
        // build treade input
        let inputs = self.build_inputs(&selected_utxos)?;
        // build treade output
        let outputs = self.build_outputs(to_address, amount, from_address, change)?;
        // create transaction
        let transaction = Transaction {
            version: 0,
            inputs,
            outputs,
            lock_time: 0,
            subnetwork_id: MAIN_NET_ID.to_string(), // Main net id
            gas: 0,
            payload: "".to_string(),
            verbose_data: None,
        };
        Ok(transaction)
    }

    /// select UTXOs
    fn select_utxos(
        &self,
        utxos: &[UtxoEntry],
        amount: u64,
        fee_per_input: u64,
    ) -> Result<(Vec<usize>, u64)> {
        let mut selected_indices = Vec::new();
        let mut total = 0u64;
        let mut indices: Vec<usize> = (0..utxos.len()).collect();
        indices.sort_by_key(|&i| utxos[i].amount);
        for index in indices {
            selected_indices.push(index);
            total += utxos[index].amount;
            let estimated_fee = selected_indices.len() as u64 * fee_per_input;
            if total >= amount + estimated_fee {
                break;
            }
        }
        if total < amount {
            return Err(KaspaError::Custom("Insufficient balance".to_string()));
        }
        Ok((selected_indices, total))
    }

    /// build inputs
    fn build_inputs(&self, utxos: &[&UtxoEntry]) -> Result<Vec<TransactionInput>> {
        let mut inputs = Vec::new();
        for (index, utxo) in utxos.iter().enumerate() {
            let input = TransactionInput {
                previous_outpoint: Outpoint {
                    transaction_id: format!("tx_{}", index),
                    index: 0,
                },
                signature_script: "".to_string(),
                sequence: 0xFFFFFFFF,
                sig_op_count: 1,
            };
            inputs.push(input);
        }
        Ok(inputs)
    }

    /// build outputs
    fn build_outputs(
        &self,
        to_address: &str,
        amount: u64,
        change_address: &str,
        change_amount: u64,
    ) -> Result<Vec<TransactionOutput>> {
        let mut outputs = Vec::new();
        let to_script = self.address_to_script(to_address)?;
        outputs.push(TransactionOutput {
            value: amount,
            script_public_key: to_script,
            verbose_data: None,
        });
        if change_amount > 0 {
            let change_script = self.address_to_script(change_address)?;
            outputs.push(TransactionOutput {
                value: change_amount,
                script_public_key: change_script,
                verbose_data: None,
            });
        }
        Ok(outputs)
    }

    /// address to script
    fn address_to_script(&self, address: &str) -> Result<ScriptPublicKey> {
        let script = format!(
            "76a914{}88ac",
            hex::encode(Sha256::digest(address.as_bytes()))
        );
        Ok(ScriptPublicKey {
            version: 0,
            script_public_key: script,
        })
    }

    /// sign message
    pub fn sign_message(&self, address: &str, message: &str) -> Result<String> {
        let keypair = self
            .keys
            .get(address)
            .ok_or_else(|| KaspaError::Custom("Address not found".to_string()))?;

        let secp = Secp256k1::new();
        let message_hash = Sha256::digest(message.as_bytes());
        let message = Message::from_slice(&message_hash)
            .map_err(|e| KaspaError::Custom(format!("Invalid message hash: {}", e)))?;
        let signature = secp.sign_ecdsa(&message, &keypair.secret_key);
        Ok(hex::encode(signature.serialize_compact()))
    }

    /// verify message
    pub fn verify_message(
        &self,
        address: &str,
        message: &str,
        signature_hex: &str,
    ) -> Result<bool> {
        let keypair = self
            .keys
            .get(address)
            .ok_or_else(|| KaspaError::Custom("Address not found".to_string()))?;
        let signature_bytes = hex::decode(signature_hex)
            .map_err(|e| KaspaError::Custom(format!("Invalid signature hex: {}", e)))?;
        let secp = Secp256k1::new();
        let message_hash = Sha256::digest(message.as_bytes());
        let message = Message::from_slice(&message_hash)
            .map_err(|e| KaspaError::Custom(format!("Invalid message hash: {}", e)))?;
        let signature = Signature::from_compact(&signature_bytes)
            .map_err(|e| KaspaError::Custom(format!("Invalid signature: {}", e)))?;
        Ok(secp
            .verify_ecdsa(&message, &signature, &keypair.public_key)
            .is_ok())
    }

    // Whether the address belongs to the current wallet
    pub fn is_my_address(&self, address: &str) -> bool {
        self.addresses.contains(address)
    }

    // get all address
    pub fn get_all_address(&self) -> Vec<String> {
        self.addresses.iter().cloned().collect()
    }

    // backup wallet
    pub fn backup_wallet(&self) -> WalletBackup {
        let mut key_backups = Vec::new();
        for (address, keypair) in &self.keys {
            key_backups.push(KeyBackup {
                address: address.clone(),
                private_key: hex::encode(keypair.secret_key.secret_bytes()),
                public_key: hex::encode(keypair.public_key.serialize()),
            });
        }
        WalletBackup {
            network: self.network,
            keys: key_backups,
        }
    }

    // restore from backup
    pub fn restore_from_backup(backup: WalletBackup) -> Result<Self> {
        let mut wallet = Wallet::new(backup.network);
        for key_backup in backup.keys {
            wallet.import_private_key(&key_backup.private_key)?;
        }
        Ok(wallet)
    }
}

/// wallet backup struct
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct WalletBackup {
    pub network: NetworkType,
    pub keys: Vec<KeyBackup>,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct KeyBackup {
    pub address: String,
    pub private_key: String,
    pub public_key: String,
}

impl<'de> serde::Deserialize<'de> for NetworkType {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        match s.as_str() {
            "mainnet" => Ok(NetworkType::Mainnet),
            "testnet" => Ok(NetworkType::Testnet),
            "devnet" => Ok(NetworkType::Devnet),
            "simnet" => Ok(NetworkType::Simnet),
            _ => Err(serde::de::Error::custom("Invalid network type")),
        }
    }
}

// HD wallet
pub struct HDWallet {
    base_wallet: Wallet,
    seed: [u8; 64],
    derivation_path: String,
    key_index: u32,
}

impl HDWallet {
    pub fn new(network: NetworkType, seed_phrase: &str) -> Result<Self> {
        let seed = Self::phrase_to_seed(seed_phrase);
        Ok(Self {
            base_wallet: Wallet::new(network),
            seed,
            derivation_path: "m/44'/111111'/0'/0".to_string(), // Kaspa BIP44 path
            key_index: 0,
        })
    }

    pub fn generate_next_address(&mut self) -> Result<String> {
        let derived_key = self.derive_key(self.key_index)?;
        let address = self
            .base_wallet
            .public_key_to_kaspa_address(&derived_key.public_key)?;
        self.base_wallet.keys.insert(address.clone(), derived_key);
        self.base_wallet.addresses.insert(address.clone());
        self.key_index += 1;
        Ok(address)
    }

    fn derive_key(&self, index: u32) -> Result<KaspaKeypair> {
        let secp = Secp256k1::new();
        let mut hasher = Sha256::new();
        hasher.update(&self.seed);
        hasher.update(&index.to_be_bytes());
        let hash_result = hasher.finalize();
        let secret_key = SecretKey::from_slice(&hash_result)
            .map_err(|e| KaspaError::Custom(format!("Key derivation failed: {}", e)))?;
        let public_key = PublicKey::from_secret_key(&secp, &secret_key);
        Ok(KaspaKeypair {
            secret_key,
            public_key,
        })
    }

    fn phrase_to_seed(phrase: &str) -> [u8; 64] {
        let mut seed = [0u8; 64];
        let phrase_bytes = phrase.as_bytes();
        let len = phrase_bytes.len().min(64);
        seed[..len].copy_from_slice(&phrase_bytes[..len]);
        seed
    }
}