diff --git a/config.yaml b/config.yaml index a82d3ad..301f91c 100644 --- a/config.yaml +++ b/config.yaml @@ -19,6 +19,11 @@ verifier: verification_key_path: "./verification_key.json" allowed_age: 18 allowed_citizenships: ["UKR"] + allowed_event_id: "event_id" + allowed_query_selector: "query_selector" + # at least one of these should be correct to pass: + allowed_identity_count: 1 + allowed_identity_timestamp: 1715698750 root_verifier: rpc: evm_rpc_url diff --git a/docs/spec/components/schemas/AirdropParams.yaml b/docs/spec/components/schemas/AirdropParams.yaml new file mode 100644 index 0000000..ebb26b8 --- /dev/null +++ b/docs/spec/components/schemas/AirdropParams.yaml @@ -0,0 +1,26 @@ +allOf: + - $ref: '#/components/schemas/AirdropKey' + - type: object + required: + - attributes + properties: + attributes: + type: object + required: + - event_id + - query_selector + - started_at + properties: + event_id: + type: string + description: Event identifier that is generated during ZKP query creation + example: "304351862882731539112827930982999985591702317710421481944329166111111129570" + started_at: + type: integer + format: int64 + description: Unix timestamp in seconds when airdrop event starts + example: 1716381206 + query_selector: + type: string + description: Query selector that is used for proof generation + example: 123 diff --git a/docs/spec/paths/integrations@airdrop-svc@airdrops@params.yaml b/docs/spec/paths/integrations@airdrop-svc@airdrops@params.yaml new file mode 100644 index 0000000..e6d153b --- /dev/null +++ b/docs/spec/paths/integrations@airdrop-svc@airdrops@params.yaml @@ -0,0 +1,17 @@ +get: + tags: + - Airdrop + summary: Get airdrop event parameters + description: Get an airdrop parameters for integration. + operationId: GetAirdropParams + responses: + 200: + content: + application/vnd.api+json: + schema: + type: object + required: + - data + properties: + data: + $ref: '#/components/schemas/AirdropParams' diff --git a/go.mod b/go.mod index 20830ab..33703a3 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/go-ozzo/ozzo-validation/v4 v4.3.0 github.com/iden3/go-rapidsnark/types v0.0.3 github.com/rarimo/rarimo-core v0.0.0-20231004143803-6b209428ecbf - github.com/rarimo/zkverifier-kit v0.2.0 + github.com/rarimo/zkverifier-kit v0.2.1 github.com/rubenv/sql-migrate v1.6.1 gitlab.com/distributed_lab/ape v1.7.1 gitlab.com/distributed_lab/figure/v3 v3.1.4 diff --git a/go.sum b/go.sum index 38e8437..b7b08cc 100644 --- a/go.sum +++ b/go.sum @@ -2106,8 +2106,12 @@ github.com/rarimo/cosmos-sdk v0.46.7 h1:jU2PiWzc+19SF02cXM0O0puKPeH1C6Q6t2lzJ9s1 github.com/rarimo/cosmos-sdk v0.46.7/go.mod h1:fqKqz39U5IlEFb4nbQ72951myztsDzFKKDtffYJ63nk= github.com/rarimo/rarimo-core v0.0.0-20231004143803-6b209428ecbf h1:NvYhOErW0d7ohn2YzGxQYKssrgVrKOvjrKL1OBQgCB4= github.com/rarimo/rarimo-core v0.0.0-20231004143803-6b209428ecbf/go.mod h1:Onkd0EJP94hw4dT/2KH7QXRwDG4eIGeaMffSjA1i6/s= -github.com/rarimo/zkverifier-kit v0.2.0 h1:FybrsDdpP/x/m0ZtEeU9lcHsztlPtCBLo67unAdbFGA= -github.com/rarimo/zkverifier-kit v0.2.0/go.mod h1:ROaCxusG9WLNwbqlTBKqZCm8tQrNdnwLyRWtS+yxO8o= +github.com/rarimo/zkverifier-kit v0.2.1-rc.4 h1:ZpCY2toKFroHK6lpttrkZ0wvb27JbZU5L15N8KSfblc= +github.com/rarimo/zkverifier-kit v0.2.1-rc.4/go.mod h1:3YDg5dTkDRr4IdfaDHGYetopd6gS/2SuwSeseYTWwNw= +github.com/rarimo/zkverifier-kit v0.2.1-rc.5 h1:NN7iW40q3p6EIfAd7bxF0XdisV8AyrMQJ3g28FUstZw= +github.com/rarimo/zkverifier-kit v0.2.1-rc.5/go.mod h1:3YDg5dTkDRr4IdfaDHGYetopd6gS/2SuwSeseYTWwNw= +github.com/rarimo/zkverifier-kit v0.2.1 h1:t/5L27RIKQEtXmocPcNeta3XV5oxz891g8NFCLeIsao= +github.com/rarimo/zkverifier-kit v0.2.1/go.mod h1:3YDg5dTkDRr4IdfaDHGYetopd6gS/2SuwSeseYTWwNw= github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM= github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/regen-network/cosmos-proto v0.3.1 h1:rV7iM4SSFAagvy8RiyhiACbWEGotmqzywPxOvwMdxcg= diff --git a/internal/config/broadcaster.go b/internal/config/broadcaster.go index 4d6efce..f518dcf 100644 --- a/internal/config/broadcaster.go +++ b/internal/config/broadcaster.go @@ -1,7 +1,6 @@ package config import ( - "crypto/tls" "fmt" "time" @@ -21,7 +20,7 @@ import ( "gitlab.com/distributed_lab/kit/comfig" "gitlab.com/distributed_lab/kit/kv" "google.golang.org/grpc" - "google.golang.org/grpc/credentials" + "google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/keepalive" ) @@ -73,14 +72,14 @@ func (b *broadcasterer) Broadcaster() Broadcaster { panic(fmt.Errorf("broadcaster: invalid airdrop amount: %w", err)) } - // this hack is required to dial gRPC, please test it with remote RPC if you change this code - withInsecure := grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{ - InsecureSkipVerify: true, - })) - cosmosRPC, err := grpc.Dial(cfg.CosmosRPC, withInsecure, grpc.WithKeepaliveParams(keepalive.ClientParameters{ - Time: 10 * time.Second, // wait time before ping if no activity - Timeout: 20 * time.Second, // ping timeout - })) + cosmosRPC, err := grpc.Dial( + cfg.CosmosRPC, + grpc.WithTransportCredentials(insecure.NewCredentials()), + grpc.WithKeepaliveParams(keepalive.ClientParameters{ + Time: 10 * time.Second, // wait time before ping if no activity + Timeout: 20 * time.Second, // ping timeout + }), + ) if err != nil { panic(fmt.Errorf("broadcaster: failed to dial cosmos core rpc: %w", err)) } diff --git a/internal/config/verifier.go b/internal/config/verifier.go index 3944da5..754d44a 100644 --- a/internal/config/verifier.go +++ b/internal/config/verifier.go @@ -8,14 +8,27 @@ import ( "gitlab.com/distributed_lab/kit/kv" ) -const proofEventIDValue = "304358862882731539112827930982999386691702727710421481944329166126417129570" +type GlobalParams struct { + EventID string + QuerySelector string + AirdropStart int64 +} + +type Verifierer struct { + Params GlobalParams + ZkVerifier *zk.Verifier +} -func (c *Config) Verifier() *zk.Verifier { +func (c *Config) Verifier() *Verifierer { return c.verifier.Do(func() interface{} { var cfg struct { - VerificationKeyPath string `fig:"verification_key_path,required"` - AllowedAge int `fig:"allowed_age,required"` - AllowedCitizenships []string `fig:"allowed_citizenships,required"` + VerificationKeyPath string `fig:"verification_key_path,required"` + AllowedAge int `fig:"allowed_age,required"` + AllowedCitizenships []string `fig:"allowed_citizenships,required"` + AllowedQuerySelector string `fig:"allowed_query_selector,required"` + AllowedEventID string `fig:"allowed_event_id,required"` + AllowedIdentityCount int64 `fig:"allowed_identity_count,required"` + AllowedIdentityTimestamp int64 `fig:"allowed_identity_timestamp,required"` } err := figure. @@ -31,14 +44,24 @@ func (c *Config) Verifier() *zk.Verifier { zk.WithVerificationKeyFile(cfg.VerificationKeyPath), zk.WithCitizenships(cfg.AllowedCitizenships...), zk.WithAgeAbove(cfg.AllowedAge), - zk.WithEventID(proofEventIDValue), - zk.WithRootVerifier(c.ProvideVerifier()), + zk.WithProofSelectorValue(cfg.AllowedQuerySelector), + zk.WithEventID(cfg.AllowedEventID), + zk.WithIdentityVerifier(c.ProvideVerifier()), + zk.WithIdentitiesCounter(cfg.AllowedIdentityCount), + zk.WithIdentitiesCreationTimestampLimit(cfg.AllowedIdentityTimestamp), ) if err != nil { panic(fmt.Errorf("failed to initialize passport verifier: %w", err)) } - return v - }).(*zk.Verifier) + return &Verifierer{ + ZkVerifier: v, + Params: GlobalParams{ + AirdropStart: cfg.AllowedIdentityTimestamp, + EventID: cfg.AllowedEventID, + QuerySelector: cfg.AllowedQuerySelector, + }, + } + }).(*Verifierer) } diff --git a/internal/service/handlers/create_airdrop.go b/internal/service/handlers/create_airdrop.go index 0e5ab86..aaddccf 100644 --- a/internal/service/handlers/create_airdrop.go +++ b/internal/service/handlers/create_airdrop.go @@ -22,7 +22,7 @@ func CreateAirdrop(w http.ResponseWriter, r *http.Request) { return } - nullifier := req.Data.Attributes.ZkProof.PubSignals[zk.PubSignalNullifier] + nullifier := req.Data.Attributes.ZkProof.PubSignals[zk.Nullifier] airdrop, err := AirdropsQ(r). FilterByNullifier(nullifier). @@ -38,7 +38,7 @@ func CreateAirdrop(w http.ResponseWriter, r *http.Request) { return } - err = Verifier(r).VerifyProof(req.Data.Attributes.ZkProof, zk.WithAddress(req.Data.Attributes.Address)) + err = Verifier(r).VerifyProof(req.Data.Attributes.ZkProof, zk.WithRarimoAddress(req.Data.Attributes.Address)) if err != nil { if errors.Is(err, identity.ErrContractCall) { Log(r).WithError(err).Error("Failed to verify proof") diff --git a/internal/service/handlers/ctx.go b/internal/service/handlers/ctx.go index d73902f..20c8489 100644 --- a/internal/service/handlers/ctx.go +++ b/internal/service/handlers/ctx.go @@ -4,6 +4,7 @@ import ( "context" "net/http" + "github.com/rarimo/airdrop-svc/internal/config" "github.com/rarimo/airdrop-svc/internal/data" zk "github.com/rarimo/zkverifier-kit" "gitlab.com/distributed_lab/logan/v3" @@ -16,6 +17,7 @@ const ( airdropsQCtxKey airdropAmountCtxKey verifierCtxKey + airdropParamsCtxKey ) func CtxLog(entry *logan.Entry) func(context.Context) context.Context { @@ -48,6 +50,16 @@ func AirdropAmount(r *http.Request) string { return r.Context().Value(airdropAmountCtxKey).(string) } +func CtxAirdropParams(amount config.GlobalParams) func(context.Context) context.Context { + return func(ctx context.Context) context.Context { + return context.WithValue(ctx, airdropParamsCtxKey, amount) + } +} + +func AirdropParams(r *http.Request) config.GlobalParams { + return r.Context().Value(airdropParamsCtxKey).(config.GlobalParams) +} + func CtxVerifier(entry *zk.Verifier) func(context.Context) context.Context { return func(ctx context.Context) context.Context { return context.WithValue(ctx, verifierCtxKey, entry) diff --git a/internal/service/handlers/get_airdrop_params.go b/internal/service/handlers/get_airdrop_params.go new file mode 100644 index 0000000..81c3542 --- /dev/null +++ b/internal/service/handlers/get_airdrop_params.go @@ -0,0 +1,23 @@ +package handlers + +import ( + "net/http" + + "github.com/rarimo/airdrop-svc/resources" + "gitlab.com/distributed_lab/ape" +) + +func GetAirdropParams(w http.ResponseWriter, r *http.Request) { + ape.Render(w, resources.AirdropParamsResponse{ + Data: resources.AirdropParams{ + Key: resources.Key{ + Type: resources.AIRDROP, + }, + Attributes: resources.AirdropParamsAttributes{ + EventId: AirdropParams(r).EventID, + StartedAt: AirdropParams(r).AirdropStart, + QuerySelector: AirdropParams(r).QuerySelector, + }, + }, + }) +} diff --git a/internal/service/router.go b/internal/service/router.go index 85ece8d..4d7bccf 100644 --- a/internal/service/router.go +++ b/internal/service/router.go @@ -19,14 +19,16 @@ func Run(ctx context.Context, cfg *config.Config) { ape.LoganMiddleware(cfg.Log()), ape.CtxMiddleware( handlers.CtxLog(cfg.Log()), - handlers.CtxVerifier(cfg.Verifier()), + handlers.CtxVerifier(cfg.Verifier().ZkVerifier), handlers.CtxAirdropAmount(cfg.Broadcaster().AirdropCoins.String()), + handlers.CtxAirdropParams(cfg.Verifier().Params), ), handlers.DBCloneMiddleware(cfg.DB()), ) r.Route("/integrations/airdrop-svc/airdrops", func(r chi.Router) { r.Post("/", handlers.CreateAirdrop) r.Get("/{nullifier}", handlers.GetAirdrop) + r.Get("/params", handlers.GetAirdropParams) }) cfg.Log().Info("Service started") diff --git a/resources/model_airdrop_params.go b/resources/model_airdrop_params.go new file mode 100644 index 0000000..e6cf2e8 --- /dev/null +++ b/resources/model_airdrop_params.go @@ -0,0 +1,43 @@ +/* + * GENERATED. Do not modify. Your changes might be overwritten! + */ + +package resources + +import "encoding/json" + +type AirdropParams struct { + Key + Attributes AirdropParamsAttributes `json:"attributes"` +} +type AirdropParamsResponse struct { + Data AirdropParams `json:"data"` + Included Included `json:"included"` +} + +type AirdropParamsListResponse struct { + Data []AirdropParams `json:"data"` + Included Included `json:"included"` + Links *Links `json:"links"` + Meta json.RawMessage `json:"meta,omitempty"` +} + +func (r *AirdropParamsListResponse) PutMeta(v interface{}) (err error) { + r.Meta, err = json.Marshal(v) + return err +} + +func (r *AirdropParamsListResponse) GetMeta(out interface{}) error { + return json.Unmarshal(r.Meta, out) +} + +// MustAirdropParams - returns AirdropParams from include collection. +// if entry with specified key does not exist - returns nil +// if entry with specified key exists but type or ID mismatches - panics +func (c *Included) MustAirdropParams(key Key) *AirdropParams { + var airdropParams AirdropParams + if c.tryFindEntry(key, &airdropParams) { + return &airdropParams + } + return nil +} diff --git a/resources/model_airdrop_params_attributes.go b/resources/model_airdrop_params_attributes.go new file mode 100644 index 0000000..57c58f7 --- /dev/null +++ b/resources/model_airdrop_params_attributes.go @@ -0,0 +1,14 @@ +/* + * GENERATED. Do not modify. Your changes might be overwritten! + */ + +package resources + +type AirdropParamsAttributes struct { + // Event identifier that is generated during ZKP query creation + EventId string `json:"event_id"` + // Query selector that is used for proof generation + QuerySelector string `json:"query_selector"` + // Unix timestamp in seconds when airdrop event starts + StartedAt int64 `json:"started_at"` +} diff --git a/verification_key.json b/verification_key.json index e5feb38..28610dd 100644 --- a/verification_key.json +++ b/verification_key.json @@ -1,7 +1,7 @@ { "protocol": "groth16", "curve": "bn128", - "nPublic": 14, + "nPublic": 22, "vk_alpha_1": [ "20491192805390485299153009773594534940189261866228447918068658471970481763042", "9383485363053290200918347156157836566562967994039712273449902621266178545958", @@ -37,12 +37,12 @@ ], "vk_delta_2": [ [ - "18902490647006052583680684949147035157755711876364177649770849967939344884538", - "19432663951169219714199180832722185001550181884984488837398124950285484557723" + "21204827410408122506446917557195812338110952697896395827593406304897719711803", + "20590906045861441741568584566524493502038750999032393069681665652961506506852" ], [ - "14591094483196085995359195833411906307448492969324321979804892684379441339558", - "5999628724148110316059290465418237180160096173074925497597305915922821774275" + "7471107084665849616218546104827024014618805625744070420213522650146939048088", + "3066967044829917804117961114491673070865852395041132880495022328798387841335" ], [ "1", @@ -81,78 +81,118 @@ ], "IC": [ [ - "21327673885005299961818505230691163479042042144639778148003692955425200736186", - "20233959945660576453804329261660391125548013335600690858049840634649293268283", + "13810349122310575403704078853013347309369151753447703791786326627414677459072", + "16455827515876227267586171497629480207925653082502157529557711335466799630822", "1" ], [ - "5903376938520018326778583809880270521491177172275283176042261261236098034824", - "15805384632440663692369401736980130251786386583587437616948123682857972322532", + "5502731839561308051882743002931640915151583383757514039679542127430735563419", + "15872150429876840853343213856232428132451302991784752503262906216986896291163", "1" ], [ - "9326220291848943201059535996242761688950495688802773201752943264311010614902", - "2003297437436365064109892185222923126758724737940241242622558954340106805287", + "17538332907711001350037407257748789321848321294254284052199194570028604348741", + "17895901350185510528353729553956941635302660113168139680658755373983791054630", "1" ], [ - "5366098896536007408381906435441844593345991512005050024745852263243878409037", - "20740059460552597602943382207610363544017420726626979303837290789685920991121", + "13117775202399506047622684710908237913953231357885503145626210152171427382866", + "17791651251957033362507395429674958943311737036563712406507935565091484860016", "1" ], [ - "20404569792849015449375865709121251498645251974471736641306215763339626800609", - "14489212707900410939196283629471436416376431939568850129009863111996688419491", + "7156739713179466161751612035150789123484569946291026312431467590891294722726", + "16817334954090525707447447219526407594801837768921277813391001355387216559766", "1" ], [ - "20251029915052268400121048730174983570679491326509635444509076172427695565733", - "8121503923205860425007604133667740884774435904318668933445724290643798160756", + "1105486057339875248776250102545631713119187659321507059565796256928842475670", + "8576779657634385851337235254224897053869792031319333550973569937271332362103", "1" ], [ - "5660476746545600816022831155597033244613491613266191153995188957757724232729", - "20927590518031292652210500445169357147761075380271057265732882151587454669838", + "1586427483689906270906778671913030196658725330300679227459121716611094522624", + "12250151493616193672524079515913615124390736083266430758978132679082070736503", "1" ], [ - "15225248180906883935064509815159151438282815605390395070094474255232393078622", - "4555050821274533518001959087157774898981791805098919625857641300660114716183", + "14331269442231248946213664368719175358269648296496811185144844271145046128075", + "4504578473513272471101174001875406359206861806200094857941450555655014926204", "1" ], [ - "14715365108364930387879568300526707316984035933059473476586781788323469412350", - "12192870055063999123137717117305345559566777164496051945164400177781475641733", + "8378522010485532731210652416526260732057549276167790357830639389973886146391", + "9366051780338650504209085125834466340785739240395282682221404175615467158882", "1" ], [ - "2082848515453217463677495129326473847336490112872029650204359132614367908122", - "2173997466818478897370776479935485571774315439446720778925042738415651328109", + "12475814140080591901238053531736948881630167641584606410770891914266636531356", + "17221043897825500479822580230151609330745271130594608299523186894169226355768", "1" ], [ - "6646023268191686713660275902219940056013913801689619995927418974603183614887", - "1231806158510607488813861836702722689673636443726119736095369434042057333372", + "2645882221166042489820928506058298499470383064675938380772318667915144297809", + "4734298179120484771523361892014966152029016912863160854860528353074857224220", "1" ], [ - "4377359631235333289892095991643823095145640220475443425003268667238699140586", - "9374374232144989733270988720515633221305302580281530317206103302242955523396", + "9567857692808858239206413862832032835562564858008785822644979611184707223684", + "19076348965990014107168152780673941303082147639714150402178543766036506679330", "1" ], [ - "8110399313240184355618312860633022154554813418871633597599517326739014864140", - "11676550680226129473408430325024673042473466539753500775196687152830088242971", + "20401245179967218028169997077358801175504331329117097120345886920622266991169", + "6446785777339065834337084832598280104620202426736428783034029335952863133119", "1" ], [ - "7024062754746500814827176673987775644012152972941215368751859266417069107618", - "2787628166343310828586499689603219957481834488889340575677676565749708186563", + "776155676532946979959888424798074704428476527237023800368416542613068934067", + "3249659587388559892383520856324043854367576656473423899442906681402377481151", "1" ], [ - "6887254711132875200665861464020284080991043892867693682461427028944957149365", - "1644391122832138403227281593066810887181390293587245530100267365785376294120", + "11632975267683660344585678617409674849389532032693943737658043534019040799391", + "19853940704112148517600365129949484576147288253491107417630957901499663980833", + "1" + ], + [ + "10017241356300463587099896455114785864819649009481878414298019603189013724424", + "9784792466587276528892849476403824009534659834233759742510244302854792591293", + "1" + ], + [ + "10141806996696120756829831768610663287874817951436918674879282362836562901396", + "15819480044311969901216694001547933274084228333972168825680302425035302909224", + "1" + ], + [ + "12697795227058016057164742844633483761402289220209639793241218560848969342075", + "511122406997771412703656749137817255134597137160659435385908038930160656301", + "1" + ], + [ + "1853251752842268735220272371943747786867520235758616012152739916316477265280", + "10740779038384422244027682009805512940070749288151042974137325537340551341039", + "1" + ], + [ + "10740783718945504974207129851625733969105732858303025361331830322203476375693", + "5610119623928883615957473317068719440142659934819056945097985001127677634434", + "1" + ], + [ + "21119833660987212479705273894661079971073560001226727364233603759210735473930", + "717001542689103771815132448423169433345309976717232843175715825028449741401", + "1" + ], + [ + "19310193791217004861342027824053627211387924421371560027467891882657462567810", + "1093953812680329562328589497354144993080166098314008041105274472189384779060", + "1" + ], + [ + "6740311001156590492186415962153902525844321438850883726872413877753154991816", + "21094685979718571696702327338197557037537849119737686436266455893691282880145", "1" ] ]