- Notifications
You must be signed in to change notification settings - Fork1
wtsnz/SwiftBSV
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A library to help you create Bitcoin transactions in Swift.
The goal is to mimic thehttps://github.com/moneybutton/bsv/ lib as close as possible. I've used multiple existing open source Bitcoin Swift libraries as a starting point. This project is in active development and will continue to change significantly.
Todo:
- BInt (Big Number)
- Base58Check
- Point (Elliptic Curve Points)
- PublicKeys
- PrivateKeys
- Bip39 (Mnemonic)
- Bip32 (HD Wallets)
- TxBuilder
- more...
Add this to your project using Swift Package Manager. In Xcode that is simply: File > Swift Packages > Add Package Dependency... and you're done. Alternative installations options are shown below for legacy projects.
If you are already usingCocoaPods, just add 'SwiftBSV' to yourPodfile
then runpod install
.
If you are already usingCarthage, just add to yourCartfile
:
github "wtsnz/SwiftBSV" ~> 0.1
Then runcarthage update
to build the framework and drag the builtSwiftBSV
.framework into your Xcode project.
letmnemonic=Bip39.create()letseed=Bip39.createSeed(mnemonic: mnemonic)letrootKey=Bip32(seed: seed, network:.mainnet)print(rootKey.address)// 1C8eycEadLHZZfRkchqTC3e72fRNNfbXs3
letmessage="hello!"letprivateKey=PrivateKey()letaddress= privateKey.addressletsigString=BitcoinSignedMessage.sign(message: message, privateKey: privateKey)print(sigString)// H5wZz9N2+O8oHCMfZBE5nbeM6dr2ZwpD6cKhC0q1lPi/A1t9KV5VO0vTL2kRg8Hg7XSmZl1cviZFj4TkfLAGT9E=
letmessage="hello!"letaddress=Address(fromString:"1D7ZaBLeT3FFr1mcKAWorZHdE18kEVvuaY")!letsigString="IOsRLk8/CBpLvOecpV0kh4ajjgpUH04T3kkJRPJng5kMOe3Az0gwGx2n8dHyooGykrqB6SuMCPtahZ5EN/TcZzg="letvalid=BitcoinSignedMessage.verify(message: message, signature: sigString, address: address)print(valid)// true
letprivateKey=PrivateKey(data: wallet.privateKey)letpublicKey= privateKey.publicKeyletaddress= publicKey.addresslettxb=TxBuilder().setFeePerKb(500).setChangeAddress(address)// Add inputs to the transactionvarnumberOfInputs=0forutxoin utxos{lettxHashBuf=Data(Data(hex: utxo.txId).reversed())lettxOut=TransactionOutput( value: utxo.satoshis, lockingScript:Data(hex: utxo.script)) txb.inputFromPubKeyHash( txHashBuffer: txHashBuf, txOutNum: utxo.outputIndex, txOut: txOut, pubKey: publicKey) numberOfInputs= numberOfInputs+1}// Add a data outputvarscript=try!Script().append(.OP_FALSE).append(.OP_RETURN).appendData("hello, world!".data(using:.utf8)!)txb.outputToScript( value:0, script: script)// Add an output to another Address (pay-to-pubkey-hash)letvalue=UInt64(payee.amount*100_000_000)txb.outputToAddress(value: value, address:Address(fromString: payee.to)!)// Build, using all the inputstry! txb.build(useAllInputs:true)// Finally sign the built transactionforinputin0..<numberOfInputs{ txb.signInTx(nIn: input, privateKey: privateKey)}// Do something with the signed transaction!txb.transaction
Will Townsend
SwiftBSV is available under the MIT license. Seethe LICENSE file for more information.
This project is a mashup of a bunch of open source projects, and many original contributions and wouldn't have been possible without the following projects:
About
Bitcoin SV comes to Swift