Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/essentia #45

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions HDWalletKit/Wallet/Coin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public enum Coin {
case litecoin
case bitcoinCash
case dash
case essentia

//https://github.com/satoshilabs/slips/blob/master/slip-0132.md
public var privateKeyVersion: UInt32 {
Expand All @@ -25,6 +26,8 @@ public enum Coin {
return 0x0488ADE4
case .dash:
return 0x02FE52CC
case .essentia:
return 0x0221312b
default:
fatalError("Not implemented")
}
Expand All @@ -39,6 +42,8 @@ public enum Coin {
return 0x00
case .dash:
return 0x4C
case .essentia:
return 0x1e
default:
fatalError("Not implemented")
}
Expand All @@ -53,6 +58,8 @@ public enum Coin {
return 0x05
case .dash:
return 0x10
case .essentia:
return 0x0d
default:
fatalError("Not implemented")
}
Expand All @@ -68,6 +75,8 @@ public enum Coin {
return 0xB0
case .dash:
return 0xCC
case .essentia:
return 0xd4
default:
fatalError("Not implemented")
}
Expand Down Expand Up @@ -99,6 +108,8 @@ public enum Coin {
return 60
case .bitcoinCash:
return 145
case .essentia:
return 11111
}
}

Expand All @@ -112,6 +123,8 @@ public enum Coin {
return "bitcoincash"
case .dash:
return "dash"
case .essentia:
return "essentia"
default: return ""
}
}
Expand Down
1 change: 1 addition & 0 deletions HDWalletKit/Wallet/PrivateKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public struct PrivateKey {
case .bitcoin: fallthrough
case .litecoin: fallthrough
case .dash: fallthrough
case .essentia: fallthrough
case .bitcoinCash:
return self.wifCompressed()
case .ethereum:
Expand Down
3 changes: 2 additions & 1 deletion HDWalletKit/Wallet/PublicKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public struct PublicKey {
case .bitcoin: fallthrough
case .dash: fallthrough
case .bitcoinCash: fallthrough
case .essentia: fallthrough
case .litecoin:
return generateBtcAddress()
case .ethereum:
Expand All @@ -43,7 +44,7 @@ public struct PublicKey {

public var utxoAddress: Address {
switch coin {
case .bitcoin, .litecoin, .dash, .bitcoinCash:
case .bitcoin, .litecoin, .dash, .bitcoinCash, .essentia:
return try! LegacyAddress(address, coin: coin)
case .ethereum:
fatalError("Coin does not support UTXO address")
Expand Down
2 changes: 1 addition & 1 deletion HDWalletKit/Wallet/UTXOBased/UTXOWallet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final public class UTXOWallet {

public convenience init(privateKey: PrivateKey) {
switch privateKey.coin {
case .bitcoin, .litecoin, .dash, .bitcoinCash:
case .bitcoin, .litecoin, .dash, .bitcoinCash, .essentia:
self.init(privateKey: privateKey,
utxoSelector: UtxoSelector(),
utxoTransactionBuilder: UtxoTransactionBuilder(),
Expand Down
9 changes: 9 additions & 0 deletions HDWalletKit/Wallet/UTXOBased/UtxoPrivateKeyType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ public enum UtxoPrivateKeyType {
case .wifUncompressed:
return "^7[rs][1-9A-HJ-NP-Za-km-z]{49}"
}
case .essentia:
switch self {
case .hex:
return "^\\p{XDigit}+$"
case .wifCompressed:
return "[Y][1-9A-HJ-NP-Za-km-z]{51}"
case .wifUncompressed:
return "^8[87][1-9A-HJ-NP-Za-km-z]{49}"
}
}
}

Expand Down
27 changes: 26 additions & 1 deletion HDWalletKit_Tests/AddressGenerationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,27 @@ class AddressGenerationTests: XCTestCase {

}

func testEssentiaAddressGeneration() {
let entropy = Data(hex: "000102030405060708090a0b0c0d0e0f")
let mnemonic = Mnemonic.create(entropy: entropy)
let seed = Mnemonic.createSeed(mnemonic: mnemonic)
let wallet = Wallet(seed: seed, coin: .essentia)

let firstAccount = wallet.generateAccount(at: 0)
XCTAssertEqual(firstAccount.address, "DNzrSF4PEfC4pVdrhNdhz9fy8e8A7zubPF")
XCTAssertEqual(firstAccount.rawPublicKey, "02567f4a520d2974bd3da15346331b8c49778463e2d57a283b42530e4700906c8b")
XCTAssertEqual(firstAccount.rawPrivateKey, "YWNsomfysfAXszQgGHG5cRFutJ7fdRY7fPbQJDwis7gLQc1keb5G")

let secondAddress = wallet.generateAddress(at: 1)
XCTAssertEqual(secondAddress, "DKnzyoFZfREjsJZhRCsaP7VJfJNrAqYYtt")

let thirdAddress = wallet.generateAddress(at: 2)
XCTAssertEqual(thirdAddress, "DE6Z8NhDExUbgoHNC5zv6VnPbZivSM5kch")

let forthAddress = wallet.generateAddress(at: 3)
XCTAssertEqual(forthAddress, "DACjj5NUvsNx63E2bxkbZiyMQLvoAxbg6g")
}

func testBitcoinAddressFromPrivateKeyGeneration() {
let privateKey = PrivateKey(pk: "L35qaFLpbCc9yCzeTuWJg4qWnTs9BaLr5CDYcnJ5UnGmgLo8JBgk", coin: .bitcoin)
XCTAssertEqual(privateKey?.publicKey.address, "128BCBZndgrPXzEgF4QbVR3jnQGwzRtEz5")
Expand All @@ -193,7 +214,11 @@ class AddressGenerationTests: XCTestCase {
func testDashAddressFromPrivateKeyGeneration() {
let privateKey = PrivateKey(pk: "XJV6uhBJu5tu34hjKK2x28t9kpMUmPH4vC9xU4RDA62Yz8oKsKac", coin: .dash)
XCTAssertEqual(privateKey?.publicKey.address, "Xqe9L4R81MhQE4MX3w38zhAJyQSSiZLZXy")
XCTAssertEqual(privateKey?.publicKey.address, "Xqe9L4R81MhQE4MX3w38zhAJyQSSiZLZXy")
}

func testEssentiaAddressFromPrivateKeyGeneration() {
let privateKey = PrivateKey(pk: "YWNsomfysfAXszQgGHG5cRFutJ7fdRY7fPbQJDwis7gLQc1keb5G", coin: .essentia)
XCTAssertEqual(privateKey?.publicKey.address, "DNzrSF4PEfC4pVdrhNdhz9fy8e8A7zubPF")
}

func bip44PrivateKey(coin: Coin , from: PrivateKey) -> PrivateKey {
Expand Down
18 changes: 18 additions & 0 deletions HDWalletKit_Tests/UTXO/UTXOSign.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ class UTXOSign: XCTestCase {
}
}

func testEssentiaSign() {
let pk = PrivateKey(pk: "YWNsomfysfAXszQgGHG5cRFutJ7fdRY7fPbQJDwis7gLQc1keb5G", coin: .essentia)!
let lockingScript: Data = Data(hex: "76a914c3f36d42801567254b137c34051c523e9479959288ac")
let txidData: Data = Data(hex: "1240eb9af29867dc20b8b9df61e1b7be54b06b16352ae0ab8d31f7d05356b458")
let txHash: Data = Data(txidData.reversed())
let output = TransactionOutput(value: 524839, lockingScript: lockingScript)
let outpoint = TransactionOutPoint(hash: txHash, index: 352337565)
let utxo = UnspentTransaction(output: output, outpoint: outpoint)
let address = try! LegacyAddress("DP1BvFnKxTRczynwc8DTWFUK22XQ7pjzWJ", coin: .essentia)
let utxoWallet = UTXOWallet(privateKey: pk)
do {
let signedTx = try utxoWallet.createTransaction(to: address, amount: 10000, utxos: [utxo])
XCTAssertEqual(signedTx, "010000000158b45653d0f7318dabe02a35166bb054beb7e161dfb9b820dc6798f29aeb40129d3e00156a47304402206bd02997ffa76f05c4879d7977d768dbf981723d35e71f00fcd61091badfbb5902201ac76c07c4ca1eb6222d88f62cdd7abd7495714e7953f6825e8f24485b299b10012102567f4a520d2974bd3da15346331b8c49778463e2d57a283b42530e4700906c8bffffffff0210270000000000001976a914c3f36d42801567254b137c34051c523e9479959288ac35da0700000000001976a914c3e329c95a1ba683c5f27edacf2aa19ef4edae0788ac00000000")
} catch {
print(error)
}
}

func testRawTransactionCration() {
let pk = PrivateKey(pk: "L5VqJYoBWVKwe3icNjSGz5maPmAaSm32TEjPdxMNyix8groNubU8", coin: .bitcoin)!
print(pk.publicKey.address)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# HDWalletKit
HDWalletKit is a Swift framwork that enables you to create and use bitcoin HD wallet ([Hierarchical Deterministic Wallets](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki)) in your own app.
<p>
<img src="https://clogos.essdev.info/64x64/bitcoin.png" > <img src="https://clogos.essdev.info/64x64/bitcoin-cash.png" > <img src="https://clogos.essdev.info/64x64/litecoin.png" > <img src="https://clogos.essdev.info/64x64/dash.png" > <img src="https://clogos.essdev.info/64x64/ethereum.png" > <img src="https://clogos.essdev.info/64x64/erc20.png" >
<img src="https://clogos.essdev.info/64x64/essentia.png" > <img src="https://clogos.essdev.info/64x64/bitcoin.png" > <img src="https://clogos.essdev.info/64x64/bitcoin-cash.png" > <img src="https://clogos.essdev.info/64x64/litecoin.png" > <img src="https://clogos.essdev.info/64x64/dash.png" > <img src="https://clogos.essdev.info/64x64/ethereum.png" > <img src="https://clogos.essdev.info/64x64/erc20.png" >
</p>
You can check if the address generation is working right [here](https://iancoleman.io/bip39/).

Expand Down