You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
5.0KB

  1. package main
  2. import (
  3. "crypto/sha256"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "math"
  8. "os"
  9. "time"
  10. )
  11. type Offer struct {
  12. ID string `json:"id"`
  13. Date int `json:"date"`
  14. OwnerNodeAddress NodeAddress `json:"ownerNodeAddress"`
  15. Direction string `json:"direction"`
  16. Price int `json:"price"`
  17. MarketPriceMargin float64 `json:"marketPriceMargin"`
  18. UseMarketBasedPrice bool `json:"useMarketBasedPrice"`
  19. Amount int64 `json:"amount"`
  20. MinAmount int64 `json:"minAmount"`
  21. BaseCurrencyCode string `json:"baseCurrencyCode"`
  22. CounterCurrencyCode string `json:"counterCurrencyCode"`
  23. ArbitratorNodeAddresses []NodeAddress `json:"arbitratorNodeAddresses"`
  24. MediatorNodeAddress []NodeAddress `json:"mediatorNodeAddresses"`
  25. PaymentMethodId string `json:"paymentMethodId"`
  26. MakerPaymentAccountId string `json:"makerPaymentAccountId"`
  27. OfferFeePaymentTxId string `json:"offerFeePaymentTxId"`
  28. VersionNR string `json:"versionNr"`
  29. BlockHeightAtOfferCreation int `json:"blockHeightAtOfferCreation"`
  30. TxFee int `json:"txFee"`
  31. MakerFee int `json:"makerFee"`
  32. IsCurrencyForMakerFeeBtc bool `json:"isCurrencyForMakerFeeBtc"`
  33. BuyerSecurityDeposit int `json:"buyerSecurityDeposit"`
  34. SellerSecurityDeposit int `json:"sellerSecurityDeposit"`
  35. MaxTradeLimit int `json:"maxTradeLimit"`
  36. MaxTradePeriod int `json:"maxTradePeriod"`
  37. UseAutoClose bool `json:"useAutoClose"`
  38. UseReOpenAfterAutoClose bool `json:"useReOpenAfterAutoClose"`
  39. LowerClosePrice int `json:"lowerClosePrice"`
  40. UpperClosePrice int `json:"upperClosePrice"`
  41. IsPrivateOffer bool `json:"isPrivateOffer"`
  42. ExtraDataMap map[string]string `json:"extraDataMap"`
  43. ProtocolVersion int `json:"protocolVersion"`
  44. }
  45. type NodeAddress struct {
  46. HostName string `json:"hostName"`
  47. Port int `json:"port"`
  48. }
  49. type AccountPayload struct {
  50. Address string `json:"address"`
  51. PaymentMethodId string `json:"paymentMethodId"`
  52. ID string `json:"id"`
  53. MaxTradePeriod int `json:"maxTradePeriod"`
  54. }
  55. type BisqContract struct {
  56. OfferPayload Offer `json:"offerPayload"`
  57. TradeAmount int `json:"tradeAmount"`
  58. TradePrice int `json:"tradePrice"`
  59. TakerFeeTXID string `json:"takerFeeTxID"`
  60. BuyerNodeAddress NodeAddress `json:"buyerNodeAddress"`
  61. SellerNodeAddress NodeAddress `json:"sellerNodeAddress"`
  62. MediatorNodeAddress NodeAddress `json:"mediatorNodeAddress"`
  63. IsBuyerMakerAndSellerTaker bool `json:"isBuyerMakerAndSellerTaker"`
  64. MakerAccountId string `json:"makerAccountId"`
  65. TakerAccountId string `json:"takerAccountId"`
  66. MakerPaymentAccountPayload AccountPayload `json:"makerPaymentAccountPayload"`
  67. TakerPaymentAccountPayload AccountPayload `json:"takerPaymentAccountPayload"`
  68. MakerPayoutAddressString string `json:"makerPayoutAddressString"`
  69. TakerPayoutAddressString string `json:"takerPayoutAddressString"`
  70. LockTime int `json:"lockTime"`
  71. RefundAgentNodeAddress NodeAddress `json:"refundAgentNodeAddress"`
  72. }
  73. // Ideally this should be 10 minutes, but realistically...
  74. const AverageBlockTime = 9.465
  75. const BisqLockTime = 6 * 24 * 10 // 6 blocks an hour * 24 hours a day * 10 days (for alt coin trades)
  76. func EstimateBlockHeightTime(blockHeight int) time.Time {
  77. loc, _ := time.LoadLocation("America/Vancouver")
  78. return time.Date(2009, 01, 03, 18, 15, 00, 00, loc).Add(time.Minute * time.Duration(int64(math.Round(float64(blockHeight)*AverageBlockTime))))
  79. }
  80. func main() {
  81. contractBytes, _ := ioutil.ReadFile(os.Args[1])
  82. contract := new(BisqContract)
  83. err := json.Unmarshal(contractBytes, contract)
  84. if err == nil {
  85. // Deal with general formatting weirdness. The canonical Bisq hash output is over the contract
  86. // when it has been formatted to separated fields by newlines, and with an indent of 2 spaces
  87. contractOut, _ := json.MarshalIndent(contract, "", " ")
  88. fmt.Printf("Successfully Validated Contract\n")
  89. fmt.Printf("Contract Offer Date: %s\n", time.Unix(int64(contract.OfferPayload.Date/1000), 0))
  90. fmt.Printf("Contract Block Height %d (estimated) / %s (estimated)\n", contract.LockTime-BisqLockTime, EstimateBlockHeightTime(contract.LockTime-BisqLockTime))
  91. fmt.Printf("Contract Hash: %x\n", sha256.Sum256(contractOut))
  92. } else {
  93. fmt.Printf("Error %v", err)
  94. }
  95. }