-
Notifications
You must be signed in to change notification settings - Fork 10
/
sign_withdraw.go
59 lines (54 loc) · 1.75 KB
/
sign_withdraw.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
package starkex
import (
"errors"
"fmt"
"github.com/shopspring/decimal"
"math"
"math/big"
"time"
)
// Sign for withdraw
type WithdrawSigner struct {
param WithdrawSignParam
msg struct {
PositionId *big.Int `json:"position_id"`
QuantumAmount *big.Int `json:"quantum_amount"`
Nonce *big.Int `json:"nonce"`
ExpirationEpochHours *big.Int `json:"expiration_epoch_hours"`
}
}
func (s *WithdrawSigner) initMsg() error {
exp, err := time.Parse("2006-01-02T15:04:05.000Z", s.param.Expiration)
if err != nil {
return err
}
QuantumAmount, err := decimal.NewFromString(s.param.HumanAmount)
if err != nil {
return err
}
s.msg.QuantumAmount = QuantumAmount.Mul(resolutionUsdc).BigInt()
s.msg.PositionId = big.NewInt(s.param.PositionId)
s.msg.Nonce = NonceByClientId(s.param.ClientId)
s.msg.ExpirationEpochHours = big.NewInt(int64(math.Ceil(float64(exp.Unix()) / float64(ONE_HOUR_IN_SECONDS))))
return nil
}
func (s *WithdrawSigner) getHash() (string, error) {
net := COLLATERAL_ASSET_ID_BY_NETWORK_ID[s.param.NetworkId]
if net == nil {
return "", errors.New(fmt.Sprintf("invalid network_id: %v", s.param.NetworkId))
}
// packed
packed := big.NewInt(WITHDRAWAL_PREFIX)
packed.Lsh(packed, WITHDRAWAL_FIELD_BIT_LENGTHS["position_id"])
packed.Add(packed, s.msg.PositionId)
packed.Lsh(packed, WITHDRAWAL_FIELD_BIT_LENGTHS["nonce"])
packed.Add(packed, s.msg.Nonce)
packed.Lsh(packed, WITHDRAWAL_FIELD_BIT_LENGTHS["quantums_amount"])
packed.Add(packed, s.msg.QuantumAmount)
packed.Lsh(packed, WITHDRAWAL_FIELD_BIT_LENGTHS["expiration_epoch_hours"])
packed.Add(packed, s.msg.ExpirationEpochHours)
packed.Lsh(packed, WITHDRAWAL_PADDING_BITS)
// pedersen hash
hash := getHash(net.String(), packed.String())
return hash, nil
}