Skip to content

Commit

Permalink
feat(helpers): parse address via known config key (ckb-js#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
homura authored Sep 20, 2022
1 parent 09bd023 commit 525023d
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 2 deletions.
79 changes: 78 additions & 1 deletion packages/helpers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from "@ckb-lumos/base";
import { bech32, bech32m } from "bech32";
import { List, Map as ImmutableMap, Record } from "immutable";
import { Config, getConfig } from "@ckb-lumos/config-manager";
import { Config, getConfig, predefined } from "@ckb-lumos/config-manager";
import { BI } from "@ckb-lumos/bi";
import {
parseDeprecatedCkb2019Address,
Expand Down Expand Up @@ -154,6 +154,13 @@ export function generateAddress(
* @deprecated please migrate to {@link encodeToAddress}, the short format address will be removed in the future */
export const scriptToAddress = generateAddress;

/**
* @deprecated please migrate to {@link encodeToConfigAddress}
* @param args
* @param scriptType
* @param param2
* @returns
*/
function generatePredefinedAddress(
args: HexString,
scriptType: string,
Expand All @@ -176,13 +183,24 @@ function generatePredefinedAddress(
return generateAddress(script, { config });
}

/**
* @deprecated please migrate to {@link encodeToConfigAddress}
* @param args
* @param param1
* @returns
*/
export function generateSecp256k1Blake160Address(
args: HexString,
{ config = undefined }: Options = {}
): Address {
return generatePredefinedAddress(args, "SECP256K1_BLAKE160", { config });
}

/**
* @deprecated please migrate to {@link encodeToConfigAddress}
* @param args
* @param config
*/
export function generateSecp256k1Blake160MultisigAddress(
args: HexString,
{ config = undefined }: Options = {}
Expand All @@ -207,6 +225,11 @@ export function parseAddress(

export const addressToScript = parseAddress;

/**
* parse a lock script to an address
* @param script
* @param config
*/
export function encodeToAddress(
script: Script,
{ config = undefined }: Options = {}
Expand All @@ -233,6 +256,60 @@ export function encodeToAddress(
return bech32m.encode(config.PREFIX, bech32m.toWords(data), BECH32_LIMIT);
}

type PredefinedScriptName = keyof typeof predefined.LINA.SCRIPTS;

export function encodeToConfigAddress(
args: HexString,
scriptType: PredefinedScriptName
): string;
export function encodeToConfigAddress<C extends Config>(
args: HexString,
scriptType: keyof C["SCRIPTS"],
options: { config?: C }
): string;
/**
* encode a script to an address with args and a key of config
* @example
* ```ts
* // parse a predefined lock to an address
* encodeToConfigAddress('0x12345678123456781234567812345678', 'SECP256K1_BLAKE160');
* // parse a custom lock to an address
* encodeToConfigAddress('0x12345678123456781234567812345678', 'MY_CUSTOM_LOCK', {
* SCRIPTS: {
* MY_CUSTOM_LOCK: {...}
* }
* })
* ```
* @param args script args
* @param scriptType a key of `config.SCRIPTS`
* @param options
* @returns
*/
export function encodeToConfigAddress<C extends Config>(
args: HexString,
scriptType: keyof C["SCRIPTS"],
options?: { config?: C }
): string {
const config = (options?.config || getConfig()) as C;
const template = config.SCRIPTS[scriptType as string];

if (!template) {
const availableKeys = Object.keys(config.SCRIPTS);
throw new Error(
// prettier-ignore
`Invalid script type: ${String(scriptType)}, only support: ${availableKeys}`
);
}

const script: Script = {
codeHash: template.CODE_HASH,
hashType: template.HASH_TYPE,
args,
};

return encodeToAddress(script, { config });
}

export interface TransactionSkeletonInterface {
cellProvider: CellProvider | null;
cellDeps: List<CellDep>;
Expand Down
23 changes: 22 additions & 1 deletion packages/helpers/tests/encode_address.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from "ava";
import { encodeToAddress } from "../src";
import { encodeToAddress, encodeToConfigAddress } from "../src";
import { predefined } from "@ckb-lumos/config-manager";
import { Script } from "@ckb-lumos/base";

Expand Down Expand Up @@ -38,3 +38,24 @@ test("encode to full address, data1", (t) => {
"ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq4nnw7qkdnnclfkg59uzn8umtfd2kwxceqkkxdwn"
);
});

test("encode to address via config key", (t) => {
const testnetAddress = encodeToConfigAddress(
"0x159890a7cacb44a95bef0743064433d763de229c",
"SECP256K1_BLAKE160",
{ config: AGGRON }
);

t.is(
testnetAddress,
"ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqg4nzg20jktgj54hmc8gvrygv7hv00z98qklce9w"
);

t.throws(() => {
encodeToConfigAddress(
"0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64",
"UNKNOWN_LOCK" as any,
{ config: AGGRON }
);
});
});

0 comments on commit 525023d

Please sign in to comment.