Skip to content

Commit 42bf1b5

Browse files
committed
refactor(js): add a helper function to convert a string to hexadecimal format
1 parent 9382ee2 commit 42bf1b5

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

js/helper.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ export function calculateTxFee(
150150
return fee * ratio < base ? fee + 1n: fee;
151151
}
152152

153+
export function encodeStringToHex(str: string): HexString {
154+
return "0x" + Buffer.from(str).toString("hex");
155+
}
156+
153157
/**
154158
* Get faucet from https://github.com/Flouse/nervos-functions#faucet
155159
*/

js/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Hash, Cell, RPC, commons, helpers as lumosHelpers, HexString, hd } from "@ckb-lumos/lumos";
22
import { minimalScriptCapacity } from "@ckb-lumos/helpers"
33
import {
4-
CKB_TESTNET_EXPLORER, TESTNET_SCRIPTS,
4+
CKB_TESTNET_EXPLORER, TESTNET_SCRIPTS, encodeStringToHex,
55
generateAccountFromPrivateKey, ckbIndexer, collectInputCells, calculateTxFee, addWitness
66
} from "./helper";
77
import { CHARLIE } from "./test-keys";
@@ -22,7 +22,7 @@ console.assert(testAccount.address === CHARLIE.ADDRESS);
2222
const constructHelloWorldTx = async (
2323
onChainMemo: string
2424
): Promise<lumosHelpers.TransactionSkeletonType> => {
25-
const onChainMemoHex: string = "0x" + Buffer.from(onChainMemo).toString("hex");
25+
const onChainMemoHex: HexString = encodeStringToHex(onChainMemo);
2626
console.log(`onChainMemoHex: ${onChainMemoHex}`);
2727

2828
// CapacityUnit.Byte = 100000000, because 1 CKB = 100000000 shannon

0 commit comments

Comments
 (0)