OSDN Git Service

Merge remote-tracking branch 'origin/dev' into wallet
[bytom/bytom.git] / common / bech32 / example_test.go
1 // Copyright (c) 2017 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
4
5 package bech32_test
6
7 import (
8         "encoding/hex"
9         "fmt"
10
11         "github.com/btcsuite/btcutil/bech32"
12 )
13
14 // This example demonstrates how to decode a bech32 encoded string.
15 func ExampleDecode() {
16         encoded := "bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7k7grplx"
17         hrp, decoded, err := bech32.Decode(encoded)
18         if err != nil {
19                 fmt.Println("Error:", err)
20         }
21
22         // Show the decoded data.
23         fmt.Println("Decoded human-readable part:", hrp)
24         fmt.Println("Decoded Data:", hex.EncodeToString(decoded))
25
26         // Output:
27         // Decoded human-readable part: bc
28         // Decoded Data: 010e140f070d1a001912060b0d081504140311021d030c1d03040f1814060e1e160e140f070d1a001912060b0d081504140311021d030c1d03040f1814060e1e16
29 }
30
31 // This example demonstrates how to encode data into a bech32 string.
32 func ExampleEncode() {
33         data := []byte("Test data")
34         // Convert test data to base32:
35         conv, err := bech32.ConvertBits(data, 8, 5, true)
36         if err != nil {
37                 fmt.Println("Error:", err)
38         }
39         encoded, err := bech32.Encode("customHrp!11111q", conv)
40         if err != nil {
41                 fmt.Println("Error:", err)
42         }
43
44         // Show the encoded data.
45         fmt.Println("Encoded Data:", encoded)
46
47         // Output:
48         // Encoded Data: customHrp!11111q123jhxapqv3shgcgumastr
49 }