Skip to content

Commit 02434df

Browse files
Merge pull request #162 from Adamant-im/feature/bitcoin-pay-to-script-hash-fix
Fixed sending btc transactions by pay-to-script-hash method
2 parents 47175af + 64d6764 commit 02434df

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

Adamant/Wallets/Bitcoin/BtcWalletService+Send.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ extension BtcWalletService: WalletServiceTwoStepSend {
139139

140140
let value = NSDecimalNumber(decimal: item.value).uint64Value
141141

142-
let lockScript = Script.buildPublicKeyHashOut(pubKeyHash: wallet.publicKey.toCashaddr().data)
142+
let lockScript = wallet.publicKey.toCashaddr().lockingScript
143143
let txHash = Data(hex: item.txId).map { Data($0.reversed()) } ?? Data()
144144
let txIndex = item.vout
145145

BitcoinKit/Sources/BitcoinKit/Core/Keys/Address.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,17 @@ extension Cashaddr: CustomStringConvertible {
252252
return cashaddr
253253
}
254254
}
255+
256+
extension AddressProtocol {
257+
public var lockingScript: Data {
258+
switch type {
259+
case .pubkeyHash:
260+
return Script.buildPublicKeyHashOut(pubKeyHash: data)
261+
case .scriptHash:
262+
return Script.buildScriptHashOut(scriptHash: data)
263+
default:
264+
assertionFailure("Unknown type")
265+
return .init()
266+
}
267+
}
268+
}

BitcoinKit/Sources/BitcoinKit/Messages/Transaction.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,8 @@ public struct Transaction {
145145
let totalAmount: UInt64 = UInt64(utxos.reduce(0) { $0 + $1.output.value })
146146
let change: UInt64 = totalAmount - amount - fee
147147

148-
let toPubKeyHash: Data = toAddress.data
149-
let changePubkeyHash: Data = changeAddress.data
150-
151-
let lockingScriptTo = Script.buildPublicKeyHashOut(pubKeyHash: toPubKeyHash)
152-
let lockingScriptChange = Script.buildPublicKeyHashOut(pubKeyHash: changePubkeyHash)
148+
let lockingScriptTo = toAddress.lockingScript
149+
let lockingScriptChange = changeAddress.lockingScript
153150

154151
var outputs = [TransactionOutput]()
155152
outputs.append(TransactionOutput(value: amount, lockingScript: lockingScriptTo))

BitcoinKit/Sources/BitcoinKit/Scripts/Script.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,16 @@ extension Script {
468468
tmp += OpCode.OP_CHECKSIG
469469
return tmp
470470
}
471+
472+
// Transaction to Bitcoin address (pay-to-script-hash)
473+
public static func buildScriptHashOut(scriptHash: Data) -> Data {
474+
var tmp = Data()
475+
tmp += OpCode.OP_HASH160
476+
tmp += UInt8(scriptHash.count)
477+
tmp += scriptHash
478+
tmp += OpCode.OP_EQUAL
479+
return tmp
480+
}
471481

472482
public static func buildPublicKeyUnlockingScript(signature: Data, pubkey: PublicKey, hashType: SighashType) -> Data {
473483
var data: Data = Data([UInt8(signature.count + 1)]) + signature + UInt8(hashType)

0 commit comments

Comments
 (0)