OSDN Git Service

update
[bytom/shuttle.git] / swap / htlc_test.go
1 package swap
2
3 import (
4         "encoding/json"
5         "fmt"
6         "testing"
7 )
8
9 var contractArgs = HTLCContractArgs{
10         SenderPublicKey:    "a550d20483af8a0ed02d061d1659f1346a16566d4afa93c49dbce9bc5a5bf559",
11         RecipientPublicKey: "6ea28f3f1389efd6a731de070fb38ab69dc93dae6c73b6524bac901b662f601d",
12         BlockHeight:        uint64(1100),
13         Hash:               "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824",
14 }
15
16 var contractValue = AssetAmount{
17         Asset:  "bae7e17bb8f5d0cfbfd87a92f3204da082d388d4c9b10e8dcd36b3d0a18ceb3a",
18         Amount: uint64(200000000),
19 }
20
21 func TestDeployHTLCContract(t *testing.T) {
22         account := AccountInfo{
23                 AccountID: "10CJPO1HG0A02",
24                 Password:  "12345",
25                 TxFee:     uint64(100000000),
26         }
27         // contractArgs := HTLCContractArgs{
28         //      SenderPublicKey:    "a550d20483af8a0ed02d061d1659f1346a16566d4afa93c49dbce9bc5a5bf559",
29         //      RecipientPublicKey: "6ea28f3f1389efd6a731de070fb38ab69dc93dae6c73b6524bac901b662f601d",
30         //      BlockHeight:        uint64(1100),
31         //      Hash:               "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824",
32         // }
33         // contractValue := AssetAmount{
34         //      Asset:  "bae7e17bb8f5d0cfbfd87a92f3204da082d388d4c9b10e8dcd36b3d0a18ceb3a",
35         //      Amount: uint64(20000000000),
36         // }
37         contractUTXOID, err := DeployHTLCContract(account, contractValue, contractArgs)
38         if err != nil {
39                 fmt.Println(err)
40         }
41         fmt.Println("contractUTXOID:", contractUTXOID)
42 }
43
44 func TestBuildUnlockHTLCContractTransaction(t *testing.T) {
45         account := AccountInfo{
46                 AccountID: "10CKAD3000A02",
47                 Password:  "12345",
48                 Receiver:  "00140fdee108543d305308097019ceb5aec3da60ec66",
49                 TxFee:     uint64(100000000),
50         }
51         contractUTXOID := "4de2ad249889499a0c5cc3190061d50390f295d6d00cf86d35df63a723a8dd0e"
52         // contractValue := AssetAmount{
53         //      Asset:  "bae7e17bb8f5d0cfbfd87a92f3204da082d388d4c9b10e8dcd36b3d0a18ceb3a",
54         //      Amount: uint64(20000000000),
55         // }
56         buildTxResp, err := buildUnlockHTLCContractTransaction(account, contractUTXOID, contractValue)
57         if err != nil {
58                 fmt.Println(err)
59         }
60         signingInst, err := json.Marshal(buildTxResp.SigningInstructions[1])
61         if err != nil {
62                 fmt.Println(err)
63         }
64         fmt.Println("raw transaction:", buildTxResp.RawTransaction)
65         fmt.Println("signingInst:", string(signingInst))
66         contractControlProgram, signData, err := decodeRawTransaction(buildTxResp.RawTransaction, contractValue)
67         if err != nil {
68                 fmt.Println(err)
69         }
70         fmt.Println("contractControlProgram:", contractControlProgram)
71         fmt.Println("signData:", signData)
72
73         // preimage := "68656c6c6f" // b'hello'.hex()
74         // recipientSig := ""
75         // signedTransaction, err := signUnlockHTLCContractTransaction(account, preimage, recipientSig, *buildTxResp)
76
77 }
78
79 func TestListAddresses(t *testing.T) {
80         accountID := "10CJPO1HG0A02"
81         addressInfos, err := listAddresses(accountID)
82         if err != nil {
83                 fmt.Println(err)
84         }
85         controlProgram := "00145b0a81adc5c2d68a9967082a09c96e82d62aa058"
86         for _, addressInfo := range addressInfos {
87                 if addressInfo.ControlProgram == controlProgram {
88                         fmt.Println("address:", addressInfo.Address)
89                 }
90         }
91 }
92
93 func TestSignMessage(t *testing.T) {
94         address := "sm1q828d7re2wp20kgx4zyrw4e049k4v0enwdadq40"
95         message := "719b521e9f341b1a07edf3805f8a5c5f9de453b61eb6e60f14a4bf94fa2bf6bc"
96         password := "12345"
97         sig, err := signMessage(address, message, password)
98         if err != nil {
99                 fmt.Println(err)
100         }
101         fmt.Println("signature:", sig)
102 }
103
104 func TestCallHTLCContract(t *testing.T) {
105         account := AccountInfo{
106                 AccountID: "10CKAD3000A02",
107                 Password:  "12345",
108                 Receiver:  "0014a90cd8c57c682e01f3e7553ea18722621be845f2",
109                 TxFee:     uint64(100000000),
110         }
111         contractUTXOID := "b70636e3093faac1d1d8fea6bf3f317bbfe8a9ac63c4ea33aea2c808217d3163"
112         preimage := "68656c6c6f"
113
114         txID, err := CallHTLCContract(account, contractUTXOID, preimage, contractArgs, contractValue)
115         if err != nil {
116                 fmt.Println(err)
117         }
118         fmt.Println("txID:", txID)
119 }