Exemplo n.º 1
0
func openWallet(db walletdb.DB, privPass string, seed []byte) (*Wallet, error) {
	addrMgrNS, err := db.Namespace(waddrmgrNamespaceKey)
	if err != nil {
		return nil, err
	}
	txMgrNS, err := db.Namespace(wtxmgrNamespaceKey)
	if err != nil {
		return nil, err
	}

	cbs := &waddrmgr.OpenCallbacks{
		ObtainSeed:        returnBytes(seed),
		ObtainPrivatePass: returnBytes([]byte(privPass)),
	}
	backWallet, err := wallet.Open(nil, bitcoinNetwork.Params, db, addrMgrNS, txMgrNS, cbs)
	if err != nil {
		return nil, err
	}

	// TODO: use more than 1 node
	node := electrum.NewNode()
	if err := node.ConnectTCP("btc.mustyoshi.com:50001"); err != nil {
		return nil, err
	}

	w := &Wallet{
		wallet: backWallet,
		node:   node,
	}

	addrs, err := w.Addresses()
	if err != nil {
		return nil, err
	}
	for _, addr := range addrs {
		if err := w.watchAddress(addr.String()); err != nil {
			return nil, err
		}
	}

	return w, nil
}
Exemplo n.º 2
0
// NewLightningWallet creates/opens and initializes a LightningWallet instance.
// If the wallet has never been created (according to the passed dataDir), first-time
// setup is executed.
// TODO(roasbeef): fin...add config
func NewLightningWallet(config *Config) (*LightningWallet, walletdb.DB, error) {
	// Ensure the wallet exists or create it when the create flag is set.
	netDir := networkDir(config.DataDir, config.NetParams)

	var pubPass []byte
	if config.PublicPass == nil {
		pubPass = defaultPubPassphrase
	} else {
		pubPass = config.PublicPass
	}

	var walletDB walletdb.DB
	loader := btcwallet.NewLoader(config.NetParams, netDir)
	loader.RunAfterLoad(func(w *btcwallet.Wallet, db walletdb.DB) {
		walletDB = db
	})

	walletExists, err := loader.WalletExists()
	if err != nil {
		return nil, nil, err
	}

	var createID bool
	var wallet *btcwallet.Wallet
	if !walletExists {
		// Wallet has never been created, perform initial set up.
		wallet, err = loader.CreateNewWallet(pubPass, config.PrivatePass,
			config.HdSeed)
		if err != nil {
			return nil, nil, err
		}

		createID = true
	} else {
		// Wallet has been created and been initialized at this point, open it
		// along with all the required DB namepsaces, and the DB itself.
		wallet, err = loader.OpenExistingWallet(pubPass, false)
		if err != nil {
			return nil, nil, err
		}
	}

	// Create a special namespace for our unique payment channel related
	// meta-data. Subsequently initializing the channeldb around the
	// created namespace.
	lnNamespace, err := walletDB.Namespace(lightningNamespaceKey)
	if err != nil {
		return nil, nil, err
	}
	cdb := channeldb.New(wallet.Manager, lnNamespace)

	if err := wallet.Manager.Unlock(config.PrivatePass); err != nil {
		return nil, nil, err
	}

	// If we just created the wallet, then reserve, and store a key for
	// our ID within the Lightning Network.
	if createID {
		adrs, err := wallet.Manager.NextInternalAddresses(waddrmgr.DefaultAccountNum, 1)
		if err != nil {
			return nil, nil, err
		}

		idPubkeyHash := adrs[0].Address().ScriptAddress()
		if err := cdb.PutIdKey(idPubkeyHash); err != nil {
			return nil, nil, err
		}
		log.Printf("stored identity key pubkey hash in channeldb\n")
	}

	chainNotifier, err := btcdnotify.NewBtcdNotifier(wallet)
	if err != nil {
		return nil, nil, err
	}

	// TODO(roasbeef): logging
	return &LightningWallet{
		db:            walletDB,
		chainNotifier: chainNotifier,
		Wallet:        wallet,
		ChannelDB:     cdb,
		msgChan:       make(chan interface{}, msgBufferSize),
		// TODO(roasbeef): make this atomic.Uint32 instead? Which is
		// faster, locks or CAS? I'm guessing CAS because assembly:
		//  * https://golang.org/src/sync/atomic/asm_amd64.s
		nextFundingID: 0,
		cfg:           config,
		fundingLimbo:  make(map[uint64]*ChannelReservation),
		quit:          make(chan struct{}),
	}, walletDB, nil
}