diff --git a/object.go b/object.go deleted file mode 100644 index bb611b3..0000000 --- a/object.go +++ /dev/null @@ -1,134 +0,0 @@ -package bot - -import ( - "bytes" - "context" - "encoding/hex" - "encoding/json" - "fmt" - "time" - "unicode/utf8" - - "github.com/MixinNetwork/go-number" - "github.com/MixinNetwork/msgpack/v4" - "github.com/gofrs/uuid/v5" -) - -type ObjectInput struct { - Amount number.Decimal - TraceId string - Memo string -} - -func CreateObject(ctx context.Context, in *ObjectInput, uid, sid, sessionKey, pin, pinToken string) (*Snapshot, error) { - if in.Amount.Exhausted() { - return nil, fmt.Errorf("amount exhausted") - } - - if len(pin) != 6 { - xin := "c94ac88f-4671-3976-b60a-09064f1811e8" - teamMixin := "773e5e77-4107-45c2-b648-8fc722ed77f5" - tipBody := TipBodyForRawTransactionCreate(xin, "", []string{teamMixin}, 64, in.Amount, in.TraceId, in.Memo) - var err error - pin, err = signTipBody(tipBody, pin) - if err != nil { - return nil, err - } - } - - encryptedPIN, err := EncryptPIN(pin, pinToken, sid, sessionKey, uint64(time.Now().UnixNano())) - if err != nil { - return nil, err - } - - data, err := json.Marshal(map[string]interface{}{ - "amount": in.Amount, - "trace_id": in.TraceId, - "memo": in.Memo, - "pin_base64": encryptedPIN, - }) - if err != nil { - return nil, err - } - - path := "/objects" - token, err := SignAuthenticationToken(uid, sid, sessionKey, "POST", path, string(data)) - if err != nil { - return nil, err - } - - id := UuidNewV4().String() - body, err := RequestWithId(ctx, "POST", path, data, token, id) - if err != nil { - return nil, err - } - - var resp struct { - Data *Snapshot `json:"data"` - Error Error `json:"error"` - } - err = json.Unmarshal(body, &resp) - if err != nil { - return nil, err - } - if resp.Error.Code > 0 { - return nil, resp.Error - } - return resp.Data, nil -} - -func EstimateObjectFee(memo string) number.Decimal { - extra := EncodeMixinExtra(uuid.Nil.String(), memo) - return number.FromString(fmt.Sprint(len(extra)/1024 + 2)).Mul(number.FromString("0.0015")) -} - -type MixinExtraPack struct { - T uuid.UUID - M string `msgpack:",omitempty"` -} - -func EncodeMixinExtra(traceId, memo string) []byte { - id, err := uuid.FromString(traceId) - if err != nil { - panic(err) - } - p := &MixinExtraPack{T: id, M: memo} - b := MsgpackMarshalPanic(p) - if len(b) >= 1024*1024*4 { - panic(memo) - } - return b -} - -func DecodeMixinExtra(b []byte) *MixinExtraPack { - var p MixinExtraPack - err := MsgpackUnmarshal(b, &p) - if err == nil && (p.M != "" || p.T.String() != uuid.Nil.String()) { - return &p - } - - if utf8.Valid(b) { - p.M = string(b) - } else { - p.M = hex.EncodeToString(b) - } - return &p -} - -func MsgpackMarshalPanic(val interface{}) []byte { - var buf bytes.Buffer - enc := msgpack.NewEncoder(&buf).UseCompactEncoding(true).SortMapKeys(true) - err := enc.Encode(val) - if err != nil { - panic(fmt.Errorf("MsgpackMarshalPanic: %#v %s", val, err.Error())) - } - return buf.Bytes() -} - -func MsgpackUnmarshal(data []byte, val interface{}) error { - err := msgpack.Unmarshal(data, val) - if err == nil { - return err - } - return fmt.Errorf("MsgpackUnmarshal: %s %s", hex.EncodeToString(data), err.Error()) -} diff --git a/payment.go b/payment.go deleted file mode 100644 index fce379e..0000000 --- a/payment.go +++ /dev/null @@ -1,82 +0,0 @@ -package bot - -import ( - "context" - "encoding/json" - "time" -) - -type Payment struct { - Type string `json:"type"` - TraceId string `json:"trace_id"` - AssetId string `json:"asset_id"` - Amount string `json:"amount"` - Threshold int64 `json:"threshold"` - Receivers []string `json:"receivers"` - Memo string `json:"memo"` - Status string `json:"status"` - CodeId string `json:"code_id"` - CreatedAt time.Time `json:"created_at"` -} - -type PaymentRequest struct { - AssetId string `json:"asset_id"` - Amount string `json:"amount"` - TraceId string `json:"trace_id"` - Memo string `json:"memo"` - OpponentMultisig struct { - Receivers []string `json:"receivers"` - Threshold int64 `json:"threshold"` - } `json:"opponent_multisig"` -} - -func CreatePaymentRequest(ctx context.Context, payment *PaymentRequest, uid, sid, sessionKey string) (*Payment, error) { - data, err := json.Marshal(payment) - if err != nil { - return nil, err - } - method, path := "POST", "/payments" - token, err := SignAuthenticationToken(uid, sid, sessionKey, method, path, string(data)) - if err != nil { - return nil, err - } - - body, err := Request(ctx, method, path, data, token) - if err != nil { - return nil, ServerError(ctx, err) - } - var resp struct { - Data *Payment `json:"data"` - Error Error `json:"error"` - } - err = json.Unmarshal(body, &resp) - if err != nil { - return nil, BadDataError(ctx) - } - if resp.Error.Code > 0 { - return nil, resp.Error - } - return resp.Data, nil -} - -func ReadPaymentByCode(ctx context.Context, codeId string) (*Payment, error) { - body, err := Request(ctx, "GET", "/codes/"+codeId, nil, "") - if err != nil { - return nil, ServerError(ctx, err) - } - var resp struct { - Data *Payment `json:"data"` - Error Error `json:"error"` - } - err = json.Unmarshal(body, &resp) - if err != nil { - return nil, BadDataError(ctx) - } - if resp.Error.Code > 0 { - return nil, resp.Error - } - return resp.Data, nil -} - -func CreateRaw(ctx context.Context) { -} diff --git a/safe_snapshot.go b/safe_snapshot.go deleted file mode 100644 index 5a9c668..0000000 --- a/safe_snapshot.go +++ /dev/null @@ -1,123 +0,0 @@ -package bot - -import ( - "context" - "encoding/json" - "net/url" - "strconv" - "time" -) - -type SafeDepositView struct { - DepositHash string `json:"deposit_hash"` - DepositIndex int64 `json:"deposit_index"` - Sender string `json:"sender"` -} - -type SafeWithdrawalView struct { - WithdrawalHash string `json:"withdrawal_hash"` - Receiver string `json:"receiver"` -} - -type SafeSnapshot struct { - Type string `json:"type"` - SnapshotID string `json:"snapshot_id"` - UserID string `json:"user_id"` - OpponentID string `json:"opponent_id"` - TransactionHash string `json:"transaction_hash"` - AssetID string `json:"asset_id"` - Amount string `json:"amount"` - Memo string `json:"memo"` - CreatedAt time.Time `json:"created_at"` - - Deposit *SafeDepositView `json:"deposit,omitempty"` - Withdrawal *SafeWithdrawalView `json:"withdrawal,omitempty"` -} - -func SafeSnapshots(ctx context.Context, limit int, app, assetId, opponent, offset, uid, sid, sessionKey string) ([]*SafeSnapshot, error) { - v := url.Values{} - v.Set("limit", strconv.Itoa(limit)) - if app != "" { - v.Set("app", app) - } - if assetId != "" { - v.Set("asset", assetId) - } - if offset != "" { - v.Set("offset", offset) - } - if opponent != "" { - v.Set("opponent", opponent) - } - path := "/safe/snapshots?" + v.Encode() - token, err := SignAuthenticationToken(uid, sid, sessionKey, "GET", path, "") - if err != nil { - return nil, err - } - return SafeSnapshotsByToken(ctx, limit, app, assetId, offset, opponent, token) -} - -func SafeSnapshotsByToken(ctx context.Context, limit int, app, assetId, opponent, offset, accessToken string) ([]*SafeSnapshot, error) { - v := url.Values{} - v.Set("limit", strconv.Itoa(limit)) - if app != "" { - v.Set("app", app) - } - if assetId != "" { - v.Set("asset", assetId) - } - if offset != "" { - v.Set("offset", offset) - } - if opponent != "" { - v.Set("opponent", opponent) - } - path := "/safe/snapshots?" + v.Encode() - body, err := Request(ctx, "GET", path, nil, accessToken) - if err != nil { - return nil, err - } - - var resp struct { - Data []*SafeSnapshot `json:"data"` - Error Error `json:"error"` - } - err = json.Unmarshal(body, &resp) - if err != nil { - return nil, err - } - if resp.Error.Code > 0 { - return nil, resp.Error - } - return resp.Data, nil -} - -func SafeSnapshotById(ctx context.Context, snapshotId string, uid, sid, sessionKey string) (*Snapshot, error) { - path := "/safe/snapshots/" + snapshotId - token, err := SignAuthenticationToken(uid, sid, sessionKey, "GET", path, "") - if err != nil { - return nil, err - } - return SnapshotByToken(ctx, snapshotId, token) -} - -func SafeSnapshotByToken(ctx context.Context, snapshotId string, accessToken string) (*SafeSnapshot, error) { - path := "/safe/snapshots/" + snapshotId - body, err := Request(ctx, "GET", path, nil, accessToken) - if err != nil { - return nil, err - } - - var resp struct { - Data *SafeSnapshot `json:"data"` - Error Error `json:"error"` - } - err = json.Unmarshal(body, &resp) - if err != nil { - return nil, err - } - if resp.Error.Code > 0 { - return nil, resp.Error - } - return resp.Data, nil -} diff --git a/snapshot.go b/snapshot.go index 2a14846..930c15b 100644 --- a/snapshot.go +++ b/snapshot.go @@ -8,159 +8,79 @@ import ( "time" ) -type Snapshot struct { +type SafeDepositView struct { + DepositHash string `json:"deposit_hash"` + DepositIndex int64 `json:"deposit_index"` + Sender string `json:"sender"` +} + +type SafeWithdrawalView struct { + WithdrawalHash string `json:"withdrawal_hash"` + Receiver string `json:"receiver"` +} + +type SafeSnapshot struct { Type string `json:"type"` - SnapshotId string `json:"snapshot_id"` - AssetId string `json:"asset_id"` + SnapshotID string `json:"snapshot_id"` + UserID string `json:"user_id"` + OpponentID string `json:"opponent_id"` + TransactionHash string `json:"transaction_hash"` + AssetID string `json:"asset_id"` Amount string `json:"amount"` - OpeningBalance string `json:"opening_balance"` - ClosingBalance string `json:"closing_balance"` - TransactionHash string `json:"transaction_hash,omitempty"` - SnapshotHash string `json:"snapshot_hash,omitempty"` - SnapshotAt time.Time `json:"snapshot_at,omitempty"` + Memo string `json:"memo"` CreatedAt time.Time `json:"created_at"` - // deposit & withdrawal - OutputIndex int64 `json:"output_index,omitempty"` // deposit - Sender string `json:"sender,omitempty"` // deposit - OpponentId string `json:"opponent_id,omitempty"` // transfer - TraceId string `json:"trace_id,omitempty"` // transfer & raw & withdrawal - Memo string `json:"memo,omitempty"` // transfer & raw & withdrawal - - OpponentKey string `json:"opponent_key"` // raw - OpponentMultisigReceivers []string `json:"opponent_receivers"` // raw - OpponentMultisigThreshold int64 `json:"opponent_threshold"` // raw - State string `json:"state"` // raw & withdrawal - // withdrawal - Receiver string `json:"receiver,omitempty"` - Confirmations int64 `json:"confirmations,omitempty"` - Fee struct { - Amount string `json:"amount"` - AssetId string `json:"asset_id"` - } `json:"fee,omitempty"` + Deposit *SafeDepositView `json:"deposit,omitempty"` + Withdrawal *SafeWithdrawalView `json:"withdrawal,omitempty"` } -type SnapshotShort struct { - Type string `json:"type"` - SnapshotId string `json:"snapshot_id"` - Source string `json:"source"` - Amount string `json:"amount"` - Asset struct { - Type string `json:"type"` - AssetId string `json:"asset_id"` - ChainId string `json:"chain_id"` - MixinId string `json:"mixin_id"` - Symbol string `json:"symbol"` - Name string `json:"name"` - AssetKey string `json:"asset_key"` - IconUrl string `json:"icon_url"` - } `json:"asset"` - State string `json:"state"` - SnapshotHash string `json:"snapshot_hash"` - CreatedAt time.Time `json:"created_at"` - TraceId string `json:"trace_id"` - OpponentId string `json:"opponent_id"` - Memo string `json:"data"` -} - -func Snapshots(ctx context.Context, limit int, offset, assetId, order, uid, sid, sessionKey string) ([]*Snapshot, error) { +func SafeSnapshots(ctx context.Context, limit int, app, assetId, opponent, offset, uid, sid, sessionKey string) ([]*SafeSnapshot, error) { v := url.Values{} v.Set("limit", strconv.Itoa(limit)) - if offset != "" { - v.Set("offset", offset) + if app != "" { + v.Set("app", app) } if assetId != "" { v.Set("asset", assetId) } - if order != "" { - v.Set("order", order) + if offset != "" { + v.Set("offset", offset) + } + if opponent != "" { + v.Set("opponent", opponent) } - - path := "/snapshots?" + v.Encode() + path := "/safe/snapshots?" + v.Encode() token, err := SignAuthenticationToken(uid, sid, sessionKey, "GET", path, "") if err != nil { return nil, err } - return SnapshotsByToken(ctx, limit, offset, assetId, order, token) + return SafeSnapshotsByToken(ctx, limit, app, assetId, offset, opponent, token) } -func SnapshotsByToken(ctx context.Context, limit int, offset, assetId, order, accessToken string) ([]*Snapshot, error) { +func SafeSnapshotsByToken(ctx context.Context, limit int, app, assetId, opponent, offset, accessToken string) ([]*SafeSnapshot, error) { v := url.Values{} v.Set("limit", strconv.Itoa(limit)) - if offset != "" { - v.Set("offset", offset) + if app != "" { + v.Set("app", app) } if assetId != "" { v.Set("asset", assetId) } - if order != "" { - v.Set("order", order) - } - - path := "/snapshots?" + v.Encode() - body, err := Request(ctx, "GET", path, nil, accessToken) - if err != nil { - return nil, err - } - - var resp struct { - Data []*Snapshot `json:"data"` - Error Error `json:"error"` - } - err = json.Unmarshal(body, &resp) - if err != nil { - return nil, err - } - if resp.Error.Code > 0 { - return nil, resp.Error - } - return resp.Data, nil -} - -func SnapshotById(ctx context.Context, snapshotId string, uid, sid, sessionKey string) (*Snapshot, error) { - path := "/snapshots/" + snapshotId - token, err := SignAuthenticationToken(uid, sid, sessionKey, "GET", path, "") - if err != nil { - return nil, err - } - return SnapshotByToken(ctx, snapshotId, token) -} - -func SnapshotByTraceId(ctx context.Context, traceId string, uid, sid, sessionKey string) (*Snapshot, error) { - path := "/snapshots/trace/" + traceId - token, err := SignAuthenticationToken(uid, sid, sessionKey, "GET", path, "") - if err != nil { - return nil, err - } - - body, err := Request(ctx, "GET", path, nil, token) - if err != nil { - return nil, err - } - var resp struct { - Data *Snapshot `json:"data"` - Error Error `json:"error"` - } - err = json.Unmarshal(body, &resp) - if err != nil { - return nil, err + if offset != "" { + v.Set("offset", offset) } - if resp.Error.Code > 0 { - return nil, resp.Error + if opponent != "" { + v.Set("opponent", opponent) } - return resp.Data, nil -} - -func SnapshotByToken(ctx context.Context, snapshotId string, accessToken string) (*Snapshot, error) { - path := "/snapshots/" + snapshotId + path := "/safe/snapshots?" + v.Encode() body, err := Request(ctx, "GET", path, nil, accessToken) if err != nil { return nil, err } var resp struct { - Data *Snapshot `json:"data"` - Error Error `json:"error"` + Data []*SafeSnapshot `json:"data"` + Error Error `json:"error"` } err = json.Unmarshal(body, &resp) if err != nil { @@ -172,65 +92,25 @@ func SnapshotByToken(ctx context.Context, snapshotId string, accessToken string) return resp.Data, nil } -func NetworkSnapshot(ctx context.Context, snapshotId string) (*Snapshot, error) { - return NetworkSnapshotByToken(ctx, snapshotId, "") -} - -func NetworkSnapshotByToken(ctx context.Context, snapshotId, accessToken string) (*Snapshot, error) { - path := "/network/snapshots/" + snapshotId - body, err := Request(ctx, "GET", path, nil, accessToken) - if err != nil { - return nil, err - } - - var resp struct { - Data *Snapshot `json:"data"` - Error Error `json:"error"` - } - err = json.Unmarshal(body, &resp) +func SafeSnapshotById(ctx context.Context, snapshotId string, uid, sid, sessionKey string) (*SafeSnapshot, error) { + path := "/safe/snapshots/" + snapshotId + token, err := SignAuthenticationToken(uid, sid, sessionKey, "GET", path, "") if err != nil { return nil, err } - if resp.Error.Code > 0 { - return nil, resp.Error - } - return resp.Data, nil -} - -func NetworkSnapshots(ctx context.Context, limit int, offset, assetId, order string) ([]*SnapshotShort, error) { - return NetworkSnapshotsByToken(ctx, limit, offset, assetId, order, "", "", "") + return SafeSnapshotByToken(ctx, snapshotId, token) } -func NetworkSnapshotsByToken(ctx context.Context, limit int, offset, assetId, order, uid, sid, sessionKey string) ([]*SnapshotShort, error) { - v := url.Values{} - v.Set("limit", strconv.Itoa(limit)) - if offset != "" { - v.Set("offset", offset) - } - if assetId != "" { - v.Set("asset", assetId) - } - if order == "ASC" || order == "DESC" { - v.Set("order", order) - } - - path := "/network/snapshots?" + v.Encode() - accessToken := "" - if sessionKey != "" { - var err error - accessToken, err = SignAuthenticationToken(uid, sid, sessionKey, "GET", path, "") - if err != nil { - return nil, err - } - } +func SafeSnapshotByToken(ctx context.Context, snapshotId string, accessToken string) (*SafeSnapshot, error) { + path := "/safe/snapshots/" + snapshotId body, err := Request(ctx, "GET", path, nil, accessToken) if err != nil { return nil, err } var resp struct { - Data []*SnapshotShort `json:"data"` - Error Error `json:"error"` + Data *SafeSnapshot `json:"data"` + Error Error `json:"error"` } err = json.Unmarshal(body, &resp) if err != nil {