Пример #1
0
func NewWalletRPC() *WalletRPC {
	rpc := WalletRPC{}

	//wallet directory
	//cleanup, pass as parameter during init

	DataDirectory := util.InitDataDir("")
	rpc.WalletDirectory = filepath.Join(DataDirectory, "wallets/")
	logger.Debug("Wallet Directory= %v", rpc.WalletDirectory)
	util.InitDataDir(rpc.WalletDirectory)

	rpc.Wallets = wallet.Wallets{}

	//util.InitDataDir(".skycoin")
	//util.InitDataDir(".skycoin/wallets")

	//if rpc.WalletDirectory != "" {
	w, err := wallet.LoadWallets(rpc.WalletDirectory)
	if err != nil {
		log.Panicf("Failed to load all wallets: %v", err)
	}
	rpc.Wallets = w
	//}
	if len(rpc.Wallets) == 0 {
		rpc.Wallets.Add(wallet.NewWallet("")) //deterministic
		if rpc.WalletDirectory != "" {
			errs := rpc.Wallets.Save(rpc.WalletDirectory)
			if len(errs) != 0 {
				log.Panicf("Failed to save wallets: %v", errs)
			}
		}
	}

	return &rpc
}
Пример #2
0
func NewWalletRPC(walletDir string) *WalletRPC {
	rpc := &WalletRPC{}

	if err := os.MkdirAll(walletDir, os.FileMode(0700)); err != nil {
		log.Panicf("Failed to create wallet directory %s: %v", walletDir, err)
	}

	rpc.WalletDirectory = walletDir

	w, err := wallet.LoadWallets(rpc.WalletDirectory)
	if err != nil {
		log.Panicf("Failed to load all wallets: %v", err)
	}
	rpc.Wallets = w

	if len(rpc.Wallets) == 0 {
		rpc.Wallets.Add(wallet.NewWallet("")) //deterministic
		errs := rpc.Wallets.Save(rpc.WalletDirectory)
		if len(errs) != 0 {
			log.Panicf("Failed to save wallets to %s: %v", rpc.WalletDirectory, errs)
		}
	}

	return rpc
}
Пример #3
0
func (self *WalletRPC) CreateWallet(wltName string, options ...wallet.Option) (wallet.Wallet, error) {
	ops := make([]wallet.Option, 0, len(self.Options)+len(options))
	ops = append(ops, self.Options...)
	ops = append(ops, options...)
	w := wallet.NewWallet(wltName, ops...)
	// generate a default address
	w.GenerateAddresses(1)

	if err := self.Wallets.Add(w); err != nil {
		return wallet.Wallet{}, err
	}

	return w, nil
}
Пример #4
0
func makeValidTxn() (coin.Transaction, error) {
	w := wallet.NewWallet("test")
	w.GenerateAddresses(2)
	uncf := NewUnconfirmedTxnPool()
	now := tNow()
	a := makeAddress()
	uxs := makeUxBalancesForAddresses([]wallet.Balance{
		wallet.Balance{10e6, 150},
		wallet.Balance{15e6, 150},
	}, now, w.GetAddresses()[:2])
	unsp := coin.NewUnspentPool()
	addUxArrayToUnspentPool(&unsp, uxs)
	amt := wallet.Balance{10 * 1e6, 0}
	return CreateSpendingTransaction(w, uncf, &unsp, now, amt, a)
}
Пример #5
0
func makeInvalidTxn() (coin.Transaction, error) {
	w := wallet.NewWallet("test")
	w.GenerateAddresses(2)
	uncf := NewUnconfirmedTxnPool()
	now := tNow()
	a := makeAddress()
	uxs := makeUxBalancesForAddresses([]wallet.Balance{}, now, w.GetAddresses()[:2])
	unsp := coin.NewUnspentPool()
	addUxArrayToUnspentPool(&unsp, uxs)
	amt := wallet.Balance{25 * 1e6, 0}
	txn, err := CreateSpendingTransaction(w, uncf, &unsp, now, amt, a)
	if err != nil {
		return txn, err
	}

	txn.Out[0].Address = cipher.Address{}
	return txn, nil
}
Пример #6
0
func TestCreateSpendingTransaction(t *testing.T) {
	// Setup

	w := wallet.NewWallet("fortest.wlt")

	w.GenerateAddresses(4)
	uncf := NewUnconfirmedTxnPool()
	now := tNow()
	a := makeAddress()

	// Failing createSpends
	amt := wallet.Balance{0, 0}
	unsp := coin.NewUnspentPool()
	_, err := CreateSpendingTransaction(w, uncf, &unsp, now, amt, a)
	assert.NotNil(t, err)

	// Valid txn, fee, no change
	uxs := makeUxBalancesForAddresses([]wallet.Balance{
		wallet.Balance{10e6, 150},
		wallet.Balance{15e6, 150},
	}, now, w.GetAddresses()[:2])
	unsp = coin.NewUnspentPool()
	addUxArrayToUnspentPool(&unsp, uxs)
	amt = wallet.Balance{25e6, 200}
	tx, err := CreateSpendingTransaction(w, uncf, &unsp, now, amt, a)
	assert.Nil(t, err)
	assert.Equal(t, len(tx.Out), 1)
	assert.Equal(t, tx.Out[0], coin.TransactionOutput{
		Coins:   25e6,
		Hours:   37,
		Address: a,
	})
	assert.Equal(t, len(tx.In), 2)
	assert.Equal(t, tx.In, []cipher.SHA256{uxs[0].Hash(), uxs[1].Hash()})
	assert.Nil(t, tx.Verify())

	// Valid txn, change
	uxs = makeUxBalancesForAddresses([]wallet.Balance{
		wallet.Balance{10e6, 150},
		wallet.Balance{15e6, 200},
		wallet.Balance{1e6, 125},
	}, now, w.GetAddresses()[:3])
	unsp = coin.NewUnspentPool()
	addUxArrayToUnspentPool(&unsp, uxs)
	amt = wallet.Balance{25e6, 200}
	tx, err = CreateSpendingTransaction(w, uncf, &unsp, now, amt, a)
	assert.Nil(t, err)
	assert.Equal(t, len(tx.Out), 2)
	assert.Equal(t, tx.Out[0], coin.TransactionOutput{
		Coins:   1e6,
		Hours:   (150 + 200 + 125) / 8,
		Address: w.GetAddresses()[0],
	})
	assert.Equal(t, tx.Out[1], coin.TransactionOutput{
		Coins:   25e6,
		Hours:   (150 + 200 + 125) / 8,
		Address: a,
	})
	assert.Equal(t, len(tx.In), 3)
	assert.Equal(t, tx.In, []cipher.SHA256{
		uxs[0].Hash(), uxs[1].Hash(), uxs[2].Hash(),
	})
	assert.Nil(t, tx.Verify())

	// Would be valid, but unconfirmed subtraction causes it to not be
	// First, make a txn to subtract
	uxs = makeUxBalancesForAddresses([]wallet.Balance{
		wallet.Balance{10e6, 150},
		wallet.Balance{15e6, 150},
	}, now, w.GetAddresses()[:2])
	unsp = coin.NewUnspentPool()
	addUxArrayToUnspentPool(&unsp, uxs)
	amt = wallet.Balance{25e6, 200}
	tx, err = CreateSpendingTransaction(w, uncf, &unsp, now, amt, a)
	assert.Nil(t, err)
	// Add it to the unconfirmed pool (bypass InjectTxn to avoid blockchain)
	uncf.Txns[tx.Hash()] = uncf.createUnconfirmedTxn(&unsp, tx)
	// Make a spend that must not reuse previous addresses
	_, err = CreateSpendingTransaction(w, uncf, &unsp, now, amt, a)
	assertError(t, err, "Not enough coins")
}
Пример #7
0
func (self *WalletRPC) CreateWallet(seed string) wallet.Wallet {
	w := wallet.NewWallet(seed)
	self.Wallets.Add(w)
	return w
}