Skip to content

Commit beb2d7c

Browse files
author
akfoster
authored
Merge pull request #14 from String-xyz/task/andrin/str-154
Task/andrin/str 154
2 parents e4e0807 + 638243e commit beb2d7c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1649
-455
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ REDIS_PORT=6379
2323
JWT_SECRET_KEY=hskdhfjfkdhsgafgwterurorhfh
2424
UNIT21_API_KEY=
2525
UNIT21_URL=https://sandbox2-api.unit21.com/v1/
26+
UNIT21_ORG_NAME=String
2627
TWILIO_ACCOUNT_SID=AC034879a536d54325687e48544403cb4d
2728
TWILIO_AUTH_TOKEN=
2829
TWILIO_SMS_SID=MG367a4f51ea6f67a28db4d126eefc734f

api/api.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func baseMiddleware(logger *zerolog.Logger, e *echo.Echo) {
4848
func authRoute(config APIConfig, e *echo.Echo) service.Auth {
4949
a := repository.NewAuth(config.Redis, config.DB)
5050
u := repository.NewUser(config.DB)
51-
c := repository.NewUserContact(config.DB)
51+
c := repository.NewContact(config.DB)
5252
service := service.NewAuth(a, u, c)
5353
handler := handler.NewAuth(service)
5454
handler.RegisterRoutes(e.Group("/auth"))
@@ -58,7 +58,7 @@ func authRoute(config APIConfig, e *echo.Echo) service.Auth {
5858
func platformRoute(config APIConfig, e *echo.Echo) {
5959
p := repository.NewPlatform(config.DB)
6060
a := repository.NewAuth(config.Redis, config.DB)
61-
c := repository.NewUserContact(config.DB)
61+
c := repository.NewContact(config.DB)
6262
service := service.NewPlatform(p, c, a)
6363
handler := handler.NewPlatform(service)
6464
handler.RegisterRoutes(e.Group("/platform"), middleware.BearerAuth())
@@ -96,6 +96,6 @@ func verificationRoute(config APIConfig, e *echo.Echo) {
9696
Instrument: repository.NewInstrument(config.DB),
9797
}
9898
service := service.NewUser(repos)
99-
handler := handler.NewUser(e, service)
99+
handler := handler.NewVerification(e, service)
100100
handler.RegisterRoutes(e.Group("/verification"))
101101
}

migrations/0001_string-user_platform_asset_network.sql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,26 +108,26 @@ CREATE INDEX network_gas_token_id_fk ON network (gas_token_id);
108108
-- ASSET ----------------------------------------------------------------
109109
DROP TRIGGER IF EXISTS update_asset_updated_at ON asset;
110110
DROP INDEX IF EXISTS network_gas_token_id_fk;
111-
DROP TABLE asset;
111+
DROP TABLE IF EXISTS asset;
112112

113113
-------------------------------------------------------------------------
114114
-- NETWORK --------------------------------------------------------------
115115
DROP TRIGGER IF EXISTS update_network_updated_at ON network;
116-
DROP TABLE network;
116+
DROP TABLE IF EXISTS network;
117117

118118
-------------------------------------------------------------------------
119119
-- PLATFORM -------------------------------------------------------------
120120
DROP TRIGGER IF EXISTS update_platform_updated_at ON platfom;
121-
DROP TABLE platform;
121+
DROP TABLE IF EXISTS platform;
122122

123123
-------------------------------------------------------------------------
124124
-- STRING_USER ----------------------------------------------------------
125125
DROP TRIGGER IF EXISTS update_string_user_updated_at ON string_user;
126-
DROP TABLE string_user;
126+
DROP TABLE IF EXISTS string_user;
127127

128128
-------------------------------------------------------------------------
129129
-- UPDATE_UPDATED_AT_COLUMN() -------------------------------------------
130-
DROP FUNCTION update_updated_at_column;
130+
DROP FUNCTION IF EXISTS update_updated_at_column;
131131

132132
-------------------------------------------------------------------------
133133
-- UUID EXTENSION -------------------------------------------------------

migrations/0002_device_contact_location_instrument.sql renamed to migrations/0002_user-platform_device_contact_location_instrument.sql

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
-------------------------------------------------------------------------
22
-- +goose Up
33

4+
-------------------------------------------------------------------------
5+
-- USER_PLATFORM --------------------------------------------------------
6+
CREATE TABLE user_platform (
7+
user_id UUID REFERENCES string_user (id),
8+
platform_id UUID REFERENCES platform (id)
9+
);
10+
11+
CREATE UNIQUE INDEX user_platform_user_id_platform_id_idx ON user_platform(user_id, platform_id);
12+
413
-------------------------------------------------------------------------
514
-- DEVICE ---------------------------------------------------------------
615
CREATE TABLE device (
@@ -13,7 +22,7 @@ CREATE TABLE device (
1322
type TEXT DEFAULT '', -- enum: to be defined at struct level in Go
1423
description TEXT DEFAULT '',
1524
fingerprint TEXT DEFAULT '',
16-
ip_addresses JSONB DEFAULT '[]'::JSONB,
25+
ip_addresses TEXT[] DEFAULT NULL,
1726
user_id UUID NOT NULL REFERENCES string_user (id)
1827
);
1928

@@ -96,19 +105,23 @@ EXECUTE PROCEDURE update_updated_at_column();
96105
-------------------------------------------------------------------------
97106
-- INSTRUMENT -----------------------------------------------------------
98107
DROP TRIGGER IF EXISTS update_instrument_updated_at ON instrument;
99-
DROP TABLE instrument;
108+
DROP TABLE IF EXISTS instrument;
100109

101110
-------------------------------------------------------------------------
102111
-- LOCATION -----------------------------------------------------------
103112
DROP TRIGGER IF EXISTS update_location_updated_at ON location;
104-
DROP TABLE location;
113+
DROP TABLE IF EXISTS location;
105114

106115
-------------------------------------------------------------------------
107116
-- CONTACT --------------------------------------------------------------
108117
DROP TRIGGER IF EXISTS update_contact_updated_at ON contact;
109-
DROP TABLE contact;
118+
DROP TABLE IF EXISTS contact;
110119

111120
-------------------------------------------------------------------------
112121
-- DEVICE ---------------------------------------------------------------
113122
DROP TRIGGER IF EXISTS update_device_updated_at ON device;
114-
DROP TABLE device;
123+
DROP TABLE IF EXISTS device;
124+
125+
-------------------------------------------------------------------------
126+
-- USER_PLATFORM -----------------------------------------------------
127+
DROP TABLE IF EXISTS user_platform;

migrations/0003_contact-platform_device-instrument_tx-leg_transaction.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,17 @@ EXECUTE PROCEDURE update_updated_at_column();
9393
-------------------------------------------------------------------------
9494
-- TRANSACTION ----------------------------------------------------------
9595
DROP TRIGGER IF EXISTS update_transaction_updated_at ON transaction;
96-
DROP TABLE transaction;
96+
DROP TABLE IF EXISTS transaction;
9797

9898
-------------------------------------------------------------------------
9999
-- TX_LEG ---------------------------------------------------------------
100100
DROP TRIGGER IF EXISTS update_tx_leg_updated_at ON tx_leg;
101-
DROP TABLE tx_leg;
101+
DROP TABLE IF EXISTS tx_leg;
102102

103103
-------------------------------------------------------------------------
104104
-- DEVICE_INSTRUMENT ----------------------------------------------------
105-
DROP TABLE device_instrument;
105+
DROP TABLE IF EXISTS device_instrument;
106106

107107
-------------------------------------------------------------------------
108108
-- CONTACT_PLATFORM -----------------------------------------------------
109-
DROP TABLE contact_platform;
109+
DROP TABLE IF EXISTS contact_platform;

migrations/0004_auth_strategy.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ EXECUTE PROCEDURE update_updated_at_column();
1717

1818
-- +goose Down
1919
DROP TRIGGER IF EXISTS update_auth_strategy_updated_at ON auth_strategy;
20-
DROP TABLE auth_strategy;
20+
DROP TABLE IF EXISTS auth_strategy;

pkg/internal/common/util.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package common
33
import (
44
"crypto/sha256"
55
"encoding/hex"
6+
"log"
7+
"math"
68
"reflect"
9+
"strconv"
710

811
"github.com/ethereum/go-ethereum/accounts"
912
ethcomm "github.com/ethereum/go-ethereum/common"
@@ -29,6 +32,17 @@ func RecoverAddress(message string, signature string) (ethcomm.Address, error) {
2932
return crypto.PubkeyToAddress(*recovered), nil
3033
}
3134

35+
func BigNumberToFloat(bigNumber string, decimals uint64) (floatReturn float64, err error) {
36+
floatReturn, err = strconv.ParseFloat(bigNumber, 64)
37+
if err != nil {
38+
log.Printf("Failed to convert bigNumber to float: %s", err)
39+
err = StringError(err)
40+
return
41+
}
42+
floatReturn = floatReturn * math.Pow(10, -float64(decimals))
43+
return
44+
}
45+
3246
func isNil(i interface{}) bool {
3347
if i == nil {
3448
return true

pkg/internal/common/util_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package common
33
import (
44
"testing"
55

6+
"github.com/String-xyz/string-api/pkg/model"
67
"github.com/stretchr/testify/assert"
78
)
89

@@ -12,3 +13,11 @@ func TestRecoverSignature(t *testing.T) {
1213
assert.NoError(t, err)
1314
assert.Equal(t, "0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC", addr.Hex())
1415
}
16+
17+
func TestKeysAndValues(t *testing.T) {
18+
mType := "type"
19+
m := model.ContactUpdates{Type: &mType}
20+
names, vals := KeysAndValues(m)
21+
assert.Len(t, names, 1)
22+
assert.Len(t, vals, 1)
23+
}

pkg/internal/unit21/base.go

Lines changed: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package unit21
33
import (
44
"bytes"
55
"encoding/json"
6+
"fmt"
67
"io/ioutil"
78
"log"
89
"net/http"
@@ -12,25 +13,10 @@ import (
1213
"github.com/String-xyz/string-api/pkg/internal/common"
1314
)
1415

15-
// type Unit21Data interface {
16-
// Create(any) (unit21Id string, err error)
17-
// Update(id string, updates any) (err error)
18-
// }
19-
20-
// type unit21Data struct {
21-
// repository repository.Transactable
22-
// }
23-
24-
// func newUnit21Data(repo repository.Transactable) Unit21Data {
25-
// return &unit21Data{repository: repo}
26-
// }
27-
28-
// u21type == entities
29-
// jsonBody = MapUserToEntity(user)
3016
func create(datatype string, jsonBody any) (body []byte, err error) {
3117
apiKey := os.Getenv("UNIT21_API_KEY")
3218
url := os.Getenv("UNIT21_URL") + datatype + "/create"
33-
log.Printf("url: %s", url)
19+
3420
reqBodyBytes, err := json.Marshal(jsonBody)
3521
if err != nil {
3622
log.Printf("Could not encode %s to bytes: %s", datatype, err)
@@ -54,8 +40,6 @@ func create(datatype string, jsonBody any) (body []byte, err error) {
5440
res, err := client.Do(req)
5541
if err != nil {
5642
log.Printf("Request failed to create %s: %s", datatype, err)
57-
//handle 409 on update that is not allowed
58-
//handle 423, 500, 503 for retries
5943
return nil, common.StringError(err)
6044
}
6145

@@ -69,52 +53,61 @@ func create(datatype string, jsonBody any) (body []byte, err error) {
6953

7054
log.Printf("String of body from response: %s", string(body))
7155

56+
if res.StatusCode != 200 {
57+
log.Printf("Request failed to create %s: %s", datatype, fmt.Sprint(res.StatusCode))
58+
err = common.StringError(fmt.Errorf("request failed with status code %s and return body: %s", fmt.Sprint(res.StatusCode), string(body)))
59+
return
60+
}
61+
7262
return body, nil
7363
}
7464

75-
func update(datatype string, id string, updates any) (body []byte, err error) {
76-
// apiKey := os.Getenv("UNIT21_API_KEY")
77-
// url := os.Getenv("UNIT21_URL") + "/" + datatype + "/" + id + "/update"
65+
func update(datatype string, id string, jsonBody any) (body []byte, err error) {
66+
apiKey := os.Getenv("UNIT21_API_KEY")
67+
orgName := os.Getenv("UNIT21_ORG_NAME")
68+
url := os.Getenv("UNIT21_URL") + orgName + "/" + datatype + "/" + id + "/update"
7869

79-
// names, keyToUpdate := common.KeysAndValues(updates)
70+
reqBodyBytes, err := json.Marshal(jsonBody)
71+
if err != nil {
72+
log.Printf("Could not encode %s to bytes: %s", datatype, err)
73+
return nil, common.StringError(err)
74+
}
8075

81-
// reqBodyBytes, err := json.Marshal(jsonBody)
82-
// if err != nil {
83-
// log.Printf("Could not encode %s to bytes: %s", datatype, err)
84-
// return
85-
// }
76+
bodyReader := bytes.NewReader(reqBodyBytes)
8677

87-
// bodyReader := bytes.NewReader(reqBodyBytes)
78+
req, err := http.NewRequest(http.MethodPut, url, bodyReader)
79+
if err != nil {
80+
log.Printf("Could not create request for %s: %s", datatype, err)
81+
return nil, common.StringError(err)
82+
}
8883

89-
// req, err := http.NewRequest(http.MethodPost, url, bodyReader)
90-
// if err != nil {
91-
// log.Printf("Could not create request for %s: %s", datatype, err)
92-
// return
93-
// }
84+
req.Header.Add("accept", "application/json")
85+
req.Header.Add("content-type", "application/json")
86+
req.Header.Add("u21-key", apiKey)
9487

95-
// req.Header.Add("accept", "application/json")
96-
// req.Header.Add("content-type", "application/json")
97-
// req.Header.Add("u21-key", apiKey)
88+
client := http.Client{Timeout: 10 * time.Second}
9889

99-
// client := http.Client{Timeout: 10 * time.Second}
90+
res, err := client.Do(req)
91+
if err != nil {
92+
log.Printf("Request failed to update %s: %s", datatype, err)
93+
return nil, common.StringError(err)
94+
}
10095

101-
// res, err := client.Do(req)
102-
// if err != nil {
103-
// log.Printf("Request failed to create %s: %s", datatype, err)
104-
// //handle 409 on update that is not allowed
105-
// //handle 423, 500, 503 for retries
106-
// return
107-
// }
96+
defer res.Body.Close()
10897

109-
// defer res.Body.Close()
98+
body, err = ioutil.ReadAll(res.Body)
99+
if err != nil {
100+
log.Printf("Error extracting body from %s update request: %s", datatype, err)
101+
return nil, common.StringError(err)
102+
}
110103

111-
// body, err = ioutil.ReadAll(res.Body)
112-
// if err != nil {
113-
// log.Printf("Error extracting body from %s create request: %s", datatype, err)
114-
// return
115-
// }
104+
log.Printf("String of body from response: %s", string(body))
116105

117-
// log.Printf("String of body from response: %s", string(body))
106+
if res.StatusCode != 200 {
107+
log.Printf("Request failed to update %s: %s", datatype, fmt.Sprint(res.StatusCode))
108+
err = common.StringError(fmt.Errorf("request failed with status code %s and return body: %s", fmt.Sprint(res.StatusCode), string(body)))
109+
return
110+
}
118111

119-
return
112+
return body, nil
120113
}

0 commit comments

Comments
 (0)