-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
59 lines (49 loc) · 1.74 KB
/
example_test.go
File metadata and controls
59 lines (49 loc) · 1.74 KB
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
// Copyright (c) 2017 The btcsuite developers
// Copyright (c) 2019 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package bech32_test
import (
"encoding/hex"
"fmt"
"github.com/picfight/bech32"
)
// This example demonstrates how to decode a bech32 encoded string.
func ExampleDecode() {
encoded := "dcr1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kf0q4gj"
hrp, decoded, err := bech32.Decode(encoded)
if err != nil {
fmt.Println("Error:", err)
}
// Convert the decoded data from 5 bits-per-element into 8-bits-per-element
// payload.
decoded8bits, err := bech32.ConvertBits(decoded, 5, 8, true)
if err != nil {
fmt.Println("Error ConvertBits:", err)
}
// Show the decoded data.
fmt.Println("Decoded human-readable part:", hrp)
fmt.Println("Decoded Data:", hex.EncodeToString(decoded))
fmt.Println("Decoded 8bpe Data:", hex.EncodeToString(decoded8bits))
// Output:
// Decoded human-readable part: dcr
// Decoded Data: 010e140f070d1a001912060b0d081504140311021d030c1d03040f1814060e1e160e140f070d1a001912060b0d081504140311021d030c1d03040f1814060e1e16
// Decoded 8bpe Data: 0ba8f3b740cc8cb6a2a4a0e22e8d9d191f8a19deb3a8f3b740cc8cb6a2a4a0e22e8d9d191f8a19deb0
}
// This example demonstrates how to encode data into a bech32 string.
func ExampleEncode() {
data := []byte("Test data")
// Convert test data to base32:
conv, err := bech32.ConvertBits(data, 8, 5, true)
if err != nil {
fmt.Println("Error:", err)
}
encoded, err := bech32.Encode("customHrp!11111q", conv)
if err != nil {
fmt.Println("Error:", err)
}
// Show the encoded data.
fmt.Println("Encoded Data:", encoded)
// Output:
// Encoded Data: customhrp!11111q123jhxapqv3shgcgkxpuhe
}