Ejemplo n.º 1
0
func (self *Config) postProcess() {

	//var GenesisSignatureStr string //only set if passed in command line arg
	//var GenesisAddressStr string   //only set if passed in command line arg
	//var BlockchainPubkeyStr string //only set if passed in command line arg
	//var BlockchainSeckeyStr string //only set if passed in command line arg

	var err error
	if GenesisSignatureStr != "" {
		self.GenesisSignature, err = cipher.SigFromHex(GenesisSignatureStr)
		if err != nil {
			log.Panic("Invalid Signature")
		}
	}
	if GenesisAddressStr != "" {
		self.GenesisAddress, err = cipher.DecodeBase58Address(GenesisAddressStr)
		if err != nil {
			log.Panic("Invalid Address")
		}
	}
	if BlockchainPubkeyStr != "" {
		self.BlockchainPubkey, err = cipher.PubKeyFromHex(BlockchainPubkeyStr)
		if err != nil {
			log.Panic("Invalid Pubkey")
		}
	}
	if BlockchainSeckeyStr != "" {
		self.BlockchainSeckey, err = cipher.SecKeyFromHex(BlockchainSeckeyStr)
		if err != nil {
			log.Panic("Invalid Seckey")
		}
		BlockchainSeckeyStr = ""
	}
	if BlockchainSeckeyStr != "" {
		self.BlockchainSeckey = cipher.SecKey{}
	}

	self.DataDirectory = util.InitDataDir(self.DataDirectory)
	if self.WebInterfaceCert == "" {
		self.WebInterfaceCert = filepath.Join(self.DataDirectory, "cert.pem")
	}
	if self.WebInterfaceKey == "" {
		self.WebInterfaceKey = filepath.Join(self.DataDirectory, "key.pem")
	}

	if self.BlockchainFile == "" {
		self.BlockchainFile = filepath.Join(self.DataDirectory, "blockchain.bin")
	}
	if self.BlockSigsFile == "" {
		self.BlockSigsFile = filepath.Join(self.DataDirectory, "blockchain.sigs")
	}
	if self.WalletDirectory == "" {
		self.WalletDirectory = filepath.Join(self.DataDirectory, "wallets/")
	}
	ll, err := logging.LogLevel(self.logLevel)
	if err != nil {
		log.Panic("Invalid -log-level %s: %v\n", self.logLevel, err)
	}
	self.LogLevel = ll
}
Ejemplo n.º 2
0
// Authorize will decrypt the request, and it's a buildin middleware for skynet.
func Authorize(servSeckey string) HandlerFunc {
	return func(c *Context) error {
		var (
			req pp.EncryptReq
			rlt *pp.EmptyRes
		)

		c.ServSeckey = servSeckey

		for {
			if c.UnmarshalReq(&req) == nil {
				// validate pubkey.
				if err := validatePubkey(req.GetPubkey()); err != nil {
					logger.Error(err.Error())
					rlt = pp.MakeErrResWithCode(pp.ErrCode_WrongPubkey)
					break
				}
				pubkey, err := cipher.PubKeyFromHex(req.GetPubkey())
				if err != nil {
					logger.Error(err.Error())
					rlt = pp.MakeErrResWithCode(pp.ErrCode_WrongPubkey)
					break
				}
				c.Pubkey = pubkey.Hex()

				seckey, err := cipher.SecKeyFromHex(servSeckey)
				if err != nil {
					logger.Error(err.Error())
					rlt = pp.MakeErrResWithCode(pp.ErrCode_ServerError)
					break
				}

				key := cipher.ECDH(pubkey, seckey)
				data, err := cipher.Chacha20Decrypt(req.GetEncryptdata(), key, req.GetNonce())
				if err != nil {
					logger.Error(err.Error())
					rlt = pp.MakeErrResWithCode(pp.ErrCode_UnAuthorized)
					break
				}

				ok, err := regexp.MatchString(`^\{.*\}$`, string(data))
				if err != nil || !ok {
					logger.Error(err.Error())
					rlt = pp.MakeErrResWithCode(pp.ErrCode_UnAuthorized)
					break
				}

				c.Raw = data

				return c.Next()
			}
			rlt = pp.MakeErrRes(errors.New("bad request"))
			break
		}
		return c.Error(rlt)
	}
}
Ejemplo n.º 3
0
// CreateRawTx creates raw transaction
func (cn coinEx) CreateRawTx(txIns []coin.TxIn, getKey coin.GetPrivKey, txOuts interface{}) (string, error) {
	tx := skycoin.Transaction{}
	for _, in := range txIns {
		tx.PushInput(cipher.MustSHA256FromHex(in.Txid))
	}

	s := reflect.ValueOf(txOuts)
	if s.Kind() != reflect.Slice {
		return "", errors.New("error tx out type")
	}
	outs := make([]interface{}, s.Len())
	for i := 0; i < s.Len(); i++ {
		outs[i] = s.Index(i).Interface()
	}

	if len(outs) > 2 {
		return "", errors.New("out address more than 2")
	}

	for _, o := range outs {
		out := o.(skycoin.TxOut)
		if (out.Coins % 1e6) != 0 {
			return "", fmt.Errorf("%s coins must be multiple of 1e6", cn.Name)
		}
		tx.PushOutput(out.Address, out.Coins, out.Hours)
	}

	keys := make([]cipher.SecKey, len(txIns))
	for i, in := range txIns {
		s, err := getKey(in.Address)
		if err != nil {
			return "", fmt.Errorf("get private key failed:%v", err)
		}
		k, err := cipher.SecKeyFromHex(s)
		if err != nil {
			return "", fmt.Errorf("invalid private key:%v", err)
		}
		keys[i] = k
	}

	tx.SignInputs(keys)
	tx.UpdateHeader()

	d, err := tx.Serialize()
	if err != nil {
		return "", err
	}
	return hex.EncodeToString(d), nil
}
Ejemplo n.º 4
0
func (c *Config) postProcess() {
	var err error
	if GenesisSignatureStr != "" {
		c.GenesisSignature, err = cipher.SigFromHex(GenesisSignatureStr)
		panicIfError(err, "Invalid Signature")
	}
	if GenesisAddressStr != "" {
		c.GenesisAddress, err = cipher.DecodeBase58Address(GenesisAddressStr)
		panicIfError(err, "Invalid Address")
	}
	if BlockchainPubkeyStr != "" {
		c.BlockchainPubkey, err = cipher.PubKeyFromHex(BlockchainPubkeyStr)
		panicIfError(err, "Invalid Pubkey")
	}
	if BlockchainSeckeyStr != "" {
		c.BlockchainSeckey, err = cipher.SecKeyFromHex(BlockchainSeckeyStr)
		panicIfError(err, "Invalid Seckey")
		BlockchainSeckeyStr = ""
	}
	if BlockchainSeckeyStr != "" {
		c.BlockchainSeckey = cipher.SecKey{}
	}

	c.DataDirectory = util.InitDataDir(c.DataDirectory)
	if c.WebInterfaceCert == "" {
		c.WebInterfaceCert = filepath.Join(c.DataDirectory, "cert.pem")
	}
	if c.WebInterfaceKey == "" {
		c.WebInterfaceKey = filepath.Join(c.DataDirectory, "key.pem")
	}

	if c.BlockchainFile == "" {
		c.BlockchainFile = filepath.Join(c.DataDirectory, "blockchain.bin")
	}
	if c.BlockSigsFile == "" {
		c.BlockSigsFile = filepath.Join(c.DataDirectory, "blockchain.sigs")
	}
	if c.WalletDirectory == "" {
		c.WalletDirectory = filepath.Join(c.DataDirectory, "wallets/")
	}

	ll, err := logging.LogLevel(c.logLevel)
	panicIfError(err, "Invalid -log-level %s", c.logLevel)
	c.LogLevel = ll
}
Ejemplo n.º 5
0
func encrypt(r interface{}, pubkey string, seckey string) (*pp.EncryptReq, error) {
	encData, nonce, err := pp.Encrypt(r, pubkey, seckey)
	if err != nil {
		return nil, err
	}

	s, err := cipher.SecKeyFromHex(seckey)
	if err != nil {
		return nil, err
	}

	p := cipher.PubKeyFromSecKey(s)
	return &pp.EncryptReq{
		Pubkey:      pp.PtrString(p.Hex()),
		Nonce:       nonce,
		Encryptdata: encData,
	}, nil
}
Ejemplo n.º 6
0
func addPrivateKeyCMD() gcli.Command {
	name := "addPrivateKey"
	return gcli.Command{
		Name:      name,
		Usage:     "Add a private key to specific wallet",
		ArgsUsage: "[private key]",
		Description: fmt.Sprintf(`Add a private key to specific wallet, the default
		wallet(%s/%s) will be 
		used if the wallet file or path is not specified`,
			cfg.WalletDir, cfg.DefaultWalletName),
		Flags: []gcli.Flag{
			gcli.StringFlag{
				Name:  "f",
				Usage: "[wallet file or path] private key will be added to this wallet",
			},
		},
		OnUsageError: onCommandUsageError(name),
		Action: func(c *gcli.Context) error {
			// get private key
			skStr := c.Args().First()
			if skStr == "" {
				gcli.ShowSubcommandHelp(c)
				return nil
			}

			// get wallet file path
			w := c.String("f")
			if w == "" {
				w = filepath.Join(cfg.WalletDir, cfg.DefaultWalletName)
			}

			if !strings.HasSuffix(w, walletExt) {
				return errWalletName
			}

			// only wallet file name, no path.
			if filepath.Base(w) == w {
				w = filepath.Join(cfg.WalletDir, w)
			}

			wlt, err := wallet.Load(w)
			if err != nil {
				errorWithHelp(c, err)
				return nil
			}

			sk, err := cipher.SecKeyFromHex(skStr)
			if err != nil {
				return fmt.Errorf("invalid private key: %s, must be an hex string of length 64", skStr)
			}

			pk := cipher.PubKeyFromSecKey(sk)
			addr := cipher.AddressFromPubKey(pk)

			entry := wallet.WalletEntry{
				Address: addr,
				Public:  pk,
				Secret:  sk,
			}

			if err := wlt.AddEntry(entry); err != nil {
				return err
			}

			dir, err := filepath.Abs(filepath.Dir(w))
			if err != nil {
				return err
			}

			if err := wlt.Save(dir); err != nil {
				return errors.New("save wallet failed")
			}

			fmt.Println("success")

			return nil
		},
	}
	// Commands = append(Commands, cmd)
}