@ckb-ccc/core 0.0.2-alpha.1 → 0.0.2-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. package/.prettierignore +12 -0
  2. package/dist/address/address.advanced.d.ts +29 -3
  3. package/dist/address/address.advanced.d.ts.map +1 -1
  4. package/dist/address/address.advanced.js +69 -22
  5. package/dist/address/advanced.d.ts +1 -28
  6. package/dist/address/advanced.d.ts.map +1 -1
  7. package/dist/address/advanced.js +3 -61
  8. package/dist/address/index.d.ts +10 -13
  9. package/dist/address/index.d.ts.map +1 -1
  10. package/dist/address/index.js +18 -18
  11. package/dist/advancedBarrel.d.ts +1 -0
  12. package/dist/advancedBarrel.d.ts.map +1 -1
  13. package/dist/advancedBarrel.js +1 -0
  14. package/dist/barrel.d.ts +4 -2
  15. package/dist/barrel.d.ts.map +1 -1
  16. package/dist/barrel.js +4 -2
  17. package/dist/bytes/index.d.ts +7 -0
  18. package/dist/bytes/index.d.ts.map +1 -0
  19. package/dist/bytes/index.js +41 -0
  20. package/dist/ckb/advanced.d.ts +3 -2
  21. package/dist/ckb/advanced.d.ts.map +1 -1
  22. package/dist/ckb/advanced.js +16 -2
  23. package/dist/ckb/index.d.ts +2 -3
  24. package/dist/ckb/index.d.ts.map +1 -1
  25. package/dist/ckb/index.js +2 -3
  26. package/dist/ckb/molecule.advanced/generated.d.ts.map +1 -0
  27. package/dist/ckb/{types/molecule.advanced → molecule.advanced}/generated.js +378 -97
  28. package/dist/ckb/molecule.advanced/index.d.ts +6 -0
  29. package/dist/ckb/molecule.advanced/index.d.ts.map +1 -0
  30. package/dist/ckb/{types/molecule.advanced → molecule.advanced}/index.js +8 -0
  31. package/dist/ckb/{types/script.advanced.d.ts → script.advanced.d.ts} +1 -0
  32. package/dist/ckb/script.advanced.d.ts.map +1 -0
  33. package/dist/ckb/{types/script.advanced.js → script.advanced.js} +2 -1
  34. package/dist/ckb/script.d.ts +29 -0
  35. package/dist/ckb/script.d.ts.map +1 -0
  36. package/dist/ckb/script.js +91 -0
  37. package/dist/ckb/{types/transaction.advanced.d.ts → transaction.advanced.d.ts} +1 -0
  38. package/dist/ckb/transaction.advanced.d.ts.map +1 -0
  39. package/dist/ckb/{types/transaction.advanced.js → transaction.advanced.js} +2 -1
  40. package/dist/ckb/transaction.d.ts +142 -0
  41. package/dist/ckb/transaction.d.ts.map +1 -0
  42. package/dist/ckb/transaction.js +262 -0
  43. package/dist/client/client.d.ts +4 -4
  44. package/dist/client/client.d.ts.map +1 -1
  45. package/dist/client/clientPublicMainnet.advanced.d.ts +2 -2
  46. package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  47. package/dist/client/clientPublicMainnet.d.ts +2 -2
  48. package/dist/client/clientPublicMainnet.d.ts.map +1 -1
  49. package/dist/client/clientPublicTestnet.advanced.d.ts +2 -2
  50. package/dist/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  51. package/dist/client/clientPublicTestnet.d.ts +2 -2
  52. package/dist/client/clientPublicTestnet.d.ts.map +1 -1
  53. package/dist/client/jsonRpc/advanced.d.ts +36 -36
  54. package/dist/client/jsonRpc/advanced.d.ts.map +1 -1
  55. package/dist/client/jsonRpc/advanced.js +11 -11
  56. package/dist/client/jsonRpc/index.d.ts.map +1 -1
  57. package/dist/client/jsonRpc/index.js +1 -1
  58. package/dist/fixedPoint/index.d.ts +7 -0
  59. package/dist/fixedPoint/index.d.ts.map +1 -0
  60. package/dist/{fixedPoint.js → fixedPoint/index.js} +9 -12
  61. package/dist/{ckb/hasher.advanced.d.ts → hasher/advanced.d.ts} +1 -1
  62. package/dist/hasher/advanced.d.ts.map +1 -0
  63. package/dist/{ckb/hasher.d.ts → hasher/index.d.ts} +3 -2
  64. package/dist/hasher/index.d.ts.map +1 -0
  65. package/dist/{ckb/hasher.js → hasher/index.js} +4 -4
  66. package/dist/hex/index.d.ts +5 -0
  67. package/dist/hex/index.d.ts.map +1 -0
  68. package/dist/hex/index.js +9 -0
  69. package/dist/num/index.d.ts +13 -0
  70. package/dist/num/index.d.ts.map +1 -0
  71. package/dist/num/index.js +47 -0
  72. package/dist/signer/helpers.d.ts +3 -3
  73. package/dist/signer/helpers.d.ts.map +1 -1
  74. package/dist/signer/helpers.js +7 -4
  75. package/dist/signer/index.d.ts +2 -0
  76. package/dist/signer/index.d.ts.map +1 -1
  77. package/dist/signer/index.js +2 -0
  78. package/dist/signer/signer.d.ts +12 -3
  79. package/dist/signer/signer.d.ts.map +1 -1
  80. package/dist/signer/signer.js +13 -2
  81. package/dist/signer/signerReadonly.d.ts +8 -0
  82. package/dist/signer/signerReadonly.d.ts.map +1 -0
  83. package/dist/signer/signerReadonly.js +13 -0
  84. package/dist/{ckb/viewerCkbScript.d.ts → signer/signerReadonlyCkbScript.d.ts} +6 -5
  85. package/dist/signer/signerReadonlyCkbScript.d.ts.map +1 -0
  86. package/dist/{ckb/viewerCkbScript.js → signer/signerReadonlyCkbScript.js} +7 -4
  87. package/dist/utils/index.d.ts +7 -0
  88. package/dist/utils/index.d.ts.map +1 -0
  89. package/dist/utils/index.js +10 -0
  90. package/package.json +5 -3
  91. package/src/address/address.advanced.ts +76 -19
  92. package/src/address/advanced.ts +1 -59
  93. package/src/address/index.ts +46 -28
  94. package/src/advancedBarrel.ts +1 -0
  95. package/src/barrel.ts +4 -2
  96. package/src/bytes/index.ts +46 -0
  97. package/src/ckb/advanced.ts +3 -2
  98. package/src/ckb/index.ts +2 -3
  99. package/src/ckb/{types/molecule.advanced → molecule.advanced}/generated.d.ts +18 -7
  100. package/src/ckb/{types/molecule.advanced → molecule.advanced}/generated.js +551 -123
  101. package/src/ckb/molecule.advanced/index.ts +11 -0
  102. package/src/ckb/{types/script.advanced.ts → script.advanced.ts} +1 -0
  103. package/src/ckb/script.ts +94 -0
  104. package/src/ckb/{types/transaction.advanced.ts → transaction.advanced.ts} +1 -0
  105. package/src/ckb/transaction.ts +357 -0
  106. package/src/client/client.ts +4 -4
  107. package/src/client/clientPublicMainnet.advanced.ts +2 -2
  108. package/src/client/clientPublicMainnet.ts +2 -2
  109. package/src/client/clientPublicTestnet.advanced.ts +2 -2
  110. package/src/client/clientPublicTestnet.ts +2 -2
  111. package/src/client/clientTypes.ts +1 -1
  112. package/src/client/jsonRpc/advanced.ts +25 -25
  113. package/src/client/jsonRpc/index.ts +1 -3
  114. package/src/fixedPoint/index.ts +30 -0
  115. package/src/{ckb/hasher.ts → hasher/index.ts} +5 -5
  116. package/src/hex/index.ts +9 -0
  117. package/src/index.ts +1 -1
  118. package/src/num/index.ts +53 -0
  119. package/src/signer/helpers.ts +9 -6
  120. package/src/signer/index.ts +3 -1
  121. package/src/signer/signer.ts +23 -3
  122. package/src/signer/signerReadonly.ts +13 -0
  123. package/src/{ckb/viewerCkbScript.ts → signer/signerReadonlyCkbScript.ts} +8 -4
  124. package/src/utils/index.ts +28 -0
  125. package/dist/bytes.d.ts +0 -15
  126. package/dist/bytes.d.ts.map +0 -1
  127. package/dist/bytes.js +0 -114
  128. package/dist/ckb/hasher.advanced.d.ts.map +0 -1
  129. package/dist/ckb/hasher.d.ts.map +0 -1
  130. package/dist/ckb/types/advanced.d.ts +0 -4
  131. package/dist/ckb/types/advanced.d.ts.map +0 -1
  132. package/dist/ckb/types/advanced.js +0 -32
  133. package/dist/ckb/types/index.d.ts +0 -3
  134. package/dist/ckb/types/index.d.ts.map +0 -1
  135. package/dist/ckb/types/index.js +0 -18
  136. package/dist/ckb/types/molecule.advanced/generated.d.ts.map +0 -1
  137. package/dist/ckb/types/molecule.advanced/index.d.ts +0 -2
  138. package/dist/ckb/types/molecule.advanced/index.d.ts.map +0 -1
  139. package/dist/ckb/types/script.advanced.d.ts.map +0 -1
  140. package/dist/ckb/types/script.d.ts +0 -26
  141. package/dist/ckb/types/script.d.ts.map +0 -1
  142. package/dist/ckb/types/script.js +0 -75
  143. package/dist/ckb/types/transaction.advanced.d.ts.map +0 -1
  144. package/dist/ckb/types/transaction.d.ts +0 -127
  145. package/dist/ckb/types/transaction.d.ts.map +0 -1
  146. package/dist/ckb/types/transaction.js +0 -266
  147. package/dist/ckb/viewerCkbScript.d.ts.map +0 -1
  148. package/dist/fixedPoint.d.ts +0 -9
  149. package/dist/fixedPoint.d.ts.map +0 -1
  150. package/dist/primitive.d.ts +0 -5
  151. package/dist/primitive.d.ts.map +0 -1
  152. package/dist/primitive.js +0 -2
  153. package/dist/tsconfig.tsbuildinfo +0 -1
  154. package/dist/viewer.d.ts +0 -12
  155. package/dist/viewer.d.ts.map +0 -1
  156. package/dist/viewer.js +0 -19
  157. package/src/bytes.ts +0 -127
  158. package/src/ckb/types/advanced.ts +0 -3
  159. package/src/ckb/types/index.ts +0 -2
  160. package/src/ckb/types/molecule.advanced/index.ts +0 -1
  161. package/src/ckb/types/script.ts +0 -70
  162. package/src/ckb/types/transaction.ts +0 -366
  163. package/src/fixedPoint.ts +0 -53
  164. package/src/primitive.ts +0 -8
  165. package/src/viewer.ts +0 -22
  166. /package/dist/ckb/{types/molecule.advanced → molecule.advanced}/generated.d.ts +0 -0
  167. /package/dist/{ckb/hasher.advanced.js → hasher/advanced.js} +0 -0
  168. /package/src/ckb/{types/molecule.advanced → molecule.advanced}/blockchain.mol +0 -0
  169. /package/src/{ckb/hasher.advanced.ts → hasher/advanced.ts} +0 -0
