Skip to content

Commit

Permalink
Update readme & Add missing licence
Browse files Browse the repository at this point in the history
  • Loading branch information
wtsnz committed Jan 19, 2021
1 parent 7ce80f5 commit d474040
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 19 deletions.
20 changes: 20 additions & 0 deletions LICENCE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2012-2021 Will Townsend

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
127 changes: 108 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,13 @@ Todo:

- [x] BInt (Big Number)
- [x] Base58Check
- [ ] Point (Elliptic Curve Points)
- [ ] PublicKeys
- [ ] PrivateKeys
- [ ] Bip39 (Mnemonic)
- [ ] Bip32 (HD Wallets)



## Usage Examples

### BIP-39: Mnemonic Seeds

```swift
let mnemonicString = Bip39.create(strength: strength)
let seed = Bip39.createSeed(mnemonic: mnemonicString)
let privateKey = PrivateKey(seed: seed)
```
- [x] Point (Elliptic Curve Points)
- [x] PublicKeys
- [x] PrivateKeys
- [x] Bip39 (Mnemonic)
- [x] Bip32 (HD Wallets)
- [x] TxBuilder
- [ ] more...


## Installation
Expand All @@ -45,6 +35,105 @@ github "wtsnz/SwiftBSV" ~> 0.1

Then run `carthage update` to build the framework and drag the built `SwiftBSV`.framework into your Xcode project.

## Usage Examples

### BIP-39: Mnemonic Seeds

```swift
let mnemonic = Bip39.create()
let seed = Bip39.createSeed(mnemonic: mnemonic)
let rootKey = Bip32(seed: seed, network: .mainnet)

print(rootKey.address)

// 1C8eycEadLHZZfRkchqTC3e72fRNNfbXs3
```

### Bitcoin Signed Messages

#### Sign a message

```swift
let message = "hello!"
let privateKey = PrivateKey()
let address = privateKey.address
let sigString = BitcoinSignedMessage.sign(message: message, privateKey: privateKey)
print(sigString)

// H5wZz9N2+O8oHCMfZBE5nbeM6dr2ZwpD6cKhC0q1lPi/A1t9KV5VO0vTL2kRg8Hg7XSmZl1cviZFj4TkfLAGT9E=
```

#### Verify a message

```swift
let message = "hello!"
let address = Address(fromString: "1D7ZaBLeT3FFr1mcKAWorZHdE18kEVvuaY")!

let sigString = "IOsRLk8/CBpLvOecpV0kh4ajjgpUH04T3kkJRPJng5kMOe3Az0gwGx2n8dHyooGykrqB6SuMCPtahZ5EN/TcZzg="

let valid = BitcoinSignedMessage.verify(message: message, signature: sigString, address: address)
print(valid)

// true
```

### Create a Transaction

```swift
let privateKey = PrivateKey(data: wallet.privateKey)
let publicKey = privateKey.publicKey
let address = publicKey.address

let txb = TxBuilder()
.setFeePerKb(500)
.setChangeAddress(address)

// Add inputs to the transaction
var numberOfInputs = 0
for utxo in utxos {
let txHashBuf = Data(Data(hex: utxo.txId).reversed())
let txOut = 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 output

var script = 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)
let value = UInt64(payee.amount * 100_000_000)
txb.outputToAddress(value: value, address: Address(fromString: payee.to)!)

// Build, using all the inputs

try! txb.build(useAllInputs: true)

// Finally sign the built transaction
for input in 0..<numberOfInputs {
txb.signInTx(nIn: input, privateKey: privateKey)
}


// Do something with the signed transaction!
txb.transaction

```

## Author

Expand All @@ -53,11 +142,11 @@ Will Townsend

## License

SwiftBSV is available under the MIT license. See [the LICENSE file](LICENSE) for more information.
SwiftBSV is available under the MIT license. See [the LICENSE file](LICENCE.md) for more information.

## Thanks to

This project is a mashup of a bunch of open source projects and wouldn't have been possible without these:
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:

- https://github.com/KevinVitale/WalletKit
- https://github.com/yuzushioh/HDWalletKit
Expand Down

0 comments on commit d474040

Please sign in to comment.