Latest 1.3.0
Homepage https://github.com/ArchieR7/BlockChainKit
License MIT
Platforms ios 11.0
Dependencies CryptoSwift, BigInt, secp256k1Converter, BitcoinKit
Authors

CI Status
Version
License
Platform

🔏 BlockChainKit 🔏

The BlockChainKit is a Swift library provides mnemonic generation and validation with 8 languages (🇹🇼🇺🇸🇪🇸🇯🇵🇰🇷🇫🇷🇮🇹🇨🇳),
and it implements NSLinguisticTagger to detect mnemonic language automatically that you do not need to set language 🌍.

There is also providing function to create a raw transaction with Ethereum.RawTransaction both of Ethereum and ERC-20 tokens,
.sign(privateKey: Data, chainID: Int) is working well 💪.

Usage

Create mnemonic

// default is 128 bytes to create mnemonic with 12 words 
let mnemonic = Mnemonic.create()

// create mnemonic with 15, 18, 21, 24 words by different strength
let mnemonicWith160Bytes = Mnemonic.create(strength: .words15)
let mnemonicWith192Bytes = Mnemonic.create(strength: .words18)
let mnemonicWith224Bytes = Mnemonic.create(strength: .words21)
let mnemonicWith256Bytes = Mnemonic.create(strength: .words24)

Validate mnemonic

do {
    try Mnemonic.valdiate(mnemonic)
} catch {
    if let error = error as? MnemonicError {
        switch error {
            case .length: // length should be 12, 15, 18, 21, 24
            case .invalid: // validate mnemonic with check sum
            case let .words(words): // contains invalid words
        }
    }
}

// valdiate with specific language
try Mnemonic.valdiate(mnemonic, wordlist: .English)

Create seed from mnemonic

Mnemonic.createSeed(mnemonic).toHexString()

Create private key and public key from seed

let node = HDNode(seed: seed)
let BIP32RootKey = node.privateExtendedKey()

Base58 encode and decode

do {
    try Base58.encode(node.privateExtendedKey())
} catch {
    if let error = error as? BaseCodableError {
        switch error {
        case .decode: // decode error
        case .encode: // encode error
        }
    }
}

and it provides a protocol to implement encode or decode with specific base.

// you can create a new base with alphabet
struct Base58: BaseCodable {
    static let alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
}

Derived

// BTC - m/44'/0'/0'
let account = node.derived(.hardened(44)).derived(.hardened(0)).derived(.hardened(0))
// BTC - m/44'/0'/0'/0
let chain = account.derived(.notHardened(0))
// BTC - m/44'/0'/0'/0/0
let addressNode = chain.derived(.notHardened(0))
let BTCPrivateKey = addressNode.WIF() 

// ETH - m/44'/60'/0'/0/0
let ETHAccount = node.derived(.hardened(44)).derived(.hardened(60)).derived(.hardened(0))
let ETHPrivateKey = ETHAccount.derived(.notHardened(0)).derived(.notHardened(0)).ethPrivateKey

Create Ethereum address from private key

let address = Ethereum.address(privateKey: privateKey)

Create Bitcoin address from private key

let address = Bitcoin.address(privateKey: privateKey)

Create ETH transaction

let rawTransaction = Ethereum.RawTransaction(nonce: "0x6e",
                                             gasPrice: "0x040000000000",
                                             gasLimit: "0x060000",
                                             toAddress: "0x85b7ca161C311d9A5f0077d5048CAdFace89a267",
                                             value: "0x015950000000000000000000",
                                             data: "")
// chainID supports zero, mainnet = 1, morden = 2, ropsten = 3, rinkeby = 4, goerli = 5, kovan = 42
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md
//
try rawTransaction.sign(privateKey: privateKeyData, chainID: .mainnet)

// Create a BNB
// contract: 0xB8c77482e45F1F44dE1745F52C74426C631bDD52
let ERC20Transaction = Ethereum.RawTransaction(nonce: "0x6e",
                                               gasPrice: "0x040000000000",
                                               gasLimit: "0x060000",
                                               toAddress: "0x85b7ca161C311d9A5f0077d5048CAdFace89a267",
                                               value: "0x015950000000000000000000",
                                               contract: "0xB8c77482e45F1F44dE1745F52C74426C631bDD52")

try ERC20Transaction.sign(privateKey: privateKeyData, chainID: .mainnet)

Feature

  • [x] BIP32
  • [x] BIP39
  • [x] BIP44
  • [x] BIP55
  • [x] signature for ETH transaction
  • [x] signature for ETH ERC-20 transaction
  • [ ] signature for BTC transaction

Requirements

  • iOS 11.0+
  • Xcode 10.2
  • Swift 5.0

Installation

BlockChainKit is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'BlockChainKit', '~> 1.2.0'

Author

Archie, [email protected]

License

BlockChainKit is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "BlockChainKit",
    "version": "1.3.0",
    "summary": "ud83dudcb0A blockchain toolkit with Swift.",
    "description": "The BlockChainKit is a Swift library provides mnemonic generation and validation with 8 languages, and it implements `NSLinguisticTagger` to detect mnemonic language automatically that you do not need to set language ud83cudf0d.",
    "homepage": "https://github.com/ArchieR7/BlockChainKit",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Archie": "https://twitter.com/ChangArchie"
    },
    "source": {
        "git": "https://github.com/ArchieR7/BlockChainKit.git",
        "tag": "1.3.0"
    },
    "social_media_url": "https://twitter.com/ChangArchie",
    "swift_versions": "5.0",
    "platforms": {
        "ios": "11.0"
    },
    "source_files": "BlockChainKit/Classes/**/*",
    "dependencies": {
        "CryptoSwift": [
            "~> 1.0.0"
        ],
        "BigInt": [
            "~> 4.0.0"
        ],
        "secp256k1Converter": [
            "~> 0.1.0"
        ],
        "BitcoinKit": [
            "~> 1.0.2"
        ]
    },
    "swift_version": "5.0"
}

Pin It on Pinterest

Share This