Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/recover wallet #105

Merged
merged 7 commits into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ export BITCOIN_NET
run_bitcoind:
./scripts/run_bitcoind.sh

reindex_bitciond:
./scripts/run_bitcoind.sh --reindex

stop_bitcoind:
./scripts/stop_bitcoind.sh

Expand Down
14 changes: 14 additions & 0 deletions internal/mocks/walletmock/Wallet.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 30 additions & 1 deletion internal/wallet/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ func (w *Wallet) NewPubkey() (pub *btcec.PublicKey, err error) {
if err != nil {
return nil, err
}
pub = (mAddr.(waddrmgr.ManagedPubKeyAddress)).PubKey()
mpka := mAddr.(waddrmgr.ManagedPubKeyAddress)
pub = mpka.PubKey()
return pub, err
}

Expand All @@ -29,6 +30,34 @@ func (w *Wallet) NewAddress() (btcutil.Address, error) {
return maddr.Address(), nil
}

// ImportAddress imports an address by creating addresses until reaching to it
// It stops after creating 100 addresses to avoid infinite loop
func (w *Wallet) ImportAddress(addr btcutil.Address) error {
var maddr waddrmgr.ManagedAddress
err := walletdb.View(w.db, func(tx walletdb.ReadTx) (e error) {
ns := tx.ReadBucket(waddrmgrNamespaceKey)
maddr, e = w.manager.Address(ns, addr)
return e
})
if err == nil && maddr != nil {
return nil
}

// generate new addresses until reaching to the target address
for i := 0; i < 100; i++ {
newAddr, err := w.NewAddress()
if err != nil {
return err
}

if newAddr.EncodeAddress() == addr.EncodeAddress() {
return nil
}
}

return errors.New("failed to import address")
}

// newAddress returns a new ManagedAddress
// NOTE: this function calls NextExternalAddresses to generate a ManagadAdddress.
func (w *Wallet) newAddress() (waddrmgr.ManagedAddress, error) {
Expand Down
72 changes: 38 additions & 34 deletions pkg/cmd/dlccli/contracts_deals_fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,8 @@ import (
"github.com/spf13/cobra"
)

var dlcid string
var osigfile string
var contractorType int

func runFixDeal(cmd *cobra.Command, args []string) {
c := initCotractor()

osig := parseOracleSignedMsg()

idxs := []int{}
n := len(osig.Sigs)
for i := 0; i < n; i++ {
idxs = append(idxs, i)
}
err := c.builder.FixDeal(osig, idxs)
errorHandler(err)

cetx, err := c.builder.SignedContractExecutionTx()
errorHandler(err)

cetxHex, err := utils.TxToHex(cetx)
errorHandler(err)
fmt.Printf("\nCETx hex:\n%s\n", cetxHex)

cltx, err := c.builder.SignedClosingTx(cetx)
errorHandler(err)
cltxHex, err := utils.TxToHex(cltx)
errorHandler(err)
fmt.Printf("\nClosingTx hex:\n%s\n", cltxHex)
}

func initCotractor() *Contractor {
func initCotractor(
dlcid, walletDir, walletName, pubpass, privpass string, contractorType int) *Contractor {
w, wdb := openWallet(pubpass, walletDir, walletName)
err := w.Unlock([]byte(privpass))
errorHandler(err)
Expand All @@ -69,7 +39,7 @@ func initCotractor() *Contractor {
}
}

func parseOracleSignedMsg() *oracle.SignedMsg {
func parseOracleSignedMsg(osigfile string) *oracle.SignedMsg {
data, err := ioutil.ReadFile(osigfile)
errorHandler(err)

Expand All @@ -81,10 +51,44 @@ func parseOracleSignedMsg() *oracle.SignedMsg {
}

func initFixDealCmd() *cobra.Command {
var dlcid string
var osigfile string
var contractorType int
var walletName string
var pubpass string
var privpass string

var cmd = &cobra.Command{
Use: "fix",
Short: "Fix deal",
Run: runFixDeal,
Run: func(cmd *cobra.Command, args []string) {
c := initCotractor(
dlcid, walletDir, walletName, pubpass, privpass, contractorType)

osig := parseOracleSignedMsg(osigfile)

idxs := []int{}
n := len(osig.Sigs)
for i := 0; i < n; i++ {
idxs = append(idxs, i)
}
err := c.builder.FixDeal(osig, idxs)
errorHandler(err)

cetx, err := c.builder.SignedContractExecutionTx()
errorHandler(err)

cetxHex, err := utils.TxToHex(cetx)
errorHandler(err)
fmt.Printf("\nCETx hex:\n%s\n", cetxHex)

cltx, err := c.builder.SignedClosingTx(cetx)
errorHandler(err)
cltxHex, err := utils.TxToHex(cltx)
errorHandler(err)
fmt.Printf("\nClosingTx hex:\n%s\n", cltxHex)

},
}

cmd.Flags().StringVar(&dlcid, "dlcid", "", "Contract ID")
Expand Down
Loading