github.com/dawnbass68/maddcash@v0.0.0-20201001105353-c91c12cb36e5/build/tools/trezor-common/sync-coins.go (about)

     1  //usr/bin/go run $0 $@ ; exit
     2  package main
     3  
     4  import (
     5  	build "blockbook/build/tools"
     6  	"encoding/json"
     7  	"errors"
     8  	"fmt"
     9  	"io"
    10  	"io/ioutil"
    11  	"net/http"
    12  	"os"
    13  	"path/filepath"
    14  	"strconv"
    15  	"strings"
    16  )
    17  
    18  const (
    19  	configsDir          = "configs"
    20  	trezorCommonDefsURL = "https://raw.githubusercontent.com/trezor/trezor-firmware/master/common/defs/bitcoin/"
    21  )
    22  
    23  type trezorCommonDef struct {
    24  	Name                  string `json:"coin_name"`
    25  	Shortcut              string `json:"coin_shortcut"`
    26  	Label                 string `json:"coin_label"`
    27  	XPubMagic             uint32 `json:"xpub_magic"`
    28  	XPubMagicSegwitP2sh   uint32 `json:"xpub_magic_segwit_p2sh"`
    29  	XPubMagicSegwitNative uint32 `json:"xpub_magic_segwit_native"`
    30  	Slip44                uint32 `json:"slip44,omitempty"`
    31  }
    32  
    33  func getTrezorCommonDef(coin string) (*trezorCommonDef, error) {
    34  	req, err := http.NewRequest("GET", trezorCommonDefsURL+coin+".json", nil)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	resp, err := http.DefaultClient.Do(req)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	defer resp.Body.Close()
    43  	if resp.StatusCode != http.StatusOK {
    44  		return nil, errors.New("Github request status code " + strconv.Itoa(resp.StatusCode))
    45  	}
    46  	bb, err := ioutil.ReadAll(resp.Body)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	var tcd trezorCommonDef
    51  	json.Unmarshal(bb, &tcd)
    52  	return &tcd, nil
    53  }
    54  
    55  func writeConfig(coin string, config *build.Config) error {
    56  	path := filepath.Join(configsDir, "coins", coin+".json")
    57  	out, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
    58  	if err != nil {
    59  		return err
    60  	}
    61  	defer out.Close()
    62  	buf, err := json.MarshalIndent(config, "", "  ")
    63  	if err != nil {
    64  		return err
    65  	}
    66  	n, err := out.Write(buf)
    67  	if err != nil {
    68  		return err
    69  	}
    70  	if n < len(buf) {
    71  		return io.ErrShortWrite
    72  	}
    73  	return nil
    74  }
    75  
    76  func main() {
    77  	var coins []string
    78  	if len(os.Args) < 2 {
    79  		filepath.Walk(filepath.Join(configsDir, "coins"), func(path string, info os.FileInfo, err error) error {
    80  			n := strings.TrimSuffix(info.Name(), ".json")
    81  			if n != info.Name() {
    82  				coins = append(coins, n)
    83  			}
    84  			return nil
    85  		})
    86  	} else {
    87  		coins = append(coins, os.Args[1])
    88  	}
    89  	for _, coin := range coins {
    90  		config, err := build.LoadConfig(configsDir, coin)
    91  		if err == nil {
    92  			var tcd *trezorCommonDef
    93  			tcd, err = getTrezorCommonDef(coin)
    94  			if err == nil {
    95  				if tcd.Name != "" {
    96  					config.Coin.Name = tcd.Name
    97  				}
    98  				if tcd.Shortcut != "" {
    99  					config.Coin.Shortcut = tcd.Shortcut
   100  				}
   101  				if tcd.Label != "" {
   102  					config.Coin.Label = tcd.Label
   103  				}
   104  				if tcd.XPubMagic != 0 {
   105  					config.Blockbook.BlockChain.XPubMagic = tcd.XPubMagic
   106  				}
   107  				if tcd.XPubMagicSegwitP2sh != 0 {
   108  					config.Blockbook.BlockChain.XPubMagicSegwitP2sh = tcd.XPubMagicSegwitP2sh
   109  				}
   110  				if tcd.XPubMagicSegwitNative != 0 {
   111  					config.Blockbook.BlockChain.XPubMagicSegwitNative = tcd.XPubMagicSegwitNative
   112  				}
   113  				if tcd.Slip44 != 0 {
   114  					config.Blockbook.BlockChain.Slip44 = tcd.Slip44
   115  				}
   116  				err = writeConfig(coin, config)
   117  				if err == nil {
   118  					fmt.Printf("%v updated\n", coin)
   119  				}
   120  			}
   121  		}
   122  		if err != nil {
   123  			fmt.Printf("%v update error %v\n", coin, err)
   124  		}
   125  	}
   126  }