-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathaddress.ts
More file actions
77 lines (68 loc) · 2.53 KB
/
Copy pathaddress.ts
File metadata and controls
77 lines (68 loc) · 2.53 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import {
convert_bigint_to_Uint8Array,
encodeCoinPublicKey,
} from '@midnight-ntwrk/compact-runtime';
import { encodeContractAddress } from '@midnight-ntwrk/ledger';
import type * as Compact from '../../artifacts/MockFungibleToken/contract/index.cjs';
const PREFIX_ADDRESS = '0200';
/**
* @description Converts an ASCII string to its hexadecimal representation,
* left-padded with zeros to a specified length. Useful for generating
* fixed-size hex strings for encoding.
* @param str ASCII string to convert.
* @param len Total desired length of the resulting hex string. Defaults to 64.
* @returns Hexadecimal string representation of `str`, padded to `length` characters.
*/
export const toHexPadded = (str: string, len = 64) =>
Buffer.from(str, 'ascii').toString('hex').padStart(len, '0');
/**
* @description Generates ZswapCoinPublicKey from `str` for testing purposes.
* @param str String to hexify and encode.
* @returns Encoded `ZswapCoinPublicKey`.
*/
export const encodeToPK = (str: string): Compact.ZswapCoinPublicKey => ({
bytes: encodeCoinPublicKey(toHexPadded(str)),
});
/**
* @description Generates ContractAddress from `str` for testing purposes.
* Prepends 32-byte hex with PREFIX_ADDRESS before encoding.
* @param str String to hexify and encode.
* @returns Encoded `ZswapCoinPublicKey`.
*/
export const encodeToAddress = (str: string): Compact.ContractAddress => ({
bytes: encodeContractAddress(PREFIX_ADDRESS + toHexPadded(str)),
});
/**
* @description Generates an Either object for ZswapCoinPublicKey for testing.
* For use when an Either argument is expected.
* @param str String to hexify and encode.
* @returns Defined Either object for ZswapCoinPublicKey.
*/
export const createEitherTestUser = (str: string) => ({
is_left: true,
left: encodeToPK(str),
right: encodeToAddress(''),
});
/**
* @description Generates an Either object for ContractAddress for testing.
* For use when an Either argument is expected.
* @param str String to hexify and encode.
* @returns Defined Either object for ContractAddress.
*/
export const createEitherTestContractAddress = (str: string) => ({
is_left: false,
left: encodeToPK(''),
right: encodeToAddress(str),
});
export const zeroUint8Array = (length = 32) =>
convert_bigint_to_Uint8Array(length, 0n);
export const ZERO_KEY = {
is_left: true,
left: { bytes: zeroUint8Array() },
right: encodeToAddress(''),
};
export const ZERO_ADDRESS = {
is_left: false,
left: encodeToPK(''),
right: { bytes: zeroUint8Array() },
};