Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Bitcoin SV comes to Swift

License

NotificationsYou must be signed in to change notification settings

wtsnz/SwiftBSV

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...

Installation

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.

CocoaPods

If you are already usingCocoaPods, just add 'SwiftBSV' to yourPodfile then runpod install.

Carthage

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.

Usage Examples

BIP-39: Mnemonic Seeds

letmnemonic=Bip39.create()letseed=Bip39.createSeed(mnemonic: mnemonic)letrootKey=Bip32(seed: seed, network:.mainnet)print(rootKey.address)// 1C8eycEadLHZZfRkchqTC3e72fRNNfbXs3

Bitcoin Signed Messages

Sign a message

letmessage="hello!"letprivateKey=PrivateKey()letaddress= privateKey.addressletsigString=BitcoinSignedMessage.sign(message: message, privateKey: privateKey)print(sigString)// H5wZz9N2+O8oHCMfZBE5nbeM6dr2ZwpD6cKhC0q1lPi/A1t9KV5VO0vTL2kRg8Hg7XSmZl1cviZFj4TkfLAGT9E=

Verify a message

letmessage="hello!"letaddress=Address(fromString:"1D7ZaBLeT3FFr1mcKAWorZHdE18kEVvuaY")!letsigString="IOsRLk8/CBpLvOecpV0kh4ajjgpUH04T3kkJRPJng5kMOe3Az0gwGx2n8dHyooGykrqB6SuMCPtahZ5EN/TcZzg="letvalid=BitcoinSignedMessage.verify(message: message, signature: sigString, address: address)print(valid)// true

Create a Transaction

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

Author

Will Townsend

License

SwiftBSV is available under the MIT license. Seethe LICENSE file for more information.

Thanks to

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:


[8]ページ先頭

©2009-2025 Movatter.jp