-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.go
74 lines (57 loc) · 1.71 KB
/
utils.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
67
68
69
70
71
72
73
74
package privatebtc
import (
"context"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"fmt"
)
func generateRPCPasswordSalt(size uint8) (string, error) {
salt := make([]byte, size)
if _, err := rand.Read(salt); err != nil {
return "", fmt.Errorf("read: %w", err)
}
return hex.EncodeToString(salt), nil
}
func rpcPasswordToHMAC(salt, password string) string {
h := hmac.New(sha256.New, []byte(salt))
// sha256.Write() never returns an error
h.Write([]byte(password))
return hex.EncodeToString(h.Sum(nil))
}
func newRPCAuth(user, password string) (string, error) {
const saltSize = 16
salt, err := generateRPCPasswordSalt(saltSize)
if err != nil {
return "", fmt.Errorf("new salt: %w", err)
}
hashedPassword := rpcPasswordToHMAC(salt, password)
return fmt.Sprintf("%s:%s$%s", user, salt, hashedPassword), nil
}
// ReplaceTransactionDrainToAddress performs a replace by fee(RBF) for the given transaction id.
// The new transaction will drain the inputs of the old transaction to the given address while
// increasing the fee by 100%.
func ReplaceTransactionDrainToAddress(
ctx context.Context,
client RPCClient,
txID string,
address string,
) (string, error) {
tx, err := client.GetTransaction(ctx, txID)
if err != nil {
return "", fmt.Errorf("get transaction: %w", err)
}
totalInputs, err := tx.TotalInputsValue(ctx, client)
if err != nil {
return "", fmt.Errorf("total inputs value: %w", err)
}
fee := tx.GetTransactionFee(totalInputs)
newFee := 2 * fee
newAmount := totalInputs - newFee
hash, err := client.SendCustomTransaction(ctx, tx.Vin, map[string]float64{address: newAmount})
if err != nil {
return "", fmt.Errorf("send custom transaction: %w", err)
}
return hash, nil
}