|
| 1 | +package zcoin_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "reflect" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/btcsuite/btcd/chaincfg" |
| 11 | + "github.com/btcsuite/btcutil" |
| 12 | + "github.com/renproject/id" |
| 13 | + "github.com/renproject/multichain/chain/zcoin" |
| 14 | + "github.com/renproject/multichain/compat/bitcoincompat" |
| 15 | + "github.com/renproject/pack" |
| 16 | + |
| 17 | + . "github.com/onsi/ginkgo" |
| 18 | + . "github.com/onsi/gomega" |
| 19 | +) |
| 20 | + |
| 21 | +var _ = Describe("Zcoin", func() { |
| 22 | + Context("when submitting transactions", func() { |
| 23 | + Context("when sending XZC to multiple addresses", func() { |
| 24 | + It("should work", func() { |
| 25 | + // Load private key, and assume that the associated address has |
| 26 | + // funds to spend. You can do this by setting ZCOIN_PK to the |
| 27 | + // value specified in the `./multichaindeploy/.env` file. |
| 28 | + pkEnv := os.Getenv("ZCOIN_PK") |
| 29 | + if pkEnv == "" { |
| 30 | + panic("ZCOIN_PK is undefined") |
| 31 | + } |
| 32 | + wif, err := btcutil.DecodeWIF(pkEnv) |
| 33 | + Expect(err).ToNot(HaveOccurred()) |
| 34 | + |
| 35 | + // PKH |
| 36 | + pkhAddr, err := btcutil.NewAddressPubKeyHash(btcutil.Hash160(wif.PrivKey.PubKey().SerializeCompressed()), &chaincfg.RegressionNetParams) |
| 37 | + Expect(err).ToNot(HaveOccurred()) |
| 38 | + pkhAddrUncompressed, err := btcutil.NewAddressPubKeyHash(btcutil.Hash160(wif.PrivKey.PubKey().SerializeUncompressed()), &chaincfg.RegressionNetParams) |
| 39 | + Expect(err).ToNot(HaveOccurred()) |
| 40 | + log.Printf("PKH %v", pkhAddr.EncodeAddress()) |
| 41 | + log.Printf("PKH (uncompressed) %v", pkhAddrUncompressed.EncodeAddress()) |
| 42 | + |
| 43 | + // Setup the client and load the unspent transaction outputs. |
| 44 | + client := bitcoincompat.NewClient(bitcoincompat.DefaultClientOptions().WithHost("http://127.0.0.1:19232")) |
| 45 | + outputs, err := client.UnspentOutputs(context.Background(), 0, 999999999, pkhAddr) |
| 46 | + Expect(err).ToNot(HaveOccurred()) |
| 47 | + Expect(len(outputs)).To(BeNumerically(">", 0)) |
| 48 | + output := outputs[0] |
| 49 | + |
| 50 | + // Check that we can load the output and that it is equal. |
| 51 | + // Otherwise, something strange is happening with the RPC |
| 52 | + // client. |
| 53 | + output2, _, err := client.Output(context.Background(), output.Outpoint) |
| 54 | + Expect(err).ToNot(HaveOccurred()) |
| 55 | + Expect(reflect.DeepEqual(output, output2)).To(BeTrue()) |
| 56 | + |
| 57 | + // Build the transaction by consuming the outputs and spending |
| 58 | + // them to a set of recipients. |
| 59 | + recipients := []bitcoincompat.Recipient{ |
| 60 | + { |
| 61 | + Address: pkhAddr, |
| 62 | + Value: pack.NewU64((output.Value.Uint64() - 1000) / 2), |
| 63 | + }, |
| 64 | + { |
| 65 | + Address: pkhAddrUncompressed, |
| 66 | + Value: pack.NewU64((output.Value.Uint64() - 1000) / 2), |
| 67 | + }, |
| 68 | + } |
| 69 | + tx, err := zcoin.NewTxBuilder().BuildTx([]bitcoincompat.Output{output}, recipients) |
| 70 | + Expect(err).ToNot(HaveOccurred()) |
| 71 | + |
| 72 | + // Get the digests that need signing from the transaction, and |
| 73 | + // sign them. In production, this would be done using the RZL |
| 74 | + // MPC algorithm, but for the purposes of this test, using an |
| 75 | + // explicit privkey is ok. |
| 76 | + sighashes, err := tx.Sighashes() |
| 77 | + signatures := make([]pack.Bytes65, len(sighashes)) |
| 78 | + Expect(err).ToNot(HaveOccurred()) |
| 79 | + for i := range sighashes { |
| 80 | + hash := id.Hash(sighashes[i]) |
| 81 | + privKey := (*id.PrivKey)(wif.PrivKey) |
| 82 | + signature, err := privKey.Sign(&hash) |
| 83 | + Expect(err).ToNot(HaveOccurred()) |
| 84 | + signatures[i] = pack.NewBytes65(signature) |
| 85 | + } |
| 86 | + Expect(tx.Sign(signatures, pack.NewBytes(wif.SerializePubKey()))).To(Succeed()) |
| 87 | + |
| 88 | + // Submit the transaction to the Zcoin node. Again, this |
| 89 | + // should be running a la `./multichaindeploy`. |
| 90 | + txHash, err := client.SubmitTx(context.Background(), tx) |
| 91 | + Expect(err).ToNot(HaveOccurred()) |
| 92 | + log.Printf("TXID %v", txHash) |
| 93 | + |
| 94 | + for { |
| 95 | + // Loop until the transaction has at least a few |
| 96 | + // confirmations. This implies that the transaction is |
| 97 | + // definitely valid, and the test has passed. We were |
| 98 | + // successfully able to use the multichain to construct and |
| 99 | + // submit a Zcoin transaction! |
| 100 | + confs, err := client.Confirmations(context.Background(), txHash) |
| 101 | + Expect(err).ToNot(HaveOccurred()) |
| 102 | + log.Printf(" %v/3 confirmations", confs) |
| 103 | + if confs >= 3 { |
| 104 | + break |
| 105 | + } |
| 106 | + time.Sleep(10 * time.Second) |
| 107 | + } |
| 108 | + |
| 109 | + // Check that we can load the output and that it is equal. |
| 110 | + // Otherwise, something strange is happening with the RPC |
| 111 | + // client. |
| 112 | + output2, _, err = client.Output(context.Background(), output.Outpoint) |
| 113 | + Expect(err).ToNot(HaveOccurred()) |
| 114 | + Expect(reflect.DeepEqual(output, output2)).To(BeTrue()) |
| 115 | + }) |
| 116 | + }) |
| 117 | + }) |
| 118 | +}) |
0 commit comments