This repository has been archived by the owner on May 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
Segwit Support #49
Merged
Merged
Segwit Support #49
Changes from 27 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
330dee0
add matchPayToScriptHash
hewigovens e311151
Add toP2SH test
hewigovens ceb058b
Add testSpendP2SHTx
hewigovens f921daf
Bitcoin signing improvements
alejandro-isaza 45d66cc
Add segwit address test
vikmeup c6e3170
Add dropfirst
vikmeup 837c940
Add sewgit address test
vikmeup 97e38b7
Support custom redeem scripts
alejandro-isaza dc2aa7e
Merge branch 'master' into unlock-p2sh
alejandro-isaza 485483b
Merge branch 'master' of github.com:TrustWallet/trust-core
hewigovens 107bde0
Merge branch 'master' into unlock-p2sh
hewigovens e8fd6ce
Merge branch 'master' of github.com:TrustWallet/trust-core
hewigovens 107c4d3
merge master and fix some tests
hewigovens dbc5b51
P2WPKH
alejandro-isaza 4eaf223
Fix isPayToWitnessScriptHash checking
hewigovens b8ce3be
SegWit progress
alejandro-isaza 64ebf48
Fix signature problems
alejandro-isaza 8e8cb87
Fix P2WPKH
alejandro-isaza 2d688a3
merge master
hewigovens 1997a7e
P2WSH
alejandro-isaza c17e7c6
Remove unnecessary lines
alejandro-isaza 2a1222f
Add P2SH-P2WPKH test case
hewigovens 4c37f8b
Fix scripthash in testSignP2SH_P2WPKH
hewigovens 8e99c03
pass P2SH-P2WPKH test case
hewigovens c59ab26
Fix P2WSH, refactor signing code.
alejandro-isaza bf2ee92
Cleanup
alejandro-isaza 3aed675
Fix tests failed on iPhone 5 (32bit), append(Data) not working somehow
hewigovens b5d3c04
try xcode 10
hewigovens 736eb8e
More cleanup
alejandro-isaza 65cf26a
Add P2SH-P2WSH test, fix multisig
alejandro-isaza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright © 2017-2018 Trust. | ||
// | ||
// This file is part of Trust. The full Trust copyright notice, including | ||
// terms governing use, modification, and redistribution, is contained in the | ||
// file LICENSE at the root of the source code distribution tree. | ||
|
||
import Foundation | ||
|
||
public protocol BitcoinPrivateKeyProvider { | ||
/// Should return the private key for a public key hash | ||
func key(forPublicKeyHash: Data) -> PrivateKey? | ||
|
||
/// Should return the private key for a script hash | ||
func key(forScriptHash: Data) -> PrivateKey? | ||
|
||
/// Should return the full redeem script for a script hash | ||
func script(forScriptHash: Data) -> BitcoinScript? | ||
} | ||
|
||
extension BitcoinPrivateKeyProvider { | ||
func key(forPublicKey pubkey: PublicKey) -> PrivateKey? { | ||
return key(forPublicKeyHash: pubkey.bitcoinKeyHash) | ||
} | ||
} | ||
|
||
public final class BitcoinDefaultPrivateKeyProvider: BitcoinPrivateKeyProvider { | ||
public var keys: [PrivateKey] | ||
public var keysByScriptHash = [Data: PrivateKey]() | ||
public var scriptsByScriptHash = [Data: BitcoinScript]() | ||
|
||
public init(keys: [PrivateKey]) { | ||
self.keys = keys | ||
} | ||
|
||
public func key(forPublicKeyHash hash: Data) -> PrivateKey? { | ||
return keys.first { key in | ||
let publicKey = key.publicKey(compressed: true) | ||
return publicKey.bitcoinKeyHash == hash | ||
} | ||
} | ||
|
||
public func key(forScriptHash hash: Data) -> PrivateKey? { | ||
return keysByScriptHash[hash] | ||
} | ||
|
||
public func script(forScriptHash hash: Data) -> BitcoinScript? { | ||
return scriptsByScriptHash[hash] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright © 2017-2018 Trust. | ||
// | ||
// This file is part of Trust. The full Trust copyright notice, including | ||
// terms governing use, modification, and redistribution, is contained in the | ||
// file LICENSE at the root of the source code distribution tree. | ||
|
||
import Foundation | ||
|
||
public extension BitcoinScript { | ||
/// Returns a single operation at the given index including its operand. | ||
/// | ||
/// - Parameters: | ||
/// - index: index where the operation starts, on return the index of the next operation. | ||
/// - Returns: a tuple with the opcode and its operand or `nil` if's the end of the script or the script is invalid. | ||
func getScriptOp(index: inout Int) -> (opcode: UInt8, operand: Data?)? { | ||
// Read instruction | ||
if index >= bytes.endIndex { | ||
return nil | ||
} | ||
|
||
let opcode = bytes[index] | ||
index += 1 | ||
|
||
if opcode > OpCode.OP_PUSHDATA4 { | ||
return (opcode: opcode, operand: nil) | ||
} | ||
|
||
// Immediate operand | ||
var size = 0 | ||
if opcode < OpCode.OP_PUSHDATA1 { | ||
size = Int(opcode) | ||
} else if opcode == OpCode.OP_PUSHDATA1 { | ||
if bytes.endIndex - index < 1 { | ||
return nil | ||
} | ||
size = index | ||
index += 1 | ||
} else if opcode == OpCode.OP_PUSHDATA2 { | ||
if bytes.endIndex - index < 2 { | ||
return nil | ||
} | ||
size = Int(readLE16(at: index)) | ||
index += 2 | ||
} else if opcode == OpCode.OP_PUSHDATA4 { | ||
if bytes.endIndex - index < 4 { | ||
return nil | ||
} | ||
size = Int(readLE32(at: index)) | ||
index += 4 | ||
} | ||
if bytes.endIndex - index < size { | ||
return nil | ||
} | ||
index += size | ||
|
||
return (opcode: opcode, operand: data[index ..< index + size]) | ||
} | ||
|
||
/// Reads a little-endian `UInt16` from the script. | ||
private func readLE16(at index: Int) -> UInt16 { | ||
return bytes.withUnsafeBufferPointer { ptr in | ||
(ptr.baseAddress! + index).withMemoryRebound(to: UInt16.self, capacity: 1) { intptr in | ||
UInt16(littleEndian: intptr.pointee) | ||
} | ||
} | ||
} | ||
|
||
/// Reads a little-endian `UInt32` from the script. | ||
private func readLE32(at index: Int) -> UInt32 { | ||
return bytes.withUnsafeBufferPointer { ptr in | ||
(ptr.baseAddress! + index).withMemoryRebound(to: UInt32.self, capacity: 1) { intptr in | ||
UInt32(littleEndian: intptr.pointee) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why this fixes anything 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Swift mystery.
Remember we had similar bug? Data wasn’t converted properly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right