From 9c2d1aa992ec7bf1cd6602505ce9fca6f0085e22 Mon Sep 17 00:00:00 2001 From: Gregory Bednov Date: Sun, 10 Aug 2025 13:36:45 +0300 Subject: [PATCH] lbc_sdk for types instead of lbc itself (less deps) --- go.mod | 1 + go.sum | 2 ++ main.go | 54 +++++++++++------------------------------------------- 3 files changed, 14 insertions(+), 43 deletions(-) diff --git a/go.mod b/go.mod index c61b1e2..62ca80b 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,6 @@ go 1.24.3 require ( github.com/google/uuid v1.6.0 + github.com/gregorybednov/lbc_sdk v0.0.0-20250810102513-432a51e65f76 github.com/spf13/pflag v1.0.7 ) diff --git a/go.sum b/go.sum index b21c462..fbfea12 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gregorybednov/lbc_sdk v0.0.0-20250810102513-432a51e65f76 h1:e3A+1v+Mjt8nuJcVnHuhHZuh4052KRLCUBpH4g74rVs= +github.com/gregorybednov/lbc_sdk v0.0.0-20250810102513-432a51e65f76/go.mod h1:DBE00+SaYBtD4qw+nOtSTLuF6h9Ia4TkuBMJB+6krik= github.com/spf13/pflag v1.0.7 h1:vN6T9TfwStFPFM5XzjsvmzZkLuaLX+HS+0SeFLRgU6M= github.com/spf13/pflag v1.0.7/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= diff --git a/main.go b/main.go index 93ecf2a..71c1df1 100644 --- a/main.go +++ b/main.go @@ -13,35 +13,11 @@ import ( "time" "github.com/google/uuid" + + types "github.com/gregorybednov/lbc_sdk" "github.com/spf13/pflag" ) -type CommiterTxBody struct { - Type string `json:"type"` - ID string `json:"id"` - Name string `json:"name"` - CommiterPubKey string `json:"commiter_pubkey"` -} - -type PromiseTxBody struct { - Type string `json:"type"` - ID string `json:"id"` - Description string `json:"description"` - Timestamp int64 `json:"timestamp"` -} - -type CommitmentTxBody struct { - Type string `json:"type"` - ID string `json:"id"` - PromiseID string `json:"promise_id"` - CommiterID string `json:"commiter_id"` -} - -type SignedTx struct { - Body any `json:"body"` - Signature string `json:"signature"` -} - type rpcResp struct { JSONRPC string `json:"jsonrpc"` ID any `json:"id"` @@ -62,14 +38,6 @@ type rpcResp struct { } `json:"result"` } -type CompoundTx struct { - Body struct { - Promise *PromiseTxBody `json:"promise"` - Commitment *CommitmentTxBody `json:"commitment"` - } `json:"body"` - Signature string `json:"signature"` -} - const configDir = "./config" const privKeyPath = configDir + "/ed25519.key" const pubKeyPath = configDir + "/ed25519.pub" @@ -109,7 +77,7 @@ func ensureKeypair() (ed25519.PublicKey, ed25519.PrivateKey, error) { return ed25519.PublicKey(pub), ed25519.PrivateKey(priv), nil } -func sendTx(tx SignedTx, rpcURL string) error { +func sendTx(tx types.SignedTx, rpcURL string) error { txBytes, _ := json.Marshal(tx) txB64 := base64.StdEncoding.EncodeToString(txBytes) final := map[string]any{ @@ -141,15 +109,15 @@ func registerCommiter(name, rpcURL string) error { pub, priv, _ := ensureKeypair() pubB64 := base64.StdEncoding.EncodeToString(pub) id := "commiter:" + pubB64 - body := CommiterTxBody{ + body := types.CommiterTxBody{ Type: "commiter", ID: id, Name: name, - CommiterPubKey: pubB64, // ← ты думаешь, а на деле может быть иначе + CommiterPubKey: pubB64, } bodyBytes, _ := json.Marshal(body) sig := ed25519.Sign(priv, bodyBytes) - tx := SignedTx{Body: body, Signature: base64.StdEncoding.EncodeToString(sig)} + tx := types.SignedTx{Body: body, Signature: base64.StdEncoding.EncodeToString(sig)} return sendTx(tx, rpcURL) } @@ -162,13 +130,13 @@ func createPromise(desc, rpcURL string) error { commitmentID := "commitment:" + uuid.NewString() // Объекты тела - promise := &PromiseTxBody{ + promise := &types.PromiseTxBody{ Type: "promise", ID: promiseID, Description: desc, Timestamp: time.Now().Unix(), } - commitment := &CommitmentTxBody{ + commitment := &types.CommitmentTxBody{ Type: "commitment", ID: commitmentID, PromiseID: promiseID, @@ -177,8 +145,8 @@ func createPromise(desc, rpcURL string) error { // Сборка тела для подписи bodyStruct := struct { - Promise *PromiseTxBody `json:"promise"` - Commitment *CommitmentTxBody `json:"commitment"` + Promise *types.PromiseTxBody `json:"promise"` + Commitment *types.CommitmentTxBody `json:"commitment"` }{ Promise: promise, Commitment: commitment, @@ -193,7 +161,7 @@ func createPromise(desc, rpcURL string) error { sigB64 := base64.StdEncoding.EncodeToString(sig) // Формирование транзакции - compound := CompoundTx{ + compound := types.CompoundTx{ Body: bodyStruct, Signature: sigB64, }