-
Notifications
You must be signed in to change notification settings - Fork 54
/
Asset.ts
59 lines (52 loc) · 1.83 KB
/
Asset.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 * as cip14 from '@emurgo/cip14-js';
import { Hash28ByteBase16, OpaqueString, assertIsHexString, typedBech32 } from '../util/primitives';
import { InvalidStringError } from '../../errors';
export type AssetId = OpaqueString<'AssetId'>;
/**
* Hex-encoded asset name
*/
export type AssetName = OpaqueString<'AssetName'>;
export const AssetName = (value: string): AssetName => {
if (value.length > 0) {
assertIsHexString(value);
if (value.length > 64) {
throw new InvalidStringError('too long');
}
}
return value.toLowerCase() as unknown as AssetName;
};
/**
* @param {string} value concatenated PolicyId and AssetName
* @throws InvalidStringError
*/
export const AssetId = (value: string): AssetId => {
assertIsHexString(value);
if (value.length > 120) throw new InvalidStringError('too long');
if (value.length < 56) throw new InvalidStringError('too short');
return value as unknown as AssetId;
};
/**
* Hex-encoded policy id
*/
export type PolicyId = OpaqueString<'PolicyId'>;
export const PolicyId = (value: string): PolicyId => Hash28ByteBase16(value) as unknown as PolicyId;
/**
* Fingerprint of a native asset for human comparison
* See CIP-0014
*/
export type AssetFingerprint = OpaqueString<'AssetFingerprint'>;
export const AssetFingerprint = (value: string): AssetFingerprint => typedBech32(value, 'asset', 32);
/**
* Gets the native asset fingerprint from its policy id and asset name.
* See CIP-0014
*
* @param policyId The native asset policy id.
* @param assetName The native asset name.
*/
AssetFingerprint.fromParts = (policyId: PolicyId, assetName: AssetName): AssetFingerprint => {
const cip14Fingerprint = cip14.default.fromParts(
Buffer.from(policyId.toString(), 'hex'),
Buffer.from(assetName.toString(), 'hex')
);
return AssetFingerprint(cip14Fingerprint.fingerprint());
};