-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathAddressTests.swift
135 lines (108 loc) · 5.53 KB
/
AddressTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
// AddressTests.swift
// AddressTests
//
// 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
class AddressTests: XCTestCase {
let hdKey = HDKey("xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ")!
let hdKeyTestnet = HDKey("tpubDDgEAMpHn8tX5Bs19WWJLZBeFzbpE7BYuP3Qo71abZnQ7FmN3idRPg4oPWt2Q6Uf9huGv7AGMTu8M2BaCxAdThQArjLWLDLpxVX2gYfh2YJ")!
override func setUp() {
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
func testDeriveLegacyAddress() {
let address = hdKey.address(.payToPubKeyHash)
XCTAssertEqual(address.description, "1JQheacLPdM5ySCkrZkV66G2ApAXe1mqLj")
}
func testDeriveLegacyAddressTestnet() {
let address = hdKeyTestnet.address(.payToPubKeyHash)
XCTAssertEqual(address.description, "mnicNaAVzyGdFvDa9VkMrjgNdnr2wHBWxk")
}
func testDeriveWrappedSegWitAddress() {
let address = hdKey.address(.payToScriptHashPayToWitnessPubKeyHash)
XCTAssertEqual(address.description, "3DymAvEWH38HuzHZ3VwLus673bNZnYwNXu")
}
func testDeriveWrappedSegWitAddressTestnet() {
let address = hdKeyTestnet.address(.payToScriptHashPayToWitnessPubKeyHash)
XCTAssertEqual(address.description, "2N6M3ah9EoggimNz5pnAmQwnpE1Z3ya3V7A")
}
func testDeriveNativeSegWitAddress() {
let address = hdKey.address(.payToWitnessPubKeyHash)
XCTAssertEqual(address.description, "bc1qhm6697d9d2224vfyt8mj4kw03ncec7a7fdafvt")
}
func testDeriveNativeSegWitAddressTestnet() {
let address = hdKeyTestnet.address(.payToWitnessPubKeyHash)
XCTAssertEqual(address.description, "tb1qfm7nmm28m9n7gy3fsfpze8vymds9qwtjwn4w7y")
}
func testParseLegacyAddress() {
let address = Address("1JQheacLPdM5ySCkrZkV66G2ApAXe1mqLj")
XCTAssertNotNil(address)
XCTAssertEqual(address!.scriptPubKey, ScriptPubKey("76a914bef5a2f9a56a94aab12459f72ad9cf8cf19c7bbe88ac"))
}
func testParseWrappedSegWitAddress() {
let address = Address("3DymAvEWH38HuzHZ3VwLus673bNZnYwNXu")
XCTAssertNotNil(address)
XCTAssertEqual(address!.scriptPubKey, ScriptPubKey("a91486cc442a97817c245ce90ed0d31d6dbcde3841f987"))
}
func testParseNativeSegWitAddress() {
let address = Address("bc1qhm6697d9d2224vfyt8mj4kw03ncec7a7fdafvt")
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)
let pubKey = PubKey(Data("02a18a98316b5f52596e75bfa5ca9fa9912edd0c989b86b73d41bb64c9c6adb992")!, .mainnet)
XCTAssertNotNil(pubKey)
let derived = privKey!.ecdh(pubKey: pubKey!)
XCTAssertNotNil(derived)
XCTAssertEqual(derived!.hexString, "ef2cf705af8714b35c0855030f358f2bee356ff3579cea2607b2025d80133c3a")
}
func testECDHDerivationBidirectional() {
let privKey1 = Key(Data("9cd3b16e10bd574fed3743d8e0de0b7b4e6c69f3245ab5a168ef010d22bfefa0")!, .mainnet)
XCTAssertNotNil(privKey1)
let privKey2 = Key(Data("ef2cf705af8714b35c0855030f358f2bee356ff3579cea2607b2025d80133c3a")!, .mainnet)
XCTAssertNotNil(privKey2)
let derived1 = privKey1!.ecdh(pubKey: privKey2!.pubKey)
let derived2 = privKey2!.ecdh(pubKey: privKey1!.pubKey)
XCTAssertNotNil(derived1)
XCTAssertNotNil(derived2)
XCTAssertEqual(derived1!.hexString, derived2!.hexString)
}
func testParseWIF() {
// https://en.bitcoin.it/wiki/Wallet_import_format
let wif = "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ"
if let key = Key(wif, .mainnet, compressed: false) {
XCTAssertEqual(key.data.hexString, "0c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d")
XCTAssertEqual(key.network, .mainnet)
XCTAssertEqual(key.compressed, false)
} else { XCTFail() }
}
func testToWIF() {
// https://en.bitcoin.it/wiki/Wallet_import_format
let data = Data("0c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d")
let key = Key(data!, .mainnet, compressed: false)
XCTAssertNotNil(key)
XCTAssertEqual(key!.wif, "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ")
}
}