-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5543cfa
commit 7d84a8f
Showing
1 changed file
with
83 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { PublicKey } from '@solana/web3.js'; | ||
import { rustEnum, bool, u8, publicKey, str, vec, option, struct } from '@project-serum/borsh' | ||
import { u16 } from 'buffer-layout'; | ||
|
||
const KEY_LAYOUT = rustEnum([ | ||
struct([], 'uninitialized'), | ||
struct([], 'editionV1'), | ||
struct([], 'masterEditionV1'), | ||
struct([], 'reservationListV1'), | ||
struct([], 'metadataV1'), | ||
struct([], 'reservationListV2'), | ||
struct([], 'masterEditionV2'), | ||
struct([], 'editionMarker'), | ||
]); | ||
|
||
const CREATOR_LAYOUT = struct([ | ||
publicKey('address'), | ||
bool('verified'), | ||
u8('share'), | ||
]) | ||
|
||
const DATA_LAYOUT = struct([ | ||
str('name'), | ||
str('symbol'), | ||
str('uri'), | ||
u16('sellerFeeBasisPoints'), | ||
option(vec(CREATOR_LAYOUT.replicate('creators')), 'creators') | ||
]) | ||
|
||
const METADATA_LAYOUT = struct([ | ||
KEY_LAYOUT.replicate('key'), | ||
publicKey('updateAuthority'), | ||
publicKey('mint'), | ||
DATA_LAYOUT.replicate('data'), | ||
bool('primarySaleHappened'), | ||
bool('isMutable'), | ||
option(u8(), 'editionNonce'), | ||
]); | ||
|
||
export interface Metadata { | ||
key: Key; | ||
updateAuthority: PublicKey; | ||
mint: PublicKey; | ||
data: Data; | ||
primarySaleHappened: boolean; | ||
isMutable: boolean; | ||
editionNonce: number; | ||
} | ||
|
||
export interface Data { | ||
name: string; | ||
symbol: string; | ||
uri: string; | ||
sellerFeeBasisPoints: number; | ||
creators: Array<Creator> | null; | ||
} | ||
|
||
export interface Creator { | ||
address: PublicKey; | ||
verified: boolean; | ||
share: number; | ||
} | ||
|
||
export type Key = | ||
{ unitialized: {} } | ||
| { editionV1: {} } | ||
| { masterEditionV1: {} } | ||
| { reserverationListV1: {} } | ||
| { metadataV1: {} } | ||
| { reservationListV2: {} } | ||
| { masterEditionV2: {} } | ||
| { editoinMarket: {} }; | ||
|
||
// eslint-disable-next-line no-control-regex | ||
const METADATA_REPLACE = new RegExp('\u0000', 'g'); | ||
|
||
export function decodeMetadata(buffer: Buffer): Metadata { | ||
const metadata: any = METADATA_LAYOUT.decode(buffer); | ||
metadata.data.name = metadata.data.name.replace(METADATA_REPLACE, ''); | ||
metadata.data.uri = metadata.data.uri.replace(METADATA_REPLACE, ''); | ||
metadata.data.symbol = metadata.data.symbol.replace(METADATA_REPLACE, ''); | ||
return metadata; | ||
}; |