package/dist/viewer.d.ts DELETED
@@ -1,12 +0,0 @@
1
- import { Address } from "./address";
2
- import { Client } from "./client";
3
- export declare abstract class Viewer {
4
- readonly client: Client;
5
- constructor(client: Client);
6
- abstract getInternalAddress(): Promise<string>;
7
- abstract getAddressObjs(): Promise<Address[]>;
8
- getRecommendedAddressObj(_preference?: unknown): Promise<Address>;
9
- getRecommendedAddress(preference?: unknown): Promise<string>;
10
- getAddresses(): Promise<string[]>;
11
- }
12
- //# sourceMappingURL=viewer.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"viewer.d.ts","sourceRoot":"","sources":["../src/viewer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC,8BAAsB,MAAM;aACE,MAAM,EAAE,MAAM;gBAAd,MAAM,EAAE,MAAM;IAE1C,QAAQ,CAAC,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;IAE9C,QAAQ,CAAC,cAAc,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;IACvC,wBAAwB,CAAC,WAAW,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IAIjE,qBAAqB,CAAC,UAAU,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC;IAG5D,YAAY,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;CAKxC"}
package/dist/viewer.js DELETED
@@ -1,19 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Viewer = void 0;
4
- const address_1 = require("./address");
5
- class Viewer {
6
- constructor(client) {
7
- this.client = client;
8
- }
9
- async getRecommendedAddressObj(_preference) {
10
- return (await this.getAddressObjs())[0];
11
- }
12
- async getRecommendedAddress(preference) {
13
- return address_1.Address.toString(await this.getRecommendedAddressObj(preference));
14
- }
15
- async getAddresses() {
16
- return this.getAddressObjs().then((addresses) => addresses.map((address) => address_1.Address.toString(address)));
17
- }
18
- }
19
- exports.Viewer = Viewer;
package/src/bytes.ts DELETED
@@ -1,127 +0,0 @@
1
- import { BytesLike, Hex, HexLike, NumberLike } from "./primitive";
2
- import { Buffer } from "buffer/";
3
-
4
- export function concatBytes(...args: BytesLike[]): Uint8Array {
5
- return new Uint8Array(
6
- args.reduce((acc: number[], v) => {
7
- acc.push(...toBytes(v));
8
- return acc;
9
- }, []),
10
- );
11
- }
12
-
13
- export function encodeUtf8(val: string): Uint8Array {
14
- return Buffer.from(val, "utf-8");
15
- }
16
-
17
- export function decodeUtf8(val: BytesLike): string {
18
- return Buffer.from(toBytes(val)).toString("utf-8");
19
- }
20
-
21
- export function toBytes(bytes: BytesLike): Uint8Array {
22
- if (bytes instanceof Uint8Array) {
23
- return bytes;
24
- }
25
-
26
- if (bytes instanceof ArrayBuffer) {
27
- return new Uint8Array(bytes);
28
- }
29
-
30
- if (Array.isArray(bytes)) {
31
- if (bytes.some((v) => v < 0 || 0xff < v)) {
32
- throw new Error(`Invalid bytes ${JSON.stringify(bytes)}`);
33
- }
34
- return new Uint8Array(bytes);
35
- }
36
-
37
- const str = bytes.length % 2 === 0 ? bytes.slice(2) : `0${bytes.slice(2)}`;
38
- const data = Buffer.from(str, "hex");
39
- if (data.length * 2 !== str.length) {
40
- throw new Error(`Invalid bytes ${bytes}`);
41
- }
42
- return data;
43
- }
44
-
45
- export function toHexFromHex(bytes: HexLike): Hex {
46
- if (typeof bytes === "string" && !bytes.startsWith("0x")) {
47
- return `0x${bytes}`;
48
- }
49
-
50
- return toHex(bytes);
51
- }
52
-
53
- export function toHex(bytes: HexLike): Hex {
54
- if (typeof bytes === "number") {
55
- return `0x${bytes.toString(16)}`;
56
- }
57
-
58
- if (typeof bytes === "string" && !bytes.startsWith("0x")) {
59
- return `0x${BigInt(bytes).toString(16)}`;
60
- }
61
-
62
- return `0x${Buffer.from(toBytes(bytes as BytesLike)).toString("hex")}`;
63
- }
64
-
65
- export function toNumber(val: NumberLike): string {
66
- return BigInt(val).toString();
67
- }
68
-
69
- export function toBytesFromNumber(val: NumberLike, bytes?: number): Uint8Array {
70
- return toBytesFromLENumber(val, bytes);
71
- }
72
-
73
- export function toBytesFromLENumber(
74
- val: NumberLike,
75
- bytes?: number,
76
- ): Uint8Array {
77
- return toBytesFromBENumber(val, bytes).reverse();
78
- }
79
-
80
- export function toBytesFromBENumber(
81
- val: NumberLike,
82
- bytes?: number,
83
- ): Uint8Array {
84
- let valStr = val;
85
- if (typeof valStr === "number" || typeof valStr === "bigint") {
86
- valStr = valStr.toString(16);
87
- } else if (typeof valStr === "string") {
88
- if (valStr.startsWith("0x")) {
89
- valStr = valStr.slice(2);
90
- } else {
91
- valStr = BigInt(valStr).toString(16);
92
- }
93
- }
94
-
95
- if (valStr.length % 2 !== 0) {
96
- valStr = `0${valStr}`;
97
- }
98
-
99
- const res = toBytes(`0x${valStr}`);
100
- if (bytes == null) {
101
- return res;
102
- }
103
- if (res.length > bytes) {
104
- return res.slice(res.length - bytes);
105
- }
106
- return new Uint8Array([
107
- ...Array.from(Array(bytes - res.length), () => 0),
108
- ...res,
109
- ]);
110
- }
111
-
112
- export function toNumberFromBytes(val: BytesLike): string {
113
- return toLENumberFromBytes(val);
114
- }
115
-
116
- export function toLENumberFromBytes(val: BytesLike): string {
117
- return toBENumberFromBytes(toBytes(val).reverse());
118
- }
119
-
120
- export function toBENumberFromBytes(val: BytesLike): string {
121
- const str = toHex(val).replace(/0x0*/, "0x");
122
- if (str.length === 2) {
123
- return "0";
124
- }
125
-
126
- return BigInt(str).toString();
127
- }
@@ -1,3 +0,0 @@
1
- export * from "./script.advanced";
2
- export * from "./transaction.advanced";
3
- export * as moleculeCodecCkb from "./molecule.advanced";
@@ -1,2 +0,0 @@
1
- export * from "./script";
2
- export * from "./transaction";
@@ -1 +0,0 @@
1
- export * from "./generated";
@@ -1,70 +0,0 @@
1
- import { DeepReadonly } from "ts-essentials";
2
- import { toBytes, toHex } from "../../bytes";
3
- import { BytesLike, Hex } from "../../primitive";
4
- import { HASH_TYPE_TO_NUM, NUM_TO_HASH_TYPE } from "./script.advanced";
5
- import * as mol from "./molecule.advanced";
6
-
7
- export type HashType = "type" | "data" | "data1" | "data2";
8
-
9
- export function encodeHashType(hashType: HashType): Uint8Array {
10
- return toBytes([HASH_TYPE_TO_NUM[hashType]]);
11
- }
12
-
13
- export function decodeHashType(bytes: BytesLike): HashType {
14
- return NUM_TO_HASH_TYPE[toBytes(bytes)[0]];
15
- }
16
-
17
- export class Script {
18
- constructor(
19
- public codeHash: Hex,
20
- public hashType: HashType,
21
- public args: Hex,
22
- ) {}
23
-
24
- static from({
25
- codeHash,
26
- hashType,
27
- args,
28
- }: {
29
- codeHash: BytesLike;
30
- hashType: string;
31
- args: BytesLike;
32
- }): Script {
33
- return {
34
- codeHash: toHex(codeHash),
35
- hashType: hashType as HashType,
36
- args: toHex(args),
37
- };
38
- }
39
-
40
- static _toMolData(script: DeepReadonly<Script>) {
41
- return {
42
- codeHash: toBytes(script.codeHash),
43
- hashType: encodeHashType(script.hashType),
44
- args: toBytes(script.args),
45
- };
46
- }
47
-
48
- static encode(script: DeepReadonly<Script>): Uint8Array {
49
- return toBytes(mol.SerializeScript(Script._toMolData(script)));
50
- }
51
-
52
- static decode(bytes: BytesLike | mol.Script): Script {
53
- const view =
54
- bytes instanceof mol.Script ? bytes : new mol.Script(toBytes(bytes));
55
-
56
- return {
57
- codeHash: toHex(view.getCodeHash().raw()),
58
- hashType: decodeHashType([view.getHashType()]),
59
- args: toHex(view.getArgs().raw()),
60
- };
61
- }
62
-
63
- static eq(a: Script, b: Script): boolean {
64
- return (
65
- a.codeHash === b.codeHash &&
66
- a.args === b.args &&
67
- a.hashType === b.hashType
68
- );
69
- }
70
- }
@@ -1,366 +0,0 @@
1
- import { HashType, Script } from "./script";
2
- import {
3
- toBytes,
4
- toBytesFromNumber,
5
- toHex,
6
- toHexFromHex,
7
- toNumber,
8
- toNumberFromBytes,
9
- } from "../../bytes";
10
- import { BytesLike, Hex, NumberLike } from "../../primitive";
11
- import type { TransactionSkeletonType } from "@ckb-lumos/helpers";
12
- import { DeepReadonly } from "ts-essentials";
13
- import * as mol from "./molecule.advanced";
14
- import { DEP_TYPE_TO_NUM, NUM_TO_DEP_TYPE } from "./transaction.advanced";
15
- import { Hasher, ckbHash } from "../hasher";
16
-
17
- export type DepType = "depGroup" | "code";
18
-
19
- export function encodeDepType(depType: DepType): Uint8Array {
20
- return toBytes([DEP_TYPE_TO_NUM[depType]]);
21
- }
22
-
23
- export function decodeDepType(bytes: BytesLike): DepType {
24
- return NUM_TO_DEP_TYPE[toBytes(bytes)[0]];
25
- }
26
-
27
- export class OutPoint {
28
- constructor(
29
- public txHash: Hex,
30
- public index: string,
31
- ) {}
32
-
33
- static from({
34
- txHash,
35
- index,
36
- }: {
37
- txHash: BytesLike;
38
- index: NumberLike;
39
- }): OutPoint {
40
- return new OutPoint(toHex(txHash), toNumber(index));
41
- }
42
-
43
- static _toMolData(outPoint: DeepReadonly<OutPoint>) {
44
- return {
45
- txHash: toBytes(outPoint.txHash),
46
- index: toBytesFromNumber(outPoint.index, 4),
47
- };
48
- }
49
-
50
- static encode(outPoint: DeepReadonly<OutPoint>): Uint8Array {
51
- return toBytes(mol.SerializeOutPoint(OutPoint._toMolData(outPoint)));
52
- }
53
-
54
- static decode(bytes: BytesLike | mol.OutPoint): OutPoint {
55
- const view =
56
- bytes instanceof mol.OutPoint ? bytes : new mol.OutPoint(toBytes(bytes));
57
- return {
58
- txHash: toHex(view.getTxHash().raw()),
59
- index: toNumberFromBytes(view.getIndex().raw()),
60
- };
61
- }
62
- }
63
-
64
- export class CellOutput {
65
- constructor(
66
- public capacity: string,
67
- public lock: Script,
68
- public type?: Script,
69
- ) {}
70
-
71
- static from({
72
- capacity,
73
- lock,
74
- type,
75
- }: {
76
- capacity: NumberLike;
77
- lock: Parameters<typeof Script.from>[0];
78
- type?: Parameters<typeof Script.from>[0];
79
- }): CellOutput {
80
- return new CellOutput(
81
- toNumber(capacity),
82
- Script.from(lock),
83
- type ? Script.from(type) : undefined,
84
- );
85
- }
86
-
87
- static _toMolData(cellOutput: DeepReadonly<CellOutput>) {
88
- return {
89
- capacity: toBytesFromNumber(cellOutput.capacity, 8),
90
- lock: Script._toMolData(cellOutput.lock),
91
- type: cellOutput.type ? Script._toMolData(cellOutput.type) : undefined,
92
- };
93
- }
94
-
95
- static encode(cellOutput: DeepReadonly<CellOutput>): Uint8Array {
96
- return toBytes(mol.SerializeCellOutput(CellOutput._toMolData(cellOutput)));
97
- }
98
-
99
- static decode(bytes: BytesLike | mol.CellOutput): CellOutput {
100
- const view =
101
- bytes instanceof mol.CellOutput
102
- ? bytes
103
- : new mol.CellOutput(toBytes(bytes));
104
- const type = view.getType();
105
- return {
106
- capacity: toNumberFromBytes(view.getCapacity().raw()),
107
- lock: Script.decode(view.getLock()),
108
- type: type.hasValue() ? Script.decode(type.value()) : undefined,
109
- };
110
- }
111
- }
112
-
113
- export class CellInput {
114
- constructor(
115
- public previousOutput: OutPoint,
116
- public since: string,
117
- public cellOutput?: CellOutput,
118
- public outputData?: Hex,
119
- ) {}
120
-
121
- static from({
122
- previousOutput,
123
- since,
124
- cellOutput,
125
- outputData,
126
- }: {
127
- previousOutput: Parameters<typeof OutPoint.from>[0];
128
- since: NumberLike;
129
- cellOutput?: Parameters<typeof CellOutput.from>[0];
130
- outputData?: BytesLike;
131
- }): CellInput {
132
- return new CellInput(
133
- OutPoint.from(previousOutput),
134
- toNumber(since),
135
- cellOutput ? CellOutput.from(cellOutput) : undefined,
136
- outputData ? toHex(outputData) : undefined,
137
- );
138
- }
139
-
140
- static _toMolData(cellInput: DeepReadonly<CellInput>) {
141
- return {
142
- previousOutput: OutPoint._toMolData(cellInput.previousOutput),
143
- since: toBytesFromNumber(cellInput.since, 8),
144
- };
145
- }
146
-
147
- static encode(cellInput: DeepReadonly<CellInput>): Uint8Array {
148
- return toBytes(mol.SerializeCellInput(CellInput._toMolData(cellInput)));
149
- }
150
-
151
- static decode(bytes: BytesLike | mol.CellInput): CellInput {
152
- const view =
153
- bytes instanceof mol.CellInput
154
- ? bytes
155
- : new mol.CellInput(toBytes(bytes));
156
- return {
157
- previousOutput: OutPoint.decode(view.getPreviousOutput()),
158
- since: toNumberFromBytes(view.getSince().raw()),
159
- };
160
- }
161
- }
162
-
163
- export class CellDep {
164
- constructor(
165
- public outPoint: OutPoint,
166
- public depType: DepType,
167
- ) {}
168
-
169
- static from({
170
- outPoint,
171
- depType,
172
- }: {
173
- outPoint: Parameters<typeof OutPoint.from>[0];
174
- depType: DepType;
175
- }): CellDep {
176
- return new CellDep(OutPoint.from(outPoint), depType);
177
- }
178
-
179
- static _toMolData(cellDep: DeepReadonly<CellDep>) {
180
- return {
181
- outPoint: OutPoint._toMolData(cellDep.outPoint),
182
- depType: encodeDepType(cellDep.depType),
183
- };
184
- }
185
-
186
- static encode(cellDep: DeepReadonly<CellDep>): Uint8Array {
187
- return toBytes(mol.SerializeCellDep(CellDep._toMolData(cellDep)));
188
- }
189
-
190
- static decode(bytes: BytesLike | mol.CellDep): CellDep {
191
- const view =
192
- bytes instanceof mol.CellDep ? bytes : new mol.CellDep(toBytes(bytes));
193
- return {
194
- outPoint: OutPoint.decode(view.getOutPoint()),
195
- depType: decodeDepType([view.getDepType()]),
196
- };
197
- }
198
- }
199
-
200
- export class WitnessArgs {
201
- constructor(
202
- public lock?: Hex,
203
- public inputType?: Hex,
204
- public outputType?: Hex,
205
- ) {}
206
-
207
- static from({
208
- lock,
209
- inputType,
210
- outputType,
211
- }: {
212
- lock?: BytesLike;
213
- inputType?: BytesLike;
214
- outputType?: BytesLike;
215
- }): WitnessArgs {
216
- return new WitnessArgs(
217
- lock ? toHex(lock) : undefined,
218
- inputType ? toHex(inputType) : undefined,
219
- outputType ? toHex(outputType) : undefined,
220
- );
221
- }
222
-
223
- static _toMolData(witnessArgs: DeepReadonly<WitnessArgs>) {
224
- return {
225
- lock: witnessArgs.lock ? toBytes(witnessArgs.lock) : undefined,
226
- inputType: witnessArgs.inputType
227
- ? toBytes(witnessArgs.inputType)
228
- : undefined,
229
- outputType: witnessArgs.outputType
230
- ? toBytes(witnessArgs.outputType)
231
- : undefined,
232
- };
233
- }
234
-
235
- static encode(witnessArgs: DeepReadonly<WitnessArgs>): Uint8Array {
236
- return toBytes(
237
- mol.SerializeWitnessArgs(WitnessArgs._toMolData(witnessArgs)),
238
- );
239
- }
240
-
241
- static decode(bytes: BytesLike | mol.WitnessArgs): WitnessArgs {
242
- const view =
243
- bytes instanceof mol.WitnessArgs
244
- ? bytes
245
- : new mol.WitnessArgs(toBytes(bytes));
246
- const lock = view.getLock();
247
- const inputType = view.getInputType();
248
- const outputType = view.getOutputType();
249
- return {
250
- lock: lock.hasValue() ? toHex(lock.value().raw()) : undefined,
251
- inputType: inputType.hasValue()
252
- ? toHex(inputType.value().raw())
253
- : undefined,
254
- outputType: outputType.hasValue()
255
- ? toHex(outputType.value().raw())
256
- : undefined,
257
- };
258
- }
259
- }
260
-
261
- export class Transaction {
262
- constructor(
263
- public version: string,
264
- public cellDeps: CellDep[],
265
- public headerDeps: Hex[],
266
- public inputs: CellInput[],
267
- public outputs: CellOutput[],
268
- public outputsData: Hex[],
269
- public witnesses: Hex[],
270
- ) {}
271
-
272
- static default(): Transaction {
273
- return new Transaction("0", [], [], [], [], [], []);
274
- }
275
-
276
- private static fromLumosOutPoint(outPoint: {
277
- index: string;
278
- txHash: string;
279
- }) {
280
- return {
281
- ...outPoint,
282
- txHash: toHexFromHex(outPoint.txHash),
283
- };
284
- }
285
-
286
- private static fromLumosScript(script: {
287
- codeHash: string;
288
- hashType: HashType;
289
- args: string;
290
- }) {
291
- return {
292
- codeHash: toHexFromHex(script.codeHash),
293
- args: toHexFromHex(script.args),
294
- hashType: script.hashType,
295
- };
296
- }
297
-
298
- private static fromLumosOutput(output: {
299
- lock: Parameters<typeof Transaction.fromLumosScript>[0];
300
- type?: Parameters<typeof Transaction.fromLumosScript>[0];
301
- capacity: string;
302
- }) {
303
- return {
304
- lock: Transaction.fromLumosScript(output.lock),
305
- type: output.type ? Transaction.fromLumosScript(output.type) : undefined,
306
- capacity: toNumber(output.capacity),
307
- };
308
- }
309
-
310
- static fromLumosSkeleton(skeleton: TransactionSkeletonType): Transaction {
311
- return new Transaction(
312
- "0",
313
- skeleton.cellDeps.toArray().map((cellDep) =>
314
- CellDep.from({
315
- ...cellDep,
316
- outPoint: Transaction.fromLumosOutPoint(cellDep.outPoint),
317
- }),
318
- ),
319
- skeleton.headerDeps.toArray().map(toHex),
320
- skeleton.inputs.toArray().map((input, i) => {
321
- if (!input.outPoint) {
322
- throw new Error("outPoint is required in input");
323
- }
324
-
325
- return CellInput.from({
326
- previousOutput: Transaction.fromLumosOutPoint(input.outPoint),
327
- since: skeleton.inputSinces.get(i, "0x00"),
328
- cellOutput: Transaction.fromLumosOutput(input.cellOutput),
329
- outputData: toHexFromHex(input.data),
330
- });
331
- }),
332
- skeleton.outputs
333
- .toArray()
334
- .map((output) =>
335
- CellOutput.from(Transaction.fromLumosOutput(output.cellOutput)),
336
- ),
337
- skeleton.outputs.toArray().map((output) => toHex(output.data)),
338
- skeleton.witnesses.toArray().map(toHex),
339
- );
340
- }
341
-
342
- static encodeRaw(
343
- transaction: Readonly<Omit<Transaction, "witnesses">>,
344
- ): Uint8Array {
345
- return toBytes(
346
- mol.SerializeRawTransaction({
347
- version: toBytesFromNumber(transaction.version, 4),
348
- cellDeps: transaction.cellDeps.map(CellDep._toMolData),
349
- headerDeps: transaction.headerDeps.map(toBytes),
350
- inputs: transaction.inputs.map(CellInput._toMolData),
351
- outputs: transaction.outputs.map(CellOutput._toMolData),
352
- outputsData: transaction.outputsData.map(toBytes),
353
- }),
354
- );
355
- }
356
-
357
- static hashRaw(transaction: Omit<Transaction, "witnesses">) {
358
- return ckbHash(Transaction.encodeRaw(transaction));
359
- }
360
-
361
- static hashWitnessToHasher(witness: Hex, hasher: Hasher) {
362
- const raw = toBytes(witness);
363
- hasher.update(toBytesFromNumber(raw.length, 8))
364
- hasher.update(raw);
365
- }
366
- }
package/src/fixedPoint.ts DELETED
@@ -1,53 +0,0 @@
1
- export function composeFixedPoint(
2
- val: undefined | null,
3
- decimals?: number,
4
- ): null;
5
- export function composeFixedPoint(val: bigint, decimals?: number): string;
6
- export function composeFixedPoint(
7
- val: bigint | undefined | null,
8
- decimals?: number,
9
- ): string | null;
10
- export function composeFixedPoint(
11
- val: bigint | undefined | null,
12
- decimals = 8,
13
- ): string | null {
14
- if (val == null) {
15
- return null;
16
- }
17
- const str = val.toString();
18
- const l = str.length <= decimals ? "0" : str.slice(0, -decimals);
19
- const r = str.slice(-decimals).padStart(decimals, "0").replace(/0*$/, "");
20
- if (r === "") {
21
- return l;
22
- }
23
-
24
- return `${l}.${r}`;
25
- }
26
-
27
- export function parseFixedPoint(ori: undefined | null, decimals?: number): null;
28
- export function parseFixedPoint(
29
- ori: string | number,
30
- decimals?: number,
31
- ): bigint;
32
- export function parseFixedPoint(
33
- ori: string | number | undefined | null,
34
- decimals?: number,
35
- ): bigint | null;
36
- export function parseFixedPoint(
37
- ori: string | number | undefined | null,
38
- decimals = 8,
39
- ): bigint | null {
40
- if (ori == null) {
41
- return null;
42
- }
43
- const [l, r] = ori.toString().split(".");
44
- const lVal = BigInt(l.padEnd(l.length + decimals, "0"));
45
- if (r === undefined) {
46
- return lVal;
47
- }
48
-
49
- return lVal + BigInt(r.slice(0, decimals).padEnd(decimals, "0"));
50
- }
51
-
52
- export const Zero = 0n;
53
- export const One = parseFixedPoint("1");
package/src/primitive.ts DELETED
@@ -1,8 +0,0 @@
1
- export type Hex = `0x${string}`;
2
- export type BytesLike =
3
- | Hex
4
- | Uint8Array
5
- | ArrayBuffer
6
- | number[];
7
- export type HexLike = BytesLike | number | string;
8
- export type NumberLike = string | Hex | number | bigint;
package/src/viewer.ts DELETED
@@ -1,22 +0,0 @@
1
- import { Address } from "./address";
2
- import { Client } from "./client";
3
-
4
- export abstract class Viewer {
5
- constructor(public readonly client: Client) {}
6
-
7
- abstract getInternalAddress(): Promise<string>;
8
-
9
- abstract getAddressObjs(): Promise<Address[]>;
10
- async getRecommendedAddressObj(_preference?: unknown): Promise<Address> {
11
- return (await this.getAddressObjs())[0];
12
- }
13
-
14
- async getRecommendedAddress(preference?: unknown): Promise<string> {
15
- return Address.toString(await this.getRecommendedAddressObj(preference));
16
- }
17
- async getAddresses(): Promise<string[]> {
18
- return this.getAddressObjs().then((addresses) =>
19
- addresses.map((address) => Address.toString(address)),
20
- );
21
- }
22
- }