2022-02-14 13:16:29 +01:00
|
|
|
import { SvgWaves } from './SvgWaves'
|
2021-11-16 22:06:02 +01:00
|
|
|
|
2022-01-11 15:52:01 +01:00
|
|
|
// 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
|
2022-01-11 15:52:01 +01:00
|
|
|
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"'
|
|
|
|
)
|
2022-02-14 13:16:29 +01:00
|
|
|
.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 {
|
2022-02-14 13:16:29 +01:00
|
|
|
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 = {
|
2022-02-14 13:16:29 +01:00
|
|
|
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.`,
|
2022-01-11 15:52:01 +01:00
|
|
|
// TODO: ideally this includes the final DID
|
|
|
|
external_url: 'https://market.oceanprotocol.com',
|
|
|
|
background_color: '141414', // dark background
|
2022-02-14 13:16:29 +01:00
|
|
|
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 {
|
2022-02-14 13:16:29 +01:00
|
|
|
// 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,
|
2022-02-14 13:16:29 +01:00
|
|
|
tokenURI: `data:application/json;base64,${encodedMetadata}`
|
2021-11-15 20:06:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nftCreateData
|
|
|
|
}
|