-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoding.ts
59 lines (51 loc) · 1.78 KB
/
encoding.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { Address } from '@unique-nft/utils/address';
import {
RoyaltyType,
UniqueRoyaltyPart,
UniqueRoyaltyPartNoBigint,
} from './types';
import { validateRoyaltyPart, ZEROS } from './shared';
const encodeAddress = (address: string): [boolean, string] => {
if (Address.is.ethereumAddress(address)) {
return [
true,
Address.normalize
.ethereumAddress(address)
.substring(2)
.padStart(64, '0')
.toLowerCase(),
];
}
return [false, Address.substrate.decode(address).hex.substring(2)];
};
/**
* encodes a UniqueRoyaltyPart into a hex string
* @param part UniqueRoyaltyPart
* @returns hex string where first 64 characters are metadata in format:
* VV000000000000000000000000000000000000000000RADDvvvvvvvvvvvvvvvv
* where:
* VV - version
* 42 zeros
* R - royalty type (0 - default, 1 - primary-only)
* A - address type (0 - ethereum, 1 - substrate)
* DD - decimals
* vvvvvvvvvvvvvvvvvv - value (uint64)
*
* and the rest of the string is the address encoded as hex
*/
export const encodeRoyaltyPart = (
part: UniqueRoyaltyPart | UniqueRoyaltyPartNoBigint,
): string => {
validateRoyaltyPart(part);
const version = part.version.toString(16).padStart(2, '0');
const royaltyType = part.royaltyType === RoyaltyType.DEFAULT ? '0' : '1';
const decimals = part.decimals.toString(16).padStart(2, '0');
const value = part.value.toString(16).padStart(16, '0');
const [isEthereum, address] = encodeAddress(part.address);
const addressType = isEthereum ? '0' : '1';
return `0x${version}${ZEROS}${royaltyType}${addressType}${decimals}${value}${address}`;
};
export const encodeRoyalty = (
parts: (UniqueRoyaltyPart | UniqueRoyaltyPartNoBigint)[],
): string =>
'0x' + parts.map((part) => encodeRoyaltyPart(part).substring(2)).join('');