add alice.CreateMoneroWallet

This commit is contained in:
noot
2021-10-23 17:03:17 -04:00
parent 9d55f5e858
commit c7ccbfcdb3
4 changed files with 59 additions and 0 deletions

View File

@@ -50,6 +50,9 @@ type Alice interface {
// and returns to her the ether in the contract.
// If time t_1 passes and Claim() has not been called, Alice should call Refund().
Refund() error
// CreateMoneroWallet creates Alice's monero wallet after Bob calls Claim().
CreateMoneroWallet(*monero.PrivateKeyPair) (monero.Address, error)
}
type alice struct {
@@ -190,3 +193,16 @@ func (a *alice) WatchForClaim() (<-chan *monero.PrivateKeyPair, error) {
func (a *alice) Refund() error {
return nil
}
func (a *alice) CreateMoneroWallet(kpB *monero.PrivateKeyPair) (monero.Address, error) {
// got Bob's secret
skAB := monero.SumPrivateSpendKeys(kpB.SpendKey(), a.privkeys.SpendKey())
vkAB := monero.SumPrivateViewKeys(kpB.ViewKey(), a.privkeys.ViewKey())
kpAB := monero.NewPrivateKeyPair(skAB, vkAB)
if err := a.client.GenerateFromKeys(kpAB, "alice-swap-wallet", ""); err != nil {
return "", err
}
return kpAB.Address(), nil
}

View File

@@ -21,6 +21,10 @@ func (n *node) doProtocolAlice() error {
n.outCh = outCh
n.inCh = n.host.ReceivedMessageCh()
// closed when we have received all the expected network messages, and we
// can move on to just watching the contract
setupDone := make(chan struct{})
for {
select {
case <-n.done:
@@ -28,6 +32,29 @@ func (n *node) doProtocolAlice() error {
if err := n.handleMessageAlice(msg.Who, msg.Message); err != nil {
fmt.Printf("failed to handle message: error=%s\n", err)
}
case <-setupDone:
break
}
}
claim, err := n.alice.WatchForClaim()
if err != nil {
return err
}
for {
select {
case <-n.done:
return nil
case kp := <-claim:
fmt.Printf("Bob claimed ether! got secret: %v", kp)
address, err := n.alice.CreateMoneroWallet(kp)
if err != nil {
return err
}
fmt.Printf("successfully created monero wallet from our secrets: address=%s", address)
// TODO: get and print balance
}
}
@@ -88,6 +115,10 @@ func (n *node) handleMessageAlice(who peer.ID, msg net.Message) error {
if msg.Address == "" {
return errors.New("got empty address for locked XMR")
}
// check that XMR was locked in expected account, and confirm amount
n.host.SetNextExpectedMessage(nil)
default:
return errors.New("unexpected message type")
}

View File

@@ -57,8 +57,12 @@ func (n *node) doProtocolBob() error {
fmt.Println("Alice called Ready!")
// contract ready, let's claim our ether
if err := n.bob.RedeemFunds(); err != nil {
return fmt.Errorf("failed to redeem ether: %w", err)
}
case kp := <-refund:
fmt.Println("Alice refunded, got monero account key", kp)
// TODO: generate wallet
}
}
@@ -109,6 +113,7 @@ func (n *node) handleMessageBob(who peer.ID, msg net.Message, setupDone chan str
return errors.New("got empty contract address")
}
n.host.SetNextExpectedMessage(nil)
fmt.Println("got Swap contract address!")
if err := n.bob.SetContract(ethcommon.HexToAddress(msg.Address)); err != nil {

View File

@@ -5,7 +5,9 @@ import (
)
type Client interface {
GetBalance(idx uint) (*getBalanceResponse, error)
Transfer(to Address, accountIdx, amount uint) error
GenerateFromKeys(kp *PrivateKeyPair, filename, password string) error
}
type client struct {
@@ -32,3 +34,8 @@ func (c *client) Transfer(to Address, accountIdx, amount uint) error {
fmt.Printf("transfer: txhash=%s\n", txhash)
return err
}
func (c *client) GenerateFromKeys(kp *PrivateKeyPair, filename, password string) error {
address := kp.Address()
return c.callGenerateFromKeys(kp.sk, kp.vk, address, filename, password)
}