Skip to content

Commit

Permalink
Normalizer versioning fix (#1785)
Browse files Browse the repository at this point in the history
* Normalizer versioning fix

* Fix error

* add logic to find newest service

* Add util-semver package

* Move logic to normalizeServices
  • Loading branch information
jribbink authored Oct 31, 2023
1 parent cc21dbf commit a3143a8
Show file tree
Hide file tree
Showing 25 changed files with 367 additions and 96 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-olives-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@onflow/fcl": patch
---

Fix version normalization of services in FCL
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/fcl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@
"@onflow/util-address": "^1.1.0",
"@onflow/util-invariant": "^1.1.0",
"@onflow/util-logger": "^1.2.2",
"@onflow/util-semver": "^1.0.0",
"@onflow/util-template": "^1.1.0",
"@onflow/util-uid": "^1.1.0",
"cross-fetch": "^3.1.6"
},
"peerDependencies": {
"@react-native-async-storage/async-storage": "^1.13.0",
"expo-web-browser": "^12.0.0",
"expo-linking": "^4.0.0",
"expo-web-browser": "^12.0.0",
"react": "^17.0.0 || ^18.0.0",
"react-native": "^0.0.0-0 || 0.60 - 0.72 || 1000.0.0"
},
Expand Down
19 changes: 9 additions & 10 deletions packages/fcl/src/current-user/build-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import * as rlp from "@onflow/rlp"
import {fetchServices} from "./fetch-services"
import {mergeServices} from "./merge-services"
import {USER_PRAGMA} from "../normalizers/service/__vsn"
import {normalizeService} from "../normalizers/service/service"
import {
normalizeService,
normalizeServices,
} from "../normalizers/service/service"
import {serviceOfType} from "./service-of-type"

function deriveCompositeId(authn) {
return rlp
Expand All @@ -20,19 +24,14 @@ function normalizeData(data) {
return data
}

function findService(type, services) {
return services.find(d => d.type === type)
}

export async function buildUser(data) {
data = normalizeData(data)

var services = mergeServices(
data.services || [],
await fetchServices(data.hks, data.code)
).map(service => normalizeService(service, data))
var services = normalizeServices(
mergeServices(data.services || [], await fetchServices(data.hks, data.code))
)

const authn = findService("authn", services)
const authn = serviceOfType(services, "authn")

return {
...USER_PRAGMA,
Expand Down
2 changes: 0 additions & 2 deletions packages/fcl/src/current-user/merge-services.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {withPrefix} from "@onflow/util-address"

export function mergeServices(sx1 = [], sx2 = []) {
// TODO: Make this smarter
return [...sx1, ...sx2]
Expand Down
13 changes: 12 additions & 1 deletion packages/fcl/src/current-user/service-of-type.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import * as semver from "@onflow/util-semver"

export function serviceOfType(services = [], type) {
return services.find(service => service.type === type)
// Find the greatest version of the service type
return services.reduce(
(mostRecent, service) =>
service.type === type
? !mostRecent || semver.compare(service.f_vsn, mostRecent.f_vsn) > 0
? service
: mostRecent
: mostRecent,
null
)
}
40 changes: 40 additions & 0 deletions packages/fcl/src/current-user/service-of-type.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {serviceOfType} from "./service-of-type"

describe("service-of-type", () => {
it("should choose the most recent version of a service", () => {
const services = [
{
type: "authn",
f_vsn: "1.0.0",
},
{
type: "authn",
f_vsn: "2.0.0",
},
]

const service = serviceOfType(services, "authn")

expect(service).toEqual({
type: "authn",
f_vsn: "2.0.0",
})
})

it("should return null if no service of type exists", () => {
const services = [
{
type: "authn",
f_vsn: "1.0.0",
},
{
type: "authn",
f_vsn: "2.0.0",
},
]

const service = serviceOfType(services, "non-existent")

expect(service).toBe(null)
})
})
6 changes: 5 additions & 1 deletion packages/fcl/src/normalizers/service/account-proof.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
export function normalizeAccountProof(service) {
if (service == null) return null

if (!service["f_vsn"]) {
throw new Error(`FCL Normalizer Error: Invalid account-proof service`)
}

switch (service["f_vsn"]) {
case "1.0.0":
return service

default:
throw new Error(`FCL Normalizer Error: Invalid account-proof service`)
return null
}
}
6 changes: 5 additions & 1 deletion packages/fcl/src/normalizers/service/authn-refresh.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
export function normalizeAuthnRefresh(service) {
if (service == null) return null

if (!service["f_vsn"]) {
throw new Error("Invalid authn-refresh service")
}

switch (service["f_vsn"]) {
case "1.0.0":
return service

default:
throw new Error("Invalid authn-refresh service")
return null
}
}
28 changes: 16 additions & 12 deletions packages/fcl/src/normalizers/service/authn.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,26 @@ import {SERVICE_PRAGMA} from "./__vsn"
export function normalizeAuthn(service) {
if (service == null) return null

if (!service["f_vsn"]) {
return {
...SERVICE_PRAGMA,
type: service.type,
uid: service.id,
endpoint: service.authn,
id: service.pid,
provider: {
address: withPrefix(service.addr),
name: service.name,
icon: service.icon,
},
}
}

switch (service["f_vsn"]) {
case "1.0.0":
return service

default:
return {
...SERVICE_PRAGMA,
type: service.type,
uid: service.id,
endpoint: service.authn,
id: service.pid,
provider: {
address: withPrefix(service.addr),
name: service.name,
icon: service.icon,
},
}
return null
}
}
32 changes: 18 additions & 14 deletions packages/fcl/src/normalizers/service/authz.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,28 @@ import {SERVICE_PRAGMA, IDENTITY_PRAGMA} from "./__vsn"
export function normalizeAuthz(service) {
if (service == null) return null

if (!service["f_vsn"]) {
return {
...SERVICE_PRAGMA,
type: service.type,
uid: service.id,
endpoint: service.endpoint,
method: service.method,
identity: {
...IDENTITY_PRAGMA,
address: withPrefix(service.addr),
keyId: service.keyId,
},
params: service.params,
data: service.data,
}
}

switch (service["f_vsn"]) {
case "1.0.0":
return service

default:
return {
...SERVICE_PRAGMA,
type: service.type,
uid: service.id,
endpoint: service.endpoint,
method: service.method,
identity: {
...IDENTITY_PRAGMA,
address: withPrefix(service.addr),
keyId: service.keyId,
},
params: service.params,
data: service.data,
}
return null
}
}
20 changes: 12 additions & 8 deletions packages/fcl/src/normalizers/service/back-channel-rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@ import {SERVICE_PRAGMA} from "./__vsn"
export function normalizeBackChannelRpc(service) {
if (service == null) return null

if (!service["f_vsn"]) {
return {
...SERVICE_PRAGMA,
type: "back-channel-rpc",
endpoint: service.endpoint,
method: service.method,
params: service.params || {},
data: service.data || {},
}
}

switch (service["f_vsn"]) {
case "1.0.0":
return service

default:
return {
...SERVICE_PRAGMA,
type: "back-channel-rpc",
endpoint: service.endpoint,
method: service.method,
params: service.params || {},
data: service.data || {},
}
return null
}
}
16 changes: 10 additions & 6 deletions packages/fcl/src/normalizers/service/composite-signature.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@ import {sansPrefix} from "@onflow/util-address"
export function normalizeCompositeSignature(resp) {
if (resp == null) return null

if (!resp["f_vsn"]) {
return {
...COMPOSITE_SIGNATURE_PRAGMA,
addr: sansPrefix(resp.addr || resp.address),
signature: resp.signature || resp.sig,
keyId: resp.keyId,
}
}

switch (resp["f_vsn"]) {
case "1.0.0":
return resp

default:
return {
...COMPOSITE_SIGNATURE_PRAGMA,
addr: sansPrefix(resp.addr || resp.address),
signature: resp.signature || resp.sig,
keyId: resp.keyId,
}
return null
}
}
20 changes: 12 additions & 8 deletions packages/fcl/src/normalizers/service/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@ import {SERVICE_PRAGMA} from "./__vsn"
export function normalizeFrame(service) {
if (service == null) return null

if (!service["f_vsn"]) {
return {
old: service,
...SERVICE_PRAGMA,
type: "frame",
endpoint: service.endpoint,
params: service.params || {},
data: service.data || {},
}
}

switch (service["f_vsn"]) {
case "1.0.0":
return service

default:
return {
old: service,
...SERVICE_PRAGMA,
type: "frame",
endpoint: service.endpoint,
params: service.params || {},
data: service.data || {},
}
return null
}
}
20 changes: 12 additions & 8 deletions packages/fcl/src/normalizers/service/local-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@ export function normalizeLocalView(resp) {
resp = {...resp, type: "local-view", method: "VIEW/IFRAME"}
}

if (!resp["f_vsn"]) {
return {
...SERVICE_PRAGMA,
type: resp.type || "local-view",
method: resp.method,
endpoint: resp.endpoint,
data: resp.data || {},
params: resp.params || {},
}
}

switch (resp["f_vsn"]) {
case "1.0.0":
return resp

default:
return {
...SERVICE_PRAGMA,
type: resp.type || "local-view",
method: resp.method,
endpoint: resp.endpoint,
data: resp.data || {},
params: resp.params || {},
}
return null
}
}
Loading

0 comments on commit a3143a8

Please sign in to comment.