Skip to content

Commit

Permalink
Add bech32m support
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjors committed Oct 20, 2021
1 parent e794cde commit da966a5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
13 changes: 10 additions & 3 deletions LibWally/Address.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
// Address
//
// Created by Sjors on 14/06/2019.
// Copyright © 2019 Blockchain. Distributed under the MIT software
// license, see the accompanying file LICENSE.md
// Copyright © 2019 Blockchain.
// Copyright © 2021 Sjors Provoost.
// Distributed under the MIT software license, see the accompanying file LICENSE.md

// Temporarily define this until libwally-core adds it (potentially with a different version)
let WALLY_ADDRESS_TYPE_P2TR: Int32 = 0x08 // P2TR taproot address ("bc1p..)"

import Foundation
import CLibWally
Expand All @@ -13,6 +17,7 @@ public enum AddressType {
case payToPubKeyHash // P2PKH (legacy)
case payToScriptHashPayToWitnessPubKeyHash // P2SH-P2WPKH (wrapped SegWit)
case payToWitnessPubKeyHash // P2WPKH (native SegWit)
case payToTaproot // P2TR (Taproot)
}

public protocol AddressProtocol : LosslessStringConvertible {
Expand Down Expand Up @@ -75,6 +80,8 @@ public struct Address : AddressProtocol {
return WALLY_ADDRESS_TYPE_P2SH_P2WPKH
case .payToWitnessPubKeyHash:
return WALLY_ADDRESS_TYPE_P2WPKH
case .payToTaproot:
return WALLY_ADDRESS_TYPE_P2TR
}
}()

Expand Down Expand Up @@ -130,7 +137,7 @@ public struct Address : AddressProtocol {
precondition(wally_scriptpubkey_to_address(bytes, bytes_len, UInt32(network == .mainnet ? WALLY_NETWORK_BITCOIN_MAINNET : WALLY_NETWORK_BITCOIN_TESTNET), &output) == WALLY_OK)
precondition(output != nil)
self.address = String(cString: output!)
case .payToWitnessPubKeyHash, .payToWitnessScriptHash:
case .payToWitnessPubKeyHash, .payToWitnessScriptHash, .payToTaproot:
var family: String
switch network {
case .mainnet:
Expand Down
8 changes: 6 additions & 2 deletions LibWally/Script.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
// Script
//
// Created by Sjors on 14/06/2019.
// Copyright © 2019 Blockchain. Distributed under the MIT software
// license, see the accompanying file LICENSE.md
// Copyright © 2019 Blockchain.
// Copyright © 2021 Sjors Provoost.
// Distributed under the MIT software license, see the accompanying file LICENSE.md

import Foundation
import CLibWally
Expand All @@ -17,6 +18,7 @@ public enum ScriptType {
case payToScriptHash // P2SH (could be wrapped SegWit)
case payToWitnessPubKeyHash // P2WPKH (native SegWit)
case payToWitnessScriptHash // P2WS (native SegWit script)
case payToTaproot // P2TR (taproot script)
case multiSig
}

Expand Down Expand Up @@ -60,6 +62,8 @@ public struct ScriptPubKey : LosslessStringConvertible, Equatable {
return .payToWitnessPubKeyHash
case WALLY_SCRIPT_TYPE_P2WSH:
return .payToWitnessScriptHash
case WALLY_SCRIPT_TYPE_P2TR:
return .payToTaproot
case WALLY_SCRIPT_TYPE_MULTISIG:
return .multiSig
default:
Expand Down
22 changes: 19 additions & 3 deletions LibWallyTests/AddressTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
// AddressTests.swift
// AddressTests
//
// Created by Bitcoin Dev on 14/06/2019.
// Copyright © 2019 Blockchain. Distributed under the MIT software
// license, see the accompanying file LICENSE.md
// Created by Sjors Provoost on 14/06/2019.
// Copyright © 2019 Blockchain.
// Copyright © 2021 Sjors Provoost.
// Distributed under the MIT software license, see the accompanying file LICENSE.md

import XCTest
@testable import LibWally
Expand Down Expand Up @@ -69,7 +70,22 @@ class AddressTests: XCTestCase {
XCTAssertNotNil(address)
XCTAssertEqual(address!.scriptPubKey, ScriptPubKey("0014bef5a2f9a56a94aab12459f72ad9cf8cf19c7bbe"))
}

func testParseTaprootAddress() {
let address = Address("bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqzk5jj0")
XCTAssertNotNil(address)
XCTAssertEqual(address!.scriptPubKey.type, .payToTaproot)
XCTAssertEqual(address!.scriptPubKey, ScriptPubKey("512079be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"))
}

func testRenderTaprootAddress() {
let scriptPubKey = ScriptPubKey("512079be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798")
XCTAssertEqual(scriptPubKey!.type, .payToTaproot)
let address = Address(scriptPubKey!, .mainnet)
XCTAssertNotNil(address)
XCTAssertEqual(address!.description, "bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqzk5jj0")
}

func testECDHDerivation() {
let privKey = Key(Data("9cd3b16e10bd574fed3743d8e0de0b7b4e6c69f3245ab5a168ef010d22bfefa0")!, .mainnet)
XCTAssertNotNil(privKey)
Expand Down

0 comments on commit da966a5

Please sign in to comment.