1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00
market/src/@utils/nft.ts

74 lines
2.0 KiB
TypeScript
Raw Normal View History

import { SvgWaves } from './SvgWaves'
// https://docs.opensea.io/docs/metadata-standards
2022-01-11 16:40:40 +01:00
export interface NftMetadata {
2021-11-15 16:02:13 +01:00
name: string
symbol: string
2021-11-15 20:06:33 +01:00
description: string
image?: string
/* eslint-disable camelcase */
external_url?: string
image_data?: string
background_color?: string
/* eslint-enable camelcase */
2021-11-15 20:06:33 +01:00
}
function encodeSvg(svgString: string): string {
return svgString
.replace(
'<svg',
~svgString.indexOf('xmlns')
? '<svg'
: '<svg xmlns="http://www.w3.org/2000/svg"'
)
.replace('></path>', '/>')
2021-11-15 20:06:33 +01:00
.replace(/"/g, "'")
.replace(/%/g, '%25')
.replace(/#/g, '%23')
.replace(/{/g, '%7B')
.replace(/}/g, '%7D')
.replace(/</g, '%3C')
.replace(/>/g, '%3E')
.replace(/\s+/g, ' ')
2021-11-15 16:02:13 +01:00
}
2022-01-11 16:40:40 +01:00
export function generateNftMetadata(): NftMetadata {
const waves = new SvgWaves()
const svg = waves.generateSvg()
// TODO: figure out if also image URI needs base64 encoding
// e.g. 'data:image/svg+xml;base64,'
// generated SVG embedded as 'data:image/svg+xml' and encoded characters
const imageData = `data:image/svg+xml,${encodeSvg(svg.outerHTML)}`
2021-11-15 21:47:59 +01:00
2022-01-11 16:40:40 +01:00
const newNft: NftMetadata = {
name: 'Ocean Asset NFT',
2022-01-13 13:07:55 +01:00
symbol: 'OCEAN-NFT',
2021-11-15 20:06:33 +01:00
description: `This NFT represents an asset in the Ocean Protocol v4 ecosystem.`,
// TODO: ideally this includes the final DID
external_url: 'https://market.oceanprotocol.com',
background_color: '141414', // dark background
image_data: imageData
2021-11-15 16:02:13 +01:00
}
return newNft
}
2021-11-15 20:06:33 +01:00
2022-01-11 16:40:40 +01:00
export function generateNftCreateData(nftMetadata: NftMetadata): any {
// TODO: figure out if Buffer.from method is working in browser in final build
// as BTOA is deprecated.
// tokenURI: window?.btoa(JSON.stringify(nftMetadata))
const encodedMetadata = Buffer.from(JSON.stringify(nftMetadata)).toString(
'base64'
)
2021-11-15 20:06:33 +01:00
const nftCreateData = {
2022-01-11 16:40:40 +01:00
name: nftMetadata.name,
symbol: nftMetadata.symbol,
2021-11-15 20:06:33 +01:00
templateIndex: 1,
tokenURI: `data:application/json;base64,${encodedMetadata}`
2021-11-15 20:06:33 +01:00
}
return nftCreateData
}