diff --git a/BitcoinKit/Sources/BitcoinKit/Mock/MockHelper.swift b/BitcoinKit/Sources/BitcoinKit/Mock/MockHelper.swift deleted file mode 100644 index 4180aadfe..000000000 --- a/BitcoinKit/Sources/BitcoinKit/Mock/MockHelper.swift +++ /dev/null @@ -1,126 +0,0 @@ -//// -//// MockHelper.swift -//// -//// Copyright © 2018 BitcoinKit developers -//// -//// 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. -//// -// -//import Foundation -// -//public struct MockHelper { -// -// public static func createUtxo(lockScript: Script) -> UnspentTransaction { -// let outputMock = TransactionOutput(value: 100_000_000, lockingScript: lockScript.data) -// let outpointMock = TransactionOutPoint(hash: Data(), index: 0) -// return UnspentTransaction(output: outputMock, outpoint: outpointMock) -// } -// -// public static func createTransaction(utxo: UnspentTransaction) -> Transaction { -// let toAddress: Address = try! AddressFactory.create("1Bp9U1ogV3A14FMvKbRJms7ctyso4Z4Tcx") -// let changeAddress: Address = try! AddressFactory.create("1FQc5LdgGHMHEN9nwkjmz6tWkxhPpxBvBU") -// // 1. inputs -// let unsignedInputs = [TransactionInput(previousOutput: utxo.outpoint, -// signatureScript: Data(), -// sequence: UInt32.max)] -// -// // 2. outputs -// // 2-1. amount, change, fee -// let amount: UInt64 = 10_000 -// let fee: UInt64 = 1000 -// let change: UInt64 = utxo.output.value - amount - fee -// -// // 2-2. Script -// let lockingScriptTo = Script(address: toAddress)! -// let lockingScriptChange = Script(address: changeAddress)! -// -// // 2-3. TransactionOutput -// let toOutput = TransactionOutput(value: amount, lockingScript: lockingScriptTo.data) -// let changeOutput = TransactionOutput(value: change, lockingScript: lockingScriptChange.data) -// -// // 3. Tx -// let tx = Transaction(version: 1, inputs: unsignedInputs, outputs: [toOutput, changeOutput], lockTime: 0) -// return tx -// } -// -// public static func updateTransaction(_ tx: Transaction, unlockScriptData: Data) -> Transaction { -// let i = 0 -// var inputs = tx.inputs -// -// // Sequence may need to be updated -// let txin = inputs[i] -// inputs[i] = TransactionInput(previousOutput: txin.previousOutput, -// signatureScript: unlockScriptData, -// sequence: txin.sequence) -// -// return Transaction(version: tx.version, -// inputs: inputs, -// outputs: tx.outputs, -// lockTime: tx.lockTime) -// } -// -// public static func verifySingleKey(lockScript: Script, unlockScriptBuilder: MockUnlockScriptBuilder, key: MockKey, verbose: Bool = true) throws -> Bool { -// // mocks -// let utxoMock: UnspentTransaction = MockHelper.createUtxo(lockScript: lockScript) -// let txMock: Transaction = MockHelper.createTransaction(utxo: utxoMock) -// -// // signature, unlockScript(scriptSig) -// let hashType = SighashType.BCH.ALL -// let signature: Data = key.privkey.sign(txMock, utxoToSign: utxoMock, hashType: hashType) -// let sigWithHashType: Data = signature + UInt8(hashType) -// let pair: SigKeyPair = SigKeyPair(sigWithHashType, key.pubkey) -// let unlockScript: Script = unlockScriptBuilder.build(pairs: [pair]) -// // signed tx -// let signedTxMock = MockHelper.updateTransaction(txMock, unlockScriptData: unlockScript.data) -// -// // context -// let context = ScriptExecutionContext(transaction: signedTxMock, utxoToVerify: utxoMock.output, inputIndex: 0)! -// context.verbose = verbose -// -// // script test -// return try ScriptMachine.verify(lockScript: lockScript, unlockScript: unlockScript, context: context) -// } -// -// public static func verifyMultiKey(lockScript: Script, unlockScriptBuilder: MockUnlockScriptBuilder, keys: [MockKey], verbose: Bool = true) throws -> Bool { -// // mocks -// let utxoMock: UnspentTransaction = MockHelper.createUtxo(lockScript: lockScript) -// let txMock: Transaction = MockHelper.createTransaction(utxo: utxoMock) -// -// // signature, unlockScript(scriptSig) -// let hashType = SighashType.BCH.ALL -// var sigKeyPairs: [SigKeyPair] = [] -// for key in keys { -// let signature: Data = key.privkey.sign(txMock, utxoToSign: utxoMock, hashType: hashType) -// let sigWithHashType: Data = signature + UInt8(hashType) -// sigKeyPairs.append(SigKeyPair(sigWithHashType, key.pubkey)) -// } -// -// let unlockScript: Script = unlockScriptBuilder.build(pairs: sigKeyPairs) -// // signed tx -// let signedTxMock = MockHelper.updateTransaction(txMock, unlockScriptData: unlockScript.data) -// -// // context -// let context = ScriptExecutionContext(transaction: signedTxMock, utxoToVerify: utxoMock.output, inputIndex: 0)! -// context.verbose = verbose -// -// // script test -// return try ScriptMachine.verify(lockScript: lockScript, unlockScript: unlockScript, context: context) -// } -// -//} diff --git a/BitcoinKit/Sources/BitcoinKit/Mock/MockKey.swift b/BitcoinKit/Sources/BitcoinKit/Mock/MockKey.swift deleted file mode 100644 index 4f72f15cc..000000000 --- a/BitcoinKit/Sources/BitcoinKit/Mock/MockKey.swift +++ /dev/null @@ -1,70 +0,0 @@ -// -// MockKey.swift -// -// Copyright © 2018 BitcoinKit developers -// -// 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. -// - -import Foundation - -public class MockKey { - public static let keyA: MockKey = MockKey(wif: "L1WFAgk5LxC5NLfuTeADvJ5nm3ooV3cKei5Yi9LJ8ENDfGMBZjdW") - public static let keyB: MockKey = MockKey(wif: "L1hpUWE7R8NsYcREtS9DJPdvnjSRK7X8fatvhH6mStiXxvGTLkdi") - public static let keyC: MockKey = MockKey(wif: "KxHkyFWVPKZE9ZrYpNmRhfLFxr6TYwXELvcSTdMtZKMzZm95e7KR") - public static let keyD: MockKey = MockKey(wif: "L31xG1KVupuVJJ6Fc6VzorCr9FaZz7TBQx7saAMBmdjTK8oL3yzB") - - private var wif: String! - public var privkey: PrivateKey { - return try! PrivateKey(wif: wif) - } - public var pubkey: PublicKey { - return privkey.publicKey() - } - public var pubkeyHash: Data { - return pubkey.hashP2pkh - } - - private init(wif: String) { - self.wif = wif - } -} - -extension MockKey: CustomStringConvertible { - public var description: String { - switch wif { - case "L1WFAgk5LxC5NLfuTeADvJ5nm3ooV3cKei5Yi9LJ8ENDfGMBZjdW": - return "keyA" - case "L1hpUWE7R8NsYcREtS9DJPdvnjSRK7X8fatvhH6mStiXxvGTLkdi": - return "keyB" - case "KxHkyFWVPKZE9ZrYpNmRhfLFxr6TYwXELvcSTdMtZKMzZm95e7KR": - return "keyC" - case "L31xG1KVupuVJJ6Fc6VzorCr9FaZz7TBQx7saAMBmdjTK8oL3yzB": - return "keyD" - default: - return "UnknownKey" - } - } -} - -extension MockKey: Equatable { - public static func == (lhs: MockKey, rhs: MockKey) -> Bool { - return lhs.wif == rhs.wif - } -} diff --git a/BitcoinKit/Sources/BitcoinKit/Mock/MockUnlockScriptBuilder.swift b/BitcoinKit/Sources/BitcoinKit/Mock/MockUnlockScriptBuilder.swift deleted file mode 100644 index 0b4312c46..000000000 --- a/BitcoinKit/Sources/BitcoinKit/Mock/MockUnlockScriptBuilder.swift +++ /dev/null @@ -1,33 +0,0 @@ -// -// MockUnlockScriptBuilder.swift -// -// Copyright © 2018 BitcoinKit developers -// -// 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. -// - -import Foundation - -public typealias SigKeyPair = (signature: Data, key: PublicKey) - -// This protocol is prepared for playing with Script. -// If you are building production application, you should implement TransactionSigner instead of MockUnlockScriptBuilder. -public protocol MockUnlockScriptBuilder { - func build(pairs: [SigKeyPair]) -> Script -} diff --git a/BitcoinKit/Sources/BitcoinKit/Scripts/Script.swift b/BitcoinKit/Sources/BitcoinKit/Scripts/Script.swift index 25e0ca407..2e36f82c4 100644 --- a/BitcoinKit/Sources/BitcoinKit/Scripts/Script.swift +++ b/BitcoinKit/Sources/BitcoinKit/Scripts/Script.swift @@ -401,29 +401,6 @@ public class Script { } extension Script { - // Standard Transaction to Bitcoin address (pay-to-pubkey-hash) - // scriptPubKey: OP_DUP OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG - public static func buildPublicKeyHashOut(pubKeyHash: Data) -> Data { - var tmp = Data() - tmp += OpCode.OP_DUP - tmp += OpCode.OP_HASH160 - tmp += UInt8(pubKeyHash.count) - tmp += pubKeyHash - tmp += OpCode.OP_EQUALVERIFY - tmp += OpCode.OP_CHECKSIG - return tmp - } - - // Transaction to Bitcoin address (pay-to-script-hash) - public static func buildScriptHashOut(scriptHash: Data) -> Data { - var tmp = Data() - tmp += OpCode.OP_HASH160 - tmp += UInt8(scriptHash.count) - tmp += scriptHash - tmp += OpCode.OP_EQUAL - return tmp - } - public static func buildPublicKeyUnlockingScript(signature: Data, pubkey: PublicKey, hashType: SighashType) -> Data { var data: Data = Data([UInt8(signature.count + 1)]) + signature + UInt8(hashType) data += VarInt(pubkey.data.count).serialized()