Module Overview#

See page for:

  • greenweb.util.address: Utilities for encoding/decoding addresses or other bech32m-encoded data (such as offers).
  • greenweb.util.coin: Coin-related utilities (getId, getName).
  • greenweb.util.serializer: Export of the internal Serializer object, which (de)serializes objects for the official wallet protocol.
  • greenweb.util.network: Network-related utilities (mainnet and testnets - ids, genesis challenges, and address prefixes).
  • greenweb.util.sexp: CLVM-related utilities - run programs, convert hex to SExp and SExp to hex. This also includes ports of some puzzle drivers, such as standardCoinPuzzle(), a wrapper for P2_DELEGATED_PUZZLE_OR_HIDDEN_PUZZLE_PROGRAM, which is also exported by this class.
  • greenweb.util.goby: Functions used for converting goby-returned data to GreenWeb.js objects.
  • greenweb.util.key: Exports methods used to parse public and private keys, as well as functions for handling mnemonics and key derivation.

Functions#

Note: All integer inputs are BigNumberish.

formatChia#

Converts the given amount of mojos to Chia. Returns a string that can be displayed to end-users.

greenweb.util.formatChia(1000000000000);
// "1.0"

greenweb.util.formatChia(1230000000000);
// "1.23"

greenweb.util.formatChia(1);
// "0.000000000001"

parseChia#

Takes an XCH amount as input (string) and returns uint representing the number of mojos in that amount.

greenweb.util.parseChia("1");
// 1000000000000

greenweb.util.parseChia("1.23");
// 1230000000000

greenweb.util.parseChia("0.000000000001");
// 1

formatToken#

Converts the given amount of units to tokens. Returns a string that can be displayed to end-users. Second argument is optional and represents the default amount of units per token (default is 1000).

greenweb.util.formatToken(1);
// "0.001"

greenweb.util.formatToken(100);
// "0.1"

greenweb.util.formatToken(12345, 100);
// "123.45"

parseToken#

Takes a token amount as input (string) and returns uint representing the number of units in that amount.

greenweb.util.parseToken("0.001");
// 1

greenweb.util.parseToken("0.1");
// 100

greenweb.util.parseToken("123.45", 100);
// 12345

greenweb.util.parseToken("12", 100);
// 1200

stdHash#

Takes a bytes / hex string as input and returns the value of Chia's std_hash function as a hex-encoded stirng.

greenweb.util.stdHash('31333337')
// "5db1fee4b5703808c48078a76768b155b421b210c0761cd6a5d223f4d99f1eaa" 

hexlify#

Takes a hex string and makes sure it starts with '0x'.

public static hexlify(value: string): string {
    return value.startsWith("0x") ? value : `0x${value}`;
}




dehexlify#

Takes a hex string and makes sure it does NOT start with '0x'.

public static dehexlify(value: string | null): string | null {
    if(value === null) return null;
    return value.startsWith("0x") ? value.slice(2) : value;
}

unhexlify#

Wrapper for dehexlify. Made available for those coming from other libraries / programming languages.

public static unhexlify = this.dehexlify;