Skip to content

Commit

Permalink
Rust: Add more helper functions (#32)
Browse files Browse the repository at this point in the history
* add more rust funcs

* refactor code

* add fn `change_wallet_name`

* Update code

---------

Co-authored-by: Cuong Nguyen <[email protected]>
  • Loading branch information
ljttl3q04t and cuongnv219 authored Dec 30, 2024
1 parent 18e082a commit 2b04d9d
Show file tree
Hide file tree
Showing 10 changed files with 514 additions and 86 deletions.
6 changes: 2 additions & 4 deletions MinWallet/Data/Model/MinWallet+Mapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import Foundation
extension MinWallet: Codable {
public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
//TODO: cuongnv
//walletName = try container.decode(String.self, forKey: .walletName)
walletName = try container.decode(String.self, forKey: .walletName)
address = try container.decode(String.self, forKey: .address)
networkId = try container.decode(UInt32.self, forKey: .networkId)
accountIndex = try container.decode(UInt32.self, forKey: .accountIndex)
Expand All @@ -14,8 +13,7 @@ extension MinWallet: Codable {

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
//TODO: cuongnv
//try container.encode(walletName, forKey: .walletName)
try container.encode(walletName, forKey: .walletName)
try container.encode(address, forKey: .address)
try container.encode(networkId, forKey: .networkId)
try container.encode(accountIndex, forKey: .accountIndex)
Expand Down
1 change: 1 addition & 0 deletions rust/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
bindings
target
ios
.idea
56 changes: 29 additions & 27 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ hex = "0.4.3"
rand = "0.8.5"
uniffi = { version = "0.28.2", features = ["cli"] }
cryptoxide = "0.4.4"
serde = { version = "1.0.215", features = ["derive"] }
serde_json = "1.0.133"

[build-dependencies]
uniffi = { version = "0.28.2", features = ["build"] }
Expand Down
68 changes: 64 additions & 4 deletions rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ let phrase = genPhrase(wordCount: 12)

---

#### `createWallet(phrase: String, password: String, networkEnv: String) -> MinWallet`
#### `createWallet(phrase: String, password: String, networkEnv: String, walletName: String) -> MinWallet`
Creates a wallet based on the provided mnemonic phrase, password, and network environment.

- **Parameters**:
- `phrase`: The mnemonic phrase used to derive the wallet.
- `password`: A user-defined password for encrypting the wallet's private key.
- `networkEnv`: The network environment for the wallet (e.g., `"mainnet"` or `"preprod"`).
- `walletName`: The Name of Wallet.
- **Returns**:
- A `MinWallet` object containing wallet details.

Expand All @@ -44,7 +45,8 @@ Creates a wallet based on the provided mnemonic phrase, password, and network en
let wallet = createWallet(
phrase: "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about",
password: "secure_password",
networkEnv: "preprod"
networkEnv: "preprod",
walletName: "My MinWallet"
)
```

Expand All @@ -60,6 +62,62 @@ Signs a raw transaction using the provided wallet.
- `txRaw`: The raw transaction string to be signed.
- **Returns**:
- A signed transaction `String`.
---

#### `change_password(wallet: MinWallet, current_password: String, new_password: String) -> MinWallet`
Changes the password of a given wallet.

- **Parameters**:
- `wallet`: A `MinWallet` object containing wallet details.
- `current_password`: The current password used to decrypt the wallet.
- `new_password`: The new password to set for the wallet.
- **Returns**:
- An updated `MinWallet` object with the new password applied.

---

#### `verify_password(wallet: MinWallet, password: String) -> boolean`
Verifies whether the provided password matches the wallet's password.

- **Parameters**:
- `wallet`: A `MinWallet` object containing wallet details.
- `password`: The password to be verified.
- **Returns**:
- A `boolean` indicating whether the password is correct (`true`) or incorrect (`false`).

---

#### `export_wallet(wallet: MinWallet, password: String, network_env: String) -> String`
Exports the wallet's data as a string for backup or transfer purposes.

- **Parameters**:
- `wallet`: A `MinWallet` object containing wallet details.
- `password`: The password used to encrypt the exported wallet data.
- `network_env`: The network environment for which the wallet data is exported (e.g., `mainnet` or `testnet`).
- **Returns**:
- A `String` containing the encrypted wallet data.

---

#### `import_wallet(data: String, password: String, wallet_name: String) -> MinWallet`
Imports a wallet from encrypted data.

- **Parameters**:
- `data`: The encrypted wallet data as a string.
- `password`: The password to decrypt the wallet data.
- `wallet_name`: The name to assign to the imported wallet.
- **Returns**:
- A `MinWallet` object representing the imported wallet.

---

#### `get_wallet_name_from_export_wallet(data: String) -> String`
Retrieves the wallet name from exported wallet data.

- **Parameters**:
- `data`: The exported wallet data as a string.
- **Returns**:
- A `String` containing the wallet name.

#### Example:
```swift
Expand All @@ -75,22 +133,24 @@ let signedTx = signTx(

### Types

#### `MinWallet`
### `MinWallet`
A dictionary-like object containing details of a wallet.

- **Fields**:
- `address` (`String`): The wallet's address.
- `networkId` (`UInt32`): The ID of the network the wallet belongs to (mainnet: 764824073, preprod: 1).
- `accountIndex` (`UInt32`): The account index within the wallet.
- `encryptedKey` (`UInt32`): The wallet's encrypted private key.
- `walletName` (`String`): the wallet's name.

#### Example:
```swift
let wallet = MinWallet(
address: "addr_test1...",
networkId: 1,
accountIndex: 0,
encryptedKey: "<encrypted_data>"
encryptedKey: "<encrypted_data>",
walletName: "My MinWallet"
)
```

Expand Down
Loading

0 comments on commit 2b04d9d

Please sign in to comment.