github.com/lbryio/lbcd@v0.22.119/rpcclient/example_test.go (about)

     1  // Copyright (c) 2020 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 rpcclient
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/lbryio/lbcd/btcjson"
    11  )
    12  
    13  var connCfg = &ConnConfig{
    14  	Host:         "localhost:9244",
    15  	User:         "user",
    16  	Pass:         "pass",
    17  	HTTPPostMode: true,
    18  	DisableTLS:   true,
    19  }
    20  
    21  func ExampleClient_GetDescriptorInfo() {
    22  	client, err := New(connCfg, nil)
    23  	if err != nil {
    24  		panic(err)
    25  	}
    26  	defer client.Shutdown()
    27  
    28  	descriptorInfo, err := client.GetDescriptorInfo(
    29  		"wpkh([d34db33f/84h/0h/0h]0279be667ef9dcbbac55a06295Ce870b07029Bfcdb2dce28d959f2815b16f81798)")
    30  	if err != nil {
    31  		panic(err)
    32  	}
    33  
    34  	fmt.Printf("%+v\n", descriptorInfo)
    35  	// &{Descriptor:wpkh([d34db33f/84'/0'/0']0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798)#n9g43y4k Checksum:qwlqgth7 IsRange:false IsSolvable:true HasPrivateKeys:false}
    36  }
    37  
    38  func ExampleClient_ImportMulti() {
    39  	client, err := New(connCfg, nil)
    40  	if err != nil {
    41  		panic(err)
    42  	}
    43  	defer client.Shutdown()
    44  
    45  	requests := []btcjson.ImportMultiRequest{
    46  		{
    47  			Descriptor: btcjson.String(
    48  				"pkh([f34db33f/44'/0'/0']xpub6Cc939fyHvfB9pPLWd3bSyyQFvgKbwhidca49jGCM5Hz5ypEPGf9JVXB4NBuUfPgoHnMjN6oNgdC9KRqM11RZtL8QLW6rFKziNwHDYhZ6Kx/0/*)#ed7px9nu"),
    49  			Range:     &btcjson.DescriptorRange{Value: []int{0, 100}},
    50  			Timestamp: btcjson.TimestampOrNow{Value: 0}, // scan from genesis
    51  			WatchOnly: btcjson.Bool(true),
    52  			KeyPool:   btcjson.Bool(false),
    53  			Internal:  btcjson.Bool(false),
    54  		},
    55  	}
    56  	opts := &btcjson.ImportMultiOptions{Rescan: true}
    57  
    58  	resp, err := client.ImportMulti(requests, opts)
    59  	if err != nil {
    60  		panic(err)
    61  	}
    62  
    63  	fmt.Println(resp[0].Success)
    64  	// true
    65  }
    66  
    67  func ExampleClient_DeriveAddresses() {
    68  	client, err := New(connCfg, nil)
    69  	if err != nil {
    70  		panic(err)
    71  	}
    72  	defer client.Shutdown()
    73  
    74  	addrs, err := client.DeriveAddresses(
    75  		"pkh([f34db33f/44'/0'/0']xpub6Cc939fyHvfB9pPLWd3bSyyQFvgKbwhidca49jGCM5Hz5ypEPGf9JVXB4NBuUfPgoHnMjN6oNgdC9KRqM11RZtL8QLW6rFKziNwHDYhZ6Kx/0/*)#ed7px9nu",
    76  		&btcjson.DescriptorRange{Value: []int{0, 2}})
    77  	if err != nil {
    78  		panic(err)
    79  	}
    80  
    81  	fmt.Printf("%+v\n", addrs)
    82  	// &[14NjenDKkGGq1McUgoSkeUHJpW3rrKLbPW 1Pn6i3cvdGhqbdgNjXHfbaYfiuviPiymXj 181x1NbgGYKLeMXkDdXEAqepG75EgU8XtG]
    83  }
    84  
    85  func ExampleClient_GetAddressInfo() {
    86  	client, err := New(connCfg, nil)
    87  	if err != nil {
    88  		panic(err)
    89  	}
    90  	defer client.Shutdown()
    91  
    92  	info, err := client.GetAddressInfo("2NF1FbxtUAsvdU4uW1UC2xkBVatp6cYQuJ3")
    93  	if err != nil {
    94  		panic(err)
    95  	}
    96  
    97  	fmt.Println(info.Address)             // 2NF1FbxtUAsvdU4uW1UC2xkBVatp6cYQuJ3
    98  	fmt.Println(info.ScriptType.String()) // witness_v0_keyhash
    99  	fmt.Println(*info.HDKeyPath)          // m/49'/1'/0'/0/4
   100  	fmt.Println(info.Embedded.Address)    // tb1q3x2h2kh57wzg7jz00jhwn0ycvqtdk2ane37j27
   101  }
   102  
   103  func ExampleClient_GetWalletInfo() {
   104  	client, err := New(connCfg, nil)
   105  	if err != nil {
   106  		panic(err)
   107  	}
   108  	defer client.Shutdown()
   109  
   110  	info, err := client.GetWalletInfo()
   111  	if err != nil {
   112  		panic(err)
   113  	}
   114  
   115  	fmt.Println(info.WalletVersion)    // 169900
   116  	fmt.Println(info.TransactionCount) // 22
   117  	fmt.Println(*info.HDSeedID)        // eb44e4e9b864ef17e7ba947da746375b000f5d94
   118  	fmt.Println(info.Scanning.Value)   // false
   119  }
   120  
   121  func ExampleClient_GetTxOutSetInfo() {
   122  	client, err := New(connCfg, nil)
   123  	if err != nil {
   124  		panic(err)
   125  	}
   126  	defer client.Shutdown()
   127  
   128  	r, err := client.GetTxOutSetInfo()
   129  	if err != nil {
   130  		panic(err)
   131  	}
   132  
   133  	fmt.Println(r.TotalAmount.String()) // 20947654.56996054 BTC
   134  	fmt.Println(r.BestBlock.String())   // 000000000000005f94116250e2407310463c0a7cf950f1af9ebe935b1c0687ab
   135  	fmt.Println(r.TxOuts)               // 24280607
   136  	fmt.Println(r.Transactions)         // 9285603
   137  	fmt.Println(r.DiskSize)             // 1320871611
   138  }
   139  
   140  func ExampleClient_CreateWallet() {
   141  	client, err := New(connCfg, nil)
   142  	if err != nil {
   143  		panic(err)
   144  	}
   145  	defer client.Shutdown()
   146  
   147  	r, err := client.CreateWallet(
   148  		"mywallet",
   149  		WithCreateWalletBlank(),
   150  		WithCreateWalletPassphrase("secret"),
   151  	)
   152  	if err != nil {
   153  		panic(err)
   154  	}
   155  
   156  	fmt.Println(r.Name) // mywallet
   157  }