/
publication.go
44 lines (36 loc) · 1020 Bytes
/
publication.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
// Copyright (c) 2018/2019 The DevCo developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package ltc
import (
"bytes"
"encoding/hex"
"fmt"
"github.com/devwarrior777/atomicswap/libs"
"github.com/ltcsuite/ltcd/wire"
)
// Publish (broadcast) transaction to the network.
func publish(testnet bool, rpcinfo libs.RPCInfo, tx string) (string, error) {
txBytes, err := hex.DecodeString(tx)
if err != nil {
return "", fmt.Errorf("failed to decode broadcast transaction bytes: %v", err)
}
var broadcastTx wire.MsgTx
err = broadcastTx.Deserialize(bytes.NewReader(txBytes))
if err != nil {
return "", fmt.Errorf("failed to decode broadcast transaction: %v", err)
}
rpcclient, err := startRPC(testnet, rpcinfo)
if err != nil {
return "", err
}
defer func() {
rpcclient.Shutdown()
rpcclient.WaitForShutdown()
}()
txHash, err := sendRawTransaction(rpcclient, &broadcastTx)
if err != nil {
return "", err
}
return txHash.String(), nil
}