Skip to content

Commit 80e605d

Browse files
committed
feat(core): stringify util
1 parent 4b04168 commit 80e605d

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ckb-ccc/core": minor
3+
---
4+
5+
feat(core): stringify util

packages/core/src/ckb/transaction.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,14 @@ export class CellInput extends mol.Entity.Base<CellInputLike, CellInput>() {
548548
this.cellOutput = cell.cellOutput;
549549
this.outputData = cell.outputData;
550550
}
551+
552+
clone(): CellInput {
553+
const cloned = super.clone();
554+
cloned.cellOutput = this.cellOutput;
555+
cloned.outputData = this.outputData;
556+
557+
return cloned;
558+
}
551559
}
552560
export const CellInputVec = mol.vector(CellInput);
553561

@@ -900,6 +908,11 @@ export class Transaction extends mol.Entity.Base<
900908
});
901909
}
902910

911+
/**
912+
* @deprecated
913+
* Use ccc.stringify instead.
914+
* stringify the tx to JSON string.
915+
*/
903916
stringify(): string {
904917
return JSON.stringify(this, (_, value) => {
905918
if (typeof value === "bigint") {

packages/core/src/utils/index.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NumLike, numFrom } from "../num/index.js";
1+
import { NumLike, numFrom, numToHex } from "../num/index.js";
22

33
/**
44
* A type safe way to apply a transformer on a value if it's not empty.
@@ -183,3 +183,16 @@ export type Constructor<T> = {
183183
export function isWebview(userAgent: string): boolean {
184184
return /webview|wv|ip((?!.*Safari)|(?=.*like Safari))/i.test(userAgent);
185185
}
186+
187+
/**
188+
* @public
189+
*/
190+
export function stringify(val: unknown) {
191+
return JSON.stringify(val, (_, value) => {
192+
if (typeof value === "bigint") {
193+
return numToHex(value);
194+
}
195+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
196+
return value;
197+
});
198+
}

0 commit comments

Comments
 (0)