-
Notifications
You must be signed in to change notification settings - Fork 2
/
submarineSwapV2.go
executable file
·66 lines (55 loc) · 1.46 KB
/
submarineSwapV2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package libwallet
import (
"errors"
"fmt"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/muun/libwallet/swaps"
)
type coinSubmarineSwapV2 struct {
Network *chaincfg.Params
OutPoint wire.OutPoint
Amount btcutil.Amount
KeyPath string
PaymentHash256 []byte
UserPublicKey []byte
MuunPublicKey []byte
ServerPublicKey []byte
BlocksForExpiration int64
ServerSignature []byte
}
func (c *coinSubmarineSwapV2) SignInput(index int, tx *wire.MsgTx, userKey *HDPrivateKey,
_ *HDPublicKey) error {
userKey, err := userKey.DeriveTo(c.KeyPath)
if err != nil {
return fmt.Errorf("failed to derive user key: %w", err)
}
if len(c.ServerSignature) == 0 {
return errors.New("swap server must provide signature")
}
witnessScript, err := swaps.CreateWitnessScriptSubmarineSwapV2(
c.PaymentHash256,
c.UserPublicKey,
c.MuunPublicKey,
c.ServerPublicKey,
c.BlocksForExpiration)
if err != nil {
return err
}
sig, err := signNativeSegwitInput(
index, tx, userKey, witnessScript, c.Amount)
if err != nil {
return err
}
txInput := tx.TxIn[index]
txInput.Witness = wire.TxWitness{
sig,
c.ServerSignature,
witnessScript,
}
return nil
}
func (c *coinSubmarineSwapV2) FullySignInput(index int, tx *wire.MsgTx, userKey, muunKey *HDPrivateKey) error {
return errors.New("cannot fully sign submarine swap transactions")
}