Skip to content

Commit 1ba4641

Browse files
committed
add tests for script/address.go
1 parent 1ed8198 commit 1ba4641

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ All notable changes to this project will be documented in this file. The format
1818
- Added new KeyShares and polynomial primitives, and polynomial operations to support key splitting
1919
- Tests for all new keyshare, private key, and polynomial operations
2020
- added recommended vscode plugin and extension settings for this repo in .vscode directory
21+
- handle base58 decode errors
22+
- additional tests for script/address.go
2123

2224
### Added
2325
- `PrivateKey.ToKeyShares`

script/address_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,71 @@ func TestBase58EncodeMissingChecksum(t *testing.T) {
113113
script.Base58EncodeMissingChecksum(input),
114114
)
115115
}
116+
117+
func TestNewAddressFromPublicKeyHash(t *testing.T) {
118+
t.Parallel()
119+
120+
t.Run("mainnet", func(t *testing.T) {
121+
hash, err := hex.DecodeString(testPublicKeyHash)
122+
require.NoError(t, err)
123+
124+
addr, err := script.NewAddressFromPublicKeyHash(hash, true)
125+
require.NoError(t, err)
126+
require.NotNil(t, addr)
127+
128+
require.Equal(t, testPublicKeyHash, addr.PublicKeyHash.String())
129+
require.Equal(t, "114ZWApV4EEU8frr7zygqQcB1V2BodGZuS", addr.AddressString)
130+
})
131+
132+
t.Run("testnet", func(t *testing.T) {
133+
hash, err := hex.DecodeString(testPublicKeyHash)
134+
require.NoError(t, err)
135+
136+
addr, err := script.NewAddressFromPublicKeyHash(hash, false)
137+
require.NoError(t, err)
138+
require.NotNil(t, addr)
139+
140+
require.Equal(t, testPublicKeyHash, addr.PublicKeyHash.String())
141+
require.Equal(t, "mfaWoDuTsFfiunLTqZx4fKpVsUctiDV9jk", addr.AddressString)
142+
})
143+
}
144+
145+
func TestNewAddressFromPublicKeyStringInvalid(t *testing.T) {
146+
t.Parallel()
147+
148+
addr, err := script.NewAddressFromPublicKeyString("invalid_pubkey", true)
149+
require.Error(t, err)
150+
require.Nil(t, addr)
151+
}
152+
153+
func TestNewAddressFromPublicKeyInvalid(t *testing.T) {
154+
t.Parallel()
155+
156+
invalidPubKeyBytes := []byte{0x00, 0x01, 0x02} // Invalid public key
157+
invalidPubKey, err := ec.ParsePubKey(invalidPubKeyBytes)
158+
require.Error(t, err)
159+
require.Nil(t, invalidPubKey)
160+
}
161+
162+
func TestBase58EncodeMissingChecksumEdgeCases(t *testing.T) {
163+
t.Parallel()
164+
165+
t.Run("empty input", func(t *testing.T) {
166+
result := script.Base58EncodeMissingChecksum([]byte{})
167+
require.NotEmpty(t, result)
168+
})
169+
170+
t.Run("single byte input", func(t *testing.T) {
171+
result := script.Base58EncodeMissingChecksum([]byte{0x00})
172+
require.NotEmpty(t, result)
173+
})
174+
175+
t.Run("large input", func(t *testing.T) {
176+
largeInput := make([]byte, 1000)
177+
for i := range largeInput {
178+
largeInput[i] = byte(i % 256)
179+
}
180+
result := script.Base58EncodeMissingChecksum(largeInput)
181+
require.NotEmpty(t, result)
182+
})
183+
}

0 commit comments

Comments
 (0)