sorare/mutations/authorization_request.go

71 lines
2.4 KiB
Go

package mutations
import (
"github.com/pkg/errors"
"git.lehouerou.net/laurent/sorare/starkware"
)
type Request struct {
Typename string `graphql:"__typename"`
LimitOrder LimitOrder `graphql:"... on StarkexLimitOrderAuthorizationRequest"`
Transfer Transfer `graphql:"... on StarkexTransferAuthorizationRequest"`
MangoPayWalletTransfer MangopayWalletTransfer `graphql:"... on MangopayWalletTransferAuthorizationRequest"`
}
type AuthorizationRequest struct {
Fingerprint string `json:"fingerprint"`
Request Request `json:"request"`
}
func (r AuthorizationRequest) sign(privatekey string) (AuthorizationApprovalInput, error) {
switch r.Request.Typename {
case "StarkexLimitOrderAuthorizationRequest":
hash, err := r.Request.LimitOrder.hash()
if err != nil {
return AuthorizationApprovalInput{}, errors.Wrap(err, "hashing limit order")
}
signature := starkware.Sign(hash, privatekey)
return AuthorizationApprovalInput{
Fingerprint: r.Fingerprint,
StarkExLimitOrderApproval: &StarkExApprovalInput{
ExpirationTimestamp: r.Request.LimitOrder.ExpirationTimestamp,
Nonce: r.Request.LimitOrder.Nonce,
Signature: signature,
},
}, nil
case "StarkexTransferAuthorizationRequest":
hash, err := r.Request.Transfer.hash()
if err != nil {
return AuthorizationApprovalInput{}, errors.Wrap(err, "hashing transfer")
}
signature := starkware.Sign(hash, privatekey)
return AuthorizationApprovalInput{
Fingerprint: r.Fingerprint,
StarkExTransferApproval: &StarkExApprovalInput{
ExpirationTimestamp: r.Request.Transfer.ExpirationTimestamp,
Nonce: r.Request.Transfer.Nonce,
Signature: signature,
},
}, nil
case "MangopayWalletTransferAuthorizationRequest":
hash, err := r.Request.MangoPayWalletTransfer.hash()
if err != nil {
return AuthorizationApprovalInput{}, errors.Wrap(err, "hashing transfer")
}
signature := starkware.Sign(hash, privatekey)
return AuthorizationApprovalInput{
Fingerprint: r.Fingerprint,
MangopayWalletTransferApproval: &MangopayWalletTransferApprovalInput{
Nonce: r.Request.MangoPayWalletTransfer.Nonce,
Signature: signature,
},
}, nil
}
return AuthorizationApprovalInput{}, errors.Errorf(
"unsupported request type %s",
r.Request.Typename,
)
}