-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
630 lines (531 loc) · 19.7 KB
/
main.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
package main
import (
"bytes"
"encoding/hex"
"fmt"
"log"
"math/big"
"net/http"
"os"
"strconv"
"strings"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/sessions"
gormsessions "github.com/gin-contrib/sessions/gorm"
"github.com/gin-gonic/gin"
_ "github.com/heroku/x/hmetrics/onload"
"github.com/joho/godotenv"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
"github.com/tkhq/demo-passkey-wallet/internal/alchemy"
"github.com/tkhq/demo-passkey-wallet/internal/db"
"github.com/tkhq/demo-passkey-wallet/internal/ethereum"
"github.com/tkhq/demo-passkey-wallet/internal/models"
"github.com/tkhq/demo-passkey-wallet/internal/turnkey"
"github.com/tkhq/demo-passkey-wallet/internal/types"
)
const SESSION_NAME = "demo_session"
const SESSION_SALT = "demo_session_salt"
const SESSION_USER_ID_KEY = "user_id"
const DROP_AMOUNT_IN_WEI = 50000000000000000
const ONE_ETH_IN_WEI = int64(1000000000000000000)
type bodyLogWriter struct {
gin.ResponseWriter
body *bytes.Buffer
}
func (w bodyLogWriter) Write(b []byte) (int, error) {
w.body.Write(b)
return w.ResponseWriter.Write(b)
}
func ginErrorLogMiddleware(c *gin.Context) {
blw := &bodyLogWriter{body: bytes.NewBufferString(""), ResponseWriter: c.Writer}
c.Writer = blw
c.Next()
statusCode := c.Writer.Status()
if statusCode >= 500 {
//ok this is an request with error, let's make a record for it
// now print body (or log in your preferred way)
log.Printf("Internal error served: %s (status: %d)\n", blw.body.String(), statusCode)
}
}
func main() {
loadEnv()
loadDatabase()
ethereum.Init()
port := os.Getenv("PORT")
if port == "" {
log.Fatal("$PORT must be set")
}
// Note: primary origin should come first (e.g. wallet.tx.xyz)
clientOrigins := os.Getenv("CLIENT_ORIGINS")
if clientOrigins == "" {
log.Fatal("$CLIENT_ORIGINS must be set")
}
origins := strings.Split(clientOrigins, ",")
router := gin.New()
router.Use(gin.Recovery())
router.Use(gin.Logger())
router.Use(ginErrorLogMiddleware)
router.Use(cors.New(cors.Config{
AllowOrigins: origins,
AllowMethods: []string{"GET", "POST"},
AllowHeaders: []string{"content-type"},
AllowCredentials: true,
MaxAge: 600,
}))
store := gormsessions.NewStore(db.Database, true, []byte(SESSION_SALT))
store.Options(sessions.Options{MaxAge: 60 * 60 * 24}) // sessions expire daily
router.Use(sessions.Sessions(SESSION_NAME, store))
err := turnkey.Init(
os.Getenv("TURNKEY_API_HOST"),
os.Getenv("TURNKEY_API_PRIVATE_KEY"),
os.Getenv("TURNKEY_ORGANIZATION_ID"),
)
if err != nil {
log.Fatalf("Unable to initialize Turnkey client: %+v", err)
}
userID, err := turnkey.Client.Whoami()
if err != nil {
log.Fatalf("Unable to use Turnkey client for whoami request: %+v", err)
}
turnkeyWarchestOrganizationId := os.Getenv("TURNKEY_WARCHEST_ORGANIZATION_ID")
turnkeyWarchestPrivateKeyId := os.Getenv("TURNKEY_WARCHEST_PRIVATE_KEY_ID")
if turnkeyWarchestOrganizationId == "" || turnkeyWarchestPrivateKeyId == "" || err != nil {
log.Fatal("Cannot find configuration for Turnkey Warchest org or private key ID! Drop functionality depends on it")
}
turnkeyWarchestPrivateKeyAddress, err := turnkey.Client.GetEthereumAddress(turnkeyWarchestOrganizationId, turnkeyWarchestPrivateKeyId)
if err != nil {
log.Fatalf("Unable to get Turnkey Warchest address: %s", err.Error())
}
log.Printf("Initialized Turnkey client successfully. Turnkey API User UUID: %s\n", userID)
router.GET("/", func(ctx *gin.Context) {
ctx.String(http.StatusOK, fmt.Sprintf("This is the Demo Passkey Wallet backend. Welcome I guess? Head to %s if you're lost.", origins[0]))
})
router.GET("/api/whoami", func(ctx *gin.Context) {
user := getCurrentUser(ctx)
if user != nil {
var subOrganizationId *string
if user.SubOrganizationId.Valid {
subOrganizationId = &user.SubOrganizationId.String
}
ctx.JSON(http.StatusOK, map[string]interface{}{
"userId": user.ID,
"email": user.Email,
"subOrganizationId": subOrganizationId,
})
} else {
// Empty response when there is no current user
ctx.JSON(http.StatusNoContent, nil)
}
})
// Whereas the above performs a "local" whoami in the context of this demo app,
// this endpoint forwards a stamped whoami request to Turnkey.
router.POST("/api/turnkey-whoami", func(ctx *gin.Context) {
var req types.WhoamiRequest
err := ctx.BindJSON(&req)
if err != nil {
ctx.String(http.StatusBadRequest, err.Error())
return
}
status, bodyBytes, err := turnkey.Client.ForwardSignedRequest(req.SignedWhoamiRequest.Url, req.SignedWhoamiRequest.Body, req.SignedWhoamiRequest.Stamp)
if err != nil {
err = errors.Wrap(err, "error while forwarding signed send transaction request")
ctx.JSON(http.StatusInternalServerError, err.Error())
return
}
if status != 200 {
ctx.JSON(http.StatusInternalServerError, fmt.Sprintf("expected 200 when forwarding signed transaction request. Got %d", status))
return
}
subOrganizationId := gjson.Get(string(bodyBytes), "organizationId").String()
subOrganizationName := gjson.Get(string(bodyBytes), "organizationName").String()
userId := gjson.Get(string(bodyBytes), "userId").String()
username := gjson.Get(string(bodyBytes), "username").String()
ctx.JSON(http.StatusOK, map[string]interface{}{
"subOrganizationId": subOrganizationId,
"subOrganizationName": subOrganizationName,
"userId": userId,
"username": username,
})
})
router.GET("/api/registration/:email", func(ctx *gin.Context) {
email := ctx.Param("email")
user, err := models.FindUserByEmail(email)
if err == nil {
ctx.JSON(http.StatusOK, map[string]interface{}{
"userId": user.ID,
"subOrganizationId": user.SubOrganizationId.String,
})
} else {
ctx.JSON(http.StatusNoContent, nil)
}
})
router.POST("/api/register", func(ctx *gin.Context) {
var requestBody types.RegistrationRequest
if err := ctx.BindJSON(&requestBody); err != nil {
ctx.JSON(http.StatusBadRequest, err.Error())
return
}
user, err := models.CreateUser(requestBody.Email)
if err != nil {
ctx.JSON(http.StatusInternalServerError, err)
return
}
subOrgResult, err := turnkey.Client.CreateUserSubOrganization(requestBody.Email, requestBody.Attestation, requestBody.Challenge)
if err != nil {
ctx.JSON(http.StatusInternalServerError, err.Error())
return
}
if _, err = models.UpdateUserTurnkeySubOrganization(user.ID, subOrgResult.SubOrganizationId); err != nil {
ctx.JSON(http.StatusInternalServerError, err.Error())
return
}
pk, err := models.SaveWalletForUser(user, subOrgResult.WalletId, subOrgResult.EthereumAddress)
if err != nil {
ctx.JSON(http.StatusInternalServerError, err.Error())
return
}
log.Printf("Wallet account successfully saved: %+v", pk)
startUserLoginSession(ctx, user.ID)
ctx.String(http.StatusOK, "Account successfully created")
})
router.POST("/api/authenticate", func(ctx *gin.Context) {
var req types.AuthenticationRequest
if err := ctx.BindJSON(&req); err != nil {
ctx.JSON(http.StatusBadRequest, err.Error())
return
}
log.Printf("Current request: %v\n", req)
var subOrganizationId string
if req.SubOrganizationId != "" {
subOrganizationId = req.SubOrganizationId
} else {
status, bodyBytes, err := turnkey.Client.ForwardSignedRequest(req.SignedWhoamiRequest.Url, req.SignedWhoamiRequest.Body, req.SignedWhoamiRequest.Stamp)
if err != nil {
err = errors.Wrap(err, "error while forwarding signed whoami request")
ctx.JSON(http.StatusInternalServerError, err.Error())
return
}
if status != 200 {
ctx.JSON(http.StatusInternalServerError, fmt.Sprintf("expected 200 when forwarding whoami request. Got %d", status))
return
}
subOrganizationId = gjson.Get(string(bodyBytes), "organizationId").String()
}
log.Printf("Suborganization ID: %v\n", subOrganizationId)
user, err := models.FindUserBySubOrganizationId(subOrganizationId)
if err != nil {
ctx.JSON(http.StatusInternalServerError, fmt.Sprintf("Unable to find user for suborg ID %s", subOrganizationId))
return
}
log.Printf("Current user: %v\n", user)
startUserLoginSession(ctx, user.ID)
ctx.String(http.StatusOK, "Successful login")
})
router.POST("/api/logout", func(ctx *gin.Context) {
endUserSession(ctx)
ctx.String(http.StatusNoContent, "")
})
router.GET("/api/wallet", func(ctx *gin.Context) {
user := getCurrentUser(ctx)
if user == nil {
ctx.String(http.StatusForbidden, "no current user")
return
}
wallet, err := models.GetWalletForUser(*user)
if err != nil {
ctx.String(http.StatusInternalServerError, errors.Wrap(err, "unable to retrieve key for current user").Error())
return
}
balance, err := ethereum.GetBalance(wallet.EthereumAddress)
if err != nil {
ctx.String(http.StatusInternalServerError, errors.Wrap(err, "unable to retrieve balance").Error())
return
}
ctx.JSON(http.StatusOK, map[string]interface{}{
"address": wallet.EthereumAddress,
"turnkeyUuid": wallet.TurnkeyUUID,
"balance": formatBalance(balance),
"dropsLeft": wallet.DropsLeft(),
})
})
router.POST("api/wallet/drop", func(ctx *gin.Context) {
user := getCurrentUser(ctx)
if user == nil {
ctx.String(http.StatusForbidden, "no current user")
return
}
wallet, err := models.GetWalletForUser(*user)
if err != nil {
ctx.String(http.StatusInternalServerError, errors.Wrap(err, "unable to retrieve key for current user").Error())
return
}
if wallet.DropsLeft() <= 0 {
ctx.String(http.StatusForbidden, "No more drops left!")
return
}
unsignedDropTx, err := ethereum.ConstructTransfer(turnkeyWarchestPrivateKeyAddress, wallet.EthereumAddress, big.NewInt(DROP_AMOUNT_IN_WEI), nil)
if err != nil {
ctx.String(http.StatusInternalServerError, errors.Wrap(err, "unable to construct drop transfer").Error())
return
}
signedDropTx, err := turnkey.Client.SignTransaction(turnkeyWarchestOrganizationId, turnkeyWarchestPrivateKeyId, hex.EncodeToString(unsignedDropTx))
if err != nil {
ctx.String(http.StatusInternalServerError, errors.Wrap(err, "unable to sign drop transfer").Error())
return
}
txHash, err := ethereum.BroadcastTransaction(signedDropTx)
if err != nil {
ctx.String(http.StatusInternalServerError, errors.Wrap(err, "unable to broadcast drop transfer").Error())
return
}
err = models.RecordDropForWallet(wallet)
if err != nil {
ctx.String(http.StatusInternalServerError, errors.Wrap(err, "unable to persist drop in DB").Error())
return
}
ctx.JSON(http.StatusOK, map[string]interface{}{
"hash": txHash,
})
})
// This handler builds a valid Ethereum transaction from the params passed in.
// Aside from checking for a valid session, nothing "stateful" happens here.
// We could (potentially!) even open this as a public endpoint. No real security risk.
// But we need to authenticate users to retrieve their private key and associated address.
router.POST("api/wallet/construct-tx", func(ctx *gin.Context) {
var params types.ConstructTxParams
err := ctx.BindJSON(¶ms)
if err != nil {
ctx.String(http.StatusBadRequest, err.Error())
return
}
user := getCurrentUser(ctx)
if user == nil {
ctx.String(http.StatusForbidden, "no current user")
return
}
wallet, err := models.GetWalletForUser(*user)
if err != nil {
ctx.String(http.StatusInternalServerError, errors.Wrap(err, "unable to retrieve key for current user").Error())
return
}
amount, err := strconv.ParseFloat(params.Amount, 64)
if err != nil {
ctx.String(http.StatusBadRequest, errors.Wrapf(err, "unable to convert amount (%q) to float", params.Amount).Error())
return
}
unsignedTransaction, err := ethereum.ConstructTransfer(wallet.EthereumAddress, params.Destination, big.NewInt(int64(amount*float64(ONE_ETH_IN_WEI))), nil)
if err != nil {
ctx.String(http.StatusInternalServerError, errors.Wrap(err, "unable to construct transaction").Error())
return
}
ctx.JSON(http.StatusOK, map[string]interface{}{
"unsignedTransaction": hex.EncodeToString(unsignedTransaction),
"address": wallet.EthereumAddress,
"organizationId": user.SubOrganizationId.String,
})
})
router.POST("/api/wallet/send-tx", func(ctx *gin.Context) {
var params types.SendTxParams
err := ctx.BindJSON(¶ms)
if err != nil {
ctx.String(http.StatusBadRequest, err.Error())
return
}
user := getCurrentUser(ctx)
if user == nil {
ctx.String(http.StatusForbidden, "no current user")
return
}
status, responseBytes, err := turnkey.Client.ForwardSignedRequest(
params.SignedSendTx.Url, params.SignedSendTx.Body, params.SignedSendTx.Stamp)
if err != nil {
err = errors.Wrap(err, "error while forwarding signed send transaction request")
ctx.JSON(http.StatusInternalServerError, err.Error())
return
}
if status != 200 {
ctx.JSON(http.StatusInternalServerError, fmt.Sprintf("expected 200 when forwarding signed transaction request. Got %d", status))
return
}
signedTransaction := gjson.Get(string(responseBytes), "activity.result.signTransactionResult.signedTransaction").String()
hash, err := ethereum.BroadcastTransaction(signedTransaction)
if err != nil {
ctx.JSON(http.StatusInternalServerError, fmt.Sprintf("error while broadcasting signed transaction %q", signedTransaction))
return
}
ctx.JSON(http.StatusOK, map[string]interface{}{
"hash": hash,
})
})
router.GET("/api/wallet/history", func(ctx *gin.Context) {
user := getCurrentUser(ctx)
if user == nil {
ctx.String(http.StatusForbidden, "no current user")
return
}
wallet, err := models.GetWalletForUser(*user)
if err != nil {
ctx.String(http.StatusInternalServerError, errors.Wrap(err, "unable to retrieve wallet for current user").Error())
return
}
history, err := alchemy.TransactionHistory(wallet.EthereumAddress)
if err != nil {
ctx.String(http.StatusInternalServerError, errors.Wrap(err, "unable to get transaction history").Error())
return
}
ctx.JSON(http.StatusOK, history)
})
router.POST("/api/wallet/export", func(ctx *gin.Context) {
var req types.ExportRequest
err = ctx.BindJSON(&req)
if err != nil {
ctx.String(http.StatusBadRequest, err.Error())
return
}
bodyBytes, err := turnkey.Client.ForwardSignedActivity(req.SignedExportRequest.Url, req.SignedExportRequest.Body, req.SignedExportRequest.Stamp)
if err != nil {
err = errors.Wrap(err, "error while forwarding signed EXPORT_WALLET activity")
ctx.JSON(http.StatusInternalServerError, err.Error())
return
}
exportBundle := gjson.Get(string(bodyBytes), "activity.result.exportWalletResult.exportBundle").String()
ctx.JSON(http.StatusOK, exportBundle)
})
router.POST("/api/init-recovery", func(ctx *gin.Context) {
var params types.RecoveryParams
err := ctx.BindJSON(¶ms)
if err != nil {
ctx.String(http.StatusBadRequest, err.Error())
return
}
user, err := models.FindUserByEmail(params.Email)
if err != nil {
ctx.String(http.StatusForbidden, "no user found")
return
}
subOrganizationId := user.SubOrganizationId.String
turnkeyUserUuid, err := turnkey.Client.InitRecovery(subOrganizationId, user.Email, params.TargetPublicKey)
if err != nil {
ctx.String(http.StatusInternalServerError, fmt.Sprintf("error while initializing recovery: %s", err.Error()))
return
}
ctx.JSON(http.StatusOK, map[string]interface{}{
"userId": turnkeyUserUuid,
"organizationId": subOrganizationId,
})
})
router.POST("/api/recover", func(ctx *gin.Context) {
var req types.RecoverRequest
err := ctx.BindJSON(&req)
if err != nil {
ctx.String(http.StatusBadRequest, err.Error())
return
}
_, err = turnkey.Client.ForwardSignedActivity(req.SignedRecoverRequest.Url, req.SignedRecoverRequest.Body, req.SignedRecoverRequest.Stamp)
if err != nil {
if strings.Contains(err.Error(), "no valid user found for authenticator") && strings.Contains(err.Error(), "Got status 401") {
// This is a tad weird, but while forwarding `RECOVER_USER`` activities, the "success" indicator isn't the usual "ACTIVITY_STATUS_COMPLETE",
// because the credential used to authenticate the request (in the stamp) is the _temporary_ recovery credential.
// The `RECOVER_USER` activity deletes this temporary credential and adds the new passkey instead, which means it loses any privileges on the org,
// which includes having read access to anything! Hence we expect this authentication error to happen when the activity completes.
// Another, less awkward way to ensure the activity is actually complete would be to poll with our backend API key since it has read permissions.
ctx.JSON(http.StatusOK, map[string]interface{}{})
return
}
err = errors.Wrap(err, "error while forwarding signed RECOVER_USER activity")
ctx.JSON(http.StatusInternalServerError, err.Error())
return
}
ctx.JSON(http.StatusOK, map[string]interface{}{})
})
router.POST("/api/email-auth", func(ctx *gin.Context) {
var params types.EmailAuthParams
err := ctx.BindJSON(¶ms)
if err != nil {
ctx.String(http.StatusBadRequest, err.Error())
return
}
user, err := models.FindUserByEmail(params.Email)
if err != nil {
ctx.String(http.StatusForbidden, "no user found")
return
}
subOrganizationId := user.SubOrganizationId.String
turnkeyUserUuid, turnkeyUserApiKeyId, err := turnkey.Client.EmailAuth(subOrganizationId, user.Email, params.TargetPublicKey)
if err != nil {
ctx.String(http.StatusInternalServerError, fmt.Sprintf("error while performing email auth: %s", err.Error()))
return
}
ctx.JSON(http.StatusOK, map[string]interface{}{
"userId": turnkeyUserUuid,
"organizationId": subOrganizationId,
"apiKeyId": turnkeyUserApiKeyId,
})
})
router.Run(":" + port)
}
// Transform a bigint (representing a wei amount) into a readable
// string ("1.23") representing an amount in ETH.
func formatBalance(balance *big.Int) string {
b := big.NewRat(balance.Int64(), ONE_ETH_IN_WEI)
return b.FloatString(2)
}
func getCurrentUser(ctx *gin.Context) *models.User {
session := sessions.Default(ctx)
// Session.Get returns nil if the session doesn't have a given key
userIdOrNil := session.Get(SESSION_USER_ID_KEY)
if userIdOrNil == nil {
log.Println("session.Get returned nil; no session provided?")
return nil
}
userId := userIdOrNil.(uint)
user, err := models.FindUserById(userId)
if err != nil {
log.Print(fmt.Errorf("error while getting current user \"%d\": %w", userId, err))
return nil
}
return &user
}
func startUserLoginSession(ctx *gin.Context, userId uint) {
log.Println("Starting user session...")
session := sessions.Default(ctx)
session.Set(SESSION_USER_ID_KEY, userId)
log.Printf("SESSION_USER_ID_KEY: %v\n", SESSION_USER_ID_KEY)
log.Printf("User ID: %v\n", userId)
err := session.Save()
if err != nil {
log.Printf("error while saving session for user %d: %+v", userId, err)
}
}
// TODO: make sure iframe creds are cleared
func endUserSession(ctx *gin.Context) {
session := sessions.Default(ctx)
userIdOrNil := session.Get(SESSION_USER_ID_KEY)
if userIdOrNil == nil {
log.Printf("error: trying to end session but no user ID data")
return
}
session.Options(sessions.Options{
MaxAge: -1,
Path: "/",
})
session.Clear()
err := session.Save()
if err != nil {
log.Printf("error while deleting current session: %+v", err)
}
log.Printf("Success: user %d was logged out", userIdOrNil.(uint))
}
func loadDatabase() {
db.Connect()
db.Database.AutoMigrate(&models.User{}, &models.Wallet{})
}
func loadEnv() {
err := godotenv.Load(".env")
// Error out if we're in local. Heroku sets DATABASE_URL automatically.
// When the app runs on Heroku, no .env file is expected.
if err != nil && os.Getenv("DATABASE_URL") == "" {
log.Fatalf("Error loading .env file: %s", err.Error())
}
}