-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: list third party collections (#1651)
* feat: list third party collections * feat: remove urn join method * feat: urn tests * feat: missing test and rename
- Loading branch information
1 parent
3294714
commit a65ed26
Showing
13 changed files
with
284 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,106 @@ | ||
import { getURNProtocol, Network } from '@dcl/schemas' | ||
import { getChainIdByNetwork } from 'decentraland-dapps/dist/lib/eth' | ||
|
||
export const VERSION = 1 | ||
export const DELIMITER = ':' | ||
const VERSION = 1 | ||
|
||
export function buildItemURN(type: string, name: string, description: string, category: string, bodyShapeTypes: string) { | ||
/** | ||
* urn:decentraland: | ||
* (?<protocol> | ||
* mainnet| | ||
* ropsten| | ||
* matic| | ||
* mumbai| | ||
* off-chain | ||
* ): | ||
* ( | ||
* (?<type> | ||
* base-avatars| | ||
* collections-v2| | ||
* collections-thirdparty | ||
* ): | ||
* (?<suffix> | ||
* ((?<=base-avatars:)BaseMale|BaseFemale)| | ||
* ((?<=collections-v2)0x[a-fA-F0-9]{40})| | ||
* ((?<=collections-thirdparty:) | ||
* (?<thirdPartyName>[^:|\\s]+) | ||
* (:(?<thirdPartyCollectionId>[^:|\\s]+))? | ||
* (:(?<thirdPartyTokenId>[^:|\\s]+))? | ||
* ) | ||
* ) | ||
* ) | ||
*/ | ||
const baseMatcher = 'urn:decentraland' | ||
const protocolMatcher = '(?<protocol>mainnet|ropsten|matic|mumbai|off-chain)' | ||
const typeMatcher = '(?<type>base-avatars|collections-v2|collections-thirdparty)' | ||
|
||
const baseAvatarsSuffixMatcher = '((?<=base-avatars:)BaseMale|BaseFemale)' | ||
const collectionsSuffixMatcher = '((?<=collections-v2:)0x[a-fA-F0-9]{40})' | ||
const thirdPartySuffixMatcher = | ||
'((?<=collections-thirdparty:)(?<thirdPartyName>[^:|\\s]+)(:(?<thirdPartyCollectionId>[^:|\\s]+))?(:(?<thirdPartyTokenId>[^:|\\s]+))?)' | ||
|
||
const urnRegExp = new RegExp( | ||
`${baseMatcher}:${protocolMatcher}:${typeMatcher}:(?<suffix>${baseAvatarsSuffixMatcher}|${collectionsSuffixMatcher}|${thirdPartySuffixMatcher})` | ||
) | ||
|
||
export enum URNProtocol { | ||
MAINNET = 'mainnet', | ||
ROPSTEN = 'ropsten', | ||
MATIC = 'matic', | ||
MUMBAI = 'mumbai', | ||
OFF_CHAIN = 'off-chain' | ||
} | ||
export enum URNType { | ||
BASE_AVATARS = 'base-avatars', | ||
COLLECTIONS_V2 = 'collections-v2', | ||
COLLECTIONS_THIRDPARTY = 'collections-thirdparty' | ||
} | ||
export type URN = string | ||
|
||
type BaseDecodedURN = { | ||
protocol: URNProtocol | ||
suffix: string | ||
} | ||
export type DecodedURN = BaseDecodedURN & | ||
( | ||
| { type: URNType.BASE_AVATARS | URNType.COLLECTIONS_V2 } | ||
| { | ||
type: URNType.COLLECTIONS_THIRDPARTY | ||
thirdPartyName: string | ||
thirdPartyCollectionId?: string | ||
thirdPartyTokenId?: string | ||
} | ||
) | ||
|
||
export function buildItemURN(type: string, name: string, description: string, category: string, bodyShapeTypes: string): URN { | ||
return `${VERSION}:${type[0]}:${name}:${description}:${category}:${bodyShapeTypes}` | ||
} | ||
|
||
export function getCatalystItemURN(contractAddress: string, tokenId: string) { | ||
return `urn:decentraland:${getURNProtocol(getChainIdByNetwork(Network.MATIC))}:collections-v2:${contractAddress}:${tokenId}` | ||
export function buildThirdPartyURN(thirdPartyName: string, collectionId: string, tokenId?: string) { | ||
let urn = `urn:decentraland:${getNetworkURNProtocol(Network.MATIC)}:collections-thirdparty:${thirdPartyName}:${collectionId}` | ||
if (tokenId) { | ||
urn += `:${tokenId}` | ||
} | ||
return urn | ||
} | ||
|
||
export function buildCatalystItemURN(contractAddress: string, tokenId: string): URN { | ||
return `urn:decentraland:${getNetworkURNProtocol(Network.MATIC)}:collections-v2:${contractAddress}:${tokenId}` | ||
} | ||
|
||
export function toLegacyURN(urn: string) { | ||
export function toLegacyURN(urn: URN): URN { | ||
return urn.replace('urn:decentraland:off-chain:base-avatars:', 'dcl://base-avatars/') | ||
} | ||
|
||
export function join(...args: string[]) { | ||
return args.join(DELIMITER) | ||
export function decodeURN(urn: URN): DecodedURN { | ||
const matches = urnRegExp.exec(urn) | ||
|
||
if (!matches || !matches.groups) { | ||
throw new Error(`Invalid URN: "${urn}"`) | ||
} | ||
|
||
return matches.groups as DecodedURN | ||
} | ||
|
||
export function pop(urn: string) { | ||
return urn.split(DELIMITER).pop() | ||
function getNetworkURNProtocol(network: Network) { | ||
return getURNProtocol(getChainIdByNetwork(network)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
a65ed26
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: