Skip to content

Commit

Permalink
feat: add ipfs v2 schema validation using a simple regex (#20)
Browse files Browse the repository at this point in the history
* feat: add ipfs v2 schema validation using a simple regex

* feat: update report schemas.api.md

* feat: change avatar snapshots properties to ipfs v2

* feat: update report schemas.api.md
  • Loading branch information
guidota authored Sep 23, 2021
1 parent b869b84 commit 212f7c7
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 25 deletions.
19 changes: 15 additions & 4 deletions report/schemas.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,17 @@ export namespace I18N {
validate: ValidateFunction<I18N>;
}

// @alpha
export type IPFSv2 = string;

// @alpha
export namespace IPFSv2 {
const // (undocumented)
schema: JSONSchema<IPFSv2>;
const // (undocumented)
validate: ValidateFunction<IPFSv2>;
}

// @alpha
export function isInsideWorldLimits(x: number, y: number): boolean;

Expand Down Expand Up @@ -581,10 +592,10 @@ export namespace SceneParcels {

// @alpha
export type Snapshots = {
face: string;
face256: string;
face128: string;
body: string;
face: IPFSv2;
face256: IPFSv2;
face128: IPFSv2;
body: IPFSv2;
};

// @alpha
Expand Down
20 changes: 20 additions & 0 deletions src/misc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,24 @@ export namespace EthAddress {
const schemaValidator: ValidateFunction<EthAddress> = generateValidator(schema);
export const validate: ValidateFunction<EthAddress> = (ethAddress: any): ethAddress is EthAddress =>
schemaValidator(ethAddress)
}

/**
* IPFSv2 is a data type that describes an IPFS v2 hash
* @alpha
*/
export type IPFSv2 = string

/**
* IPFSv2
* @alpha
*/
export namespace IPFSv2 {
export const schema: JSONSchema<IPFSv2> = {
type: 'string',
pattern: '^(bafy)[a-zA-Z0-9]{55}$'
}
const schemaValidator: ValidateFunction<IPFSv2> = generateValidator(schema);
export const validate: ValidateFunction<IPFSv2> = (hash: any): hash is IPFSv2 =>
schemaValidator(hash)
}
26 changes: 9 additions & 17 deletions src/platform/profile/avatar.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Color3, EthAddress, WearableId } from "../../misc"
import { Color3, EthAddress, IPFSv2, WearableId } from "../../misc"
import { generateValidator, JSONSchema, ValidateFunction } from "../../validation"

/**
* Snapshots
* @alpha
*/
export type Snapshots = {
face: string
face256: string
face128: string
body: string
face: IPFSv2
face256: IPFSv2
face128: IPFSv2
body: IPFSv2
}

/**
Expand All @@ -21,18 +21,10 @@ export namespace Snapshots {
type: 'object',
required: ['face', 'face128', 'face256', 'body'],
properties: {
face: {
type: 'string'
},
face256: {
type: 'string'
},
face128: {
type: 'string'
},
body: {
type: 'string'
}
face: IPFSv2.schema,
face256: IPFSv2.schema,
face128: IPFSv2.schema,
body: IPFSv2.schema
}
}
const schemaValidator: ValidateFunction<Snapshots> = generateValidator(schema);
Expand Down
15 changes: 15 additions & 0 deletions test/hashing.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import expect from 'expect'
import { IPFSv2 } from '../src'
import { testTypeSignature } from './test-utils'

describe('Hashing tests', () => {
const hash: IPFSv2 = 'bafybeiasb5vpmaounyilfuxbd3lryvosl4yefqrfahsb2esg46q6tu6y5q'

testTypeSignature(IPFSv2, hash)

it('static tests must pass', () => {
expect(IPFSv2.validate(hash)).toEqual(true)
expect(IPFSv2.validate(null)).toEqual(false)
expect(IPFSv2.validate({})).toEqual(false)
})
})
8 changes: 4 additions & 4 deletions test/platform/profiles/avatar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { testTypeSignature } from '../../test-utils'
const AVATAR_INFO = {
bodyShape: 'urn:decentraland:off-chain:base-avatars:BaseMale',
snapshots: {
face: 'https://peer.decentraland.org/content/contents/QmZdwrWnF2kLghFJ9kSj2brFEmywfAiqssr2LCqFj9HVWi',
face: 'bafybeiasb5vpmaounyilfuxbd3lryvosl4yefqrfahsb2esg46q6tu6y5q',
face128:
'https://peer.decentraland.org/content/contents/QmefLJryuN2Zyv44iHALWsGghAF3MsAthauoAnHAbFi5Mv',
'bafybeiasb5vpmaounyilfuxbd3lryvosl4yefqrfahsb2esg46q6tu6y5r',
face256:
'https://peer.decentraland.org/content/contents/QmNj97kkczscWiJKax44hZQq9ahfBdA5nNKTs9s9AYidh9',
body: 'https://peer.decentraland.org/content/contents/QmWDjKPd9oac2KwzUvWdqHefjvcm66CrNM393QFwkS7Dhu',
'bafybeiasb5vpmaounyilfuxbd3lryvosl4yefqrfahsb2esg46q6tu6y5s',
body: 'bafybeiasb5vpmaounyilfuxbd3lryvosl4yefqrfahsb2esg46q6tu6y5t',
},
eyes: { color: { r: 0.23046875, g: 0.625, b: 0.3125 } },
hair: { color: { r: 0.35546875, g: 0.19140625, b: 0.05859375 } },
Expand Down

0 comments on commit 212f7c7

Please sign in to comment.