-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoding.ts
36 lines (29 loc) · 1.06 KB
/
decoding.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { Address } from '@unique-nft/utils/address';
import { RoyaltyType, UniqueRoyaltyPart } from './types';
import { splitStringEvery } from './shared';
export const decodeRoyaltyPart = (encoded: string): UniqueRoyaltyPart => {
const encodedMeta = encoded.slice(2, 66);
const encodedAddress = encoded.slice(2 + 64);
const version = parseInt(encodedMeta.slice(0, 2), 16);
const decimals = parseInt(encodedMeta.slice(46, 46 + 2), 16);
const value = BigInt('0x' + encodedMeta.slice(48));
const royaltyType =
encodedMeta[44] === '0' ? RoyaltyType.DEFAULT : RoyaltyType.PRIMARY_ONLY;
const isEthereum = encodedMeta[45] === '0';
const address = isEthereum
? '0x' + encodedAddress.slice(24)
: Address.substrate.encode(encodedAddress);
return {
version,
decimals,
value,
royaltyType,
address,
};
};
export const decodeRoyalty = (encoded: string): UniqueRoyaltyPart[] => {
const parts = splitStringEvery(encoded.substring(2), 128).map(
(encoded) => '0x' + encoded,
);
return parts.map((part) => decodeRoyaltyPart(part));
};