-
Notifications
You must be signed in to change notification settings - Fork 2
/
submarineSwapV1.go
executable file
·62 lines (51 loc) · 1.45 KB
/
submarineSwapV1.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
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 coinSubmarineSwapV1 struct {
Network *chaincfg.Params
OutPoint wire.OutPoint
KeyPath string
Amount btcutil.Amount
RefundAddress string
PaymentHash256 []byte
ServerPublicKey []byte
LockTime int64
}
func (c *coinSubmarineSwapV1) 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)
}
witnessScript, err := swaps.CreateWitnessScriptSubmarineSwapV1(
c.RefundAddress,
c.PaymentHash256,
c.ServerPublicKey,
c.LockTime,
userKey.Network.network,
)
if err != nil {
return err
}
redeemScript, err := createNonNativeSegwitRedeemScript(witnessScript)
if err != nil {
return fmt.Errorf("failed to build reedem script for signing: %w", err)
}
sig, err := signNonNativeSegwitInput(
index, tx, userKey, redeemScript, witnessScript, c.Amount)
if err != nil {
return err
}
txInput := tx.TxIn[index]
txInput.Witness = wire.TxWitness{sig, userKey.PublicKey().Raw(), witnessScript}
return nil
}
func (c *coinSubmarineSwapV1) FullySignInput(index int, tx *wire.MsgTx, userKey, muunKey *HDPrivateKey) error {
return errors.New("cannot fully sign submarine swap transactions")
}