@kontor/kontor-sdk 1.0.0-alpha.18 → 1.0.0-alpha.20

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 (117) hide show
  1. package/dist/cjs/tsconfig.build.tsbuildinfo +1 -1
  2. package/dist/esm/exports/index.js.map +1 -1
  3. package/dist/esm/tsconfig.build.tsbuildinfo +1 -1
  4. package/dist/src/exports/index.test.js +27 -0
  5. package/dist/src/exports/index.test.js.map +1 -0
  6. package/dist/src/sdk/accounts/private-key-to-account.js +42 -0
  7. package/dist/src/sdk/accounts/private-key-to-account.js.map +1 -0
  8. package/dist/src/sdk/actions/get-contract.js +50 -0
  9. package/dist/src/sdk/actions/get-contract.js.map +1 -0
  10. package/dist/src/sdk/actions/get-contract.test-d.js +43 -0
  11. package/dist/src/sdk/actions/get-contract.test-d.js.map +1 -0
  12. package/dist/src/sdk/actions/kontor/public/call-view.js +21 -0
  13. package/dist/src/sdk/actions/kontor/public/call-view.js.map +1 -0
  14. package/dist/src/sdk/actions/kontor/public/inspect.js +20 -0
  15. package/dist/src/sdk/actions/kontor/public/inspect.js.map +1 -0
  16. package/dist/src/sdk/actions/kontor/public/proc-contract.test.js +47 -0
  17. package/dist/src/sdk/actions/kontor/public/proc-contract.test.js.map +1 -0
  18. package/dist/src/sdk/actions/kontor/public/view-contract.js +47 -0
  19. package/dist/src/sdk/actions/kontor/public/view-contract.js.map +1 -0
  20. package/dist/src/sdk/actions/kontor/public/view-contract.test-d.js +29 -0
  21. package/dist/src/sdk/actions/kontor/public/view-contract.test-d.js.map +1 -0
  22. package/dist/src/sdk/actions/kontor/wallet/sign-reveal.js +59 -0
  23. package/dist/src/sdk/actions/kontor/wallet/sign-reveal.js.map +1 -0
  24. package/dist/src/sdk/actions/wallet/get-addresses.js +20 -0
  25. package/dist/src/sdk/actions/wallet/get-addresses.js.map +1 -0
  26. package/dist/src/sdk/actions/wallet/sign-psbt.js +28 -0
  27. package/dist/src/sdk/actions/wallet/sign-psbt.js.map +1 -0
  28. package/dist/src/sdk/clients/base/create-http-client.js +41 -0
  29. package/dist/src/sdk/clients/base/create-http-client.js.map +1 -0
  30. package/dist/src/sdk/clients/base/create-rpc-client.js +41 -0
  31. package/dist/src/sdk/clients/base/create-rpc-client.js.map +1 -0
  32. package/dist/src/sdk/clients/base/create-rpc-client.test.js +151 -0
  33. package/dist/src/sdk/clients/base/create-rpc-client.test.js.map +1 -0
  34. package/dist/src/sdk/clients/create-public-client.js +13 -0
  35. package/dist/src/sdk/clients/create-public-client.js.map +1 -0
  36. package/dist/src/sdk/clients/create-public-client.test.js +121 -0
  37. package/dist/src/sdk/clients/create-public-client.test.js.map +1 -0
  38. package/dist/src/sdk/clients/create-wallet-client.js +14 -0
  39. package/dist/src/sdk/clients/create-wallet-client.js.map +1 -0
  40. package/dist/src/sdk/clients/kontor/create-public-client.js +14 -0
  41. package/dist/src/sdk/clients/kontor/create-public-client.js.map +1 -0
  42. package/dist/src/sdk/clients/kontor/create-public-client.test.js +45 -0
  43. package/dist/src/sdk/clients/kontor/create-public-client.test.js.map +1 -0
  44. package/dist/src/sdk/clients/transports/create-rpc-transport.js +36 -0
  45. package/dist/src/sdk/clients/transports/create-rpc-transport.js.map +1 -0
  46. package/dist/src/sdk/contracts/wits.js +26 -0
  47. package/dist/src/sdk/contracts/wits.js.map +1 -0
  48. package/dist/src/sdk/types/chain.js +2 -0
  49. package/dist/src/sdk/types/chain.js.map +1 -0
  50. package/dist/src/sdk/types/contract.js +2 -0
  51. package/dist/src/sdk/types/contract.js.map +1 -0
  52. package/dist/src/sdk/types/contract.test-d.js +34 -0
  53. package/dist/src/sdk/types/contract.test-d.js.map +1 -0
  54. package/dist/src/sdk/types/http-schema.js +73 -0
  55. package/dist/src/sdk/types/http-schema.js.map +1 -0
  56. package/dist/src/sdk/types/rpc-schema.js +2 -0
  57. package/dist/src/sdk/types/rpc-schema.js.map +1 -0
  58. package/dist/src/sdk/utils/chain/defineChain.js +9 -0
  59. package/dist/src/sdk/utils/chain/defineChain.js.map +1 -0
  60. package/dist/src/sdk/utils/wit/codecs/u64.js +63 -0
  61. package/dist/src/sdk/utils/wit/codecs/u64.js.map +1 -0
  62. package/dist/src/sdk/utils/wit/decode-wit-parameter.js +289 -0
  63. package/dist/src/sdk/utils/wit/decode-wit-parameter.js.map +1 -0
  64. package/dist/src/sdk/utils/wit/encode-function-data.test-d.js +24 -0
  65. package/dist/src/sdk/utils/wit/encode-function-data.test-d.js.map +1 -0
  66. package/dist/src/sdk/utils/wit/encode-function-data.test.js +91 -0
  67. package/dist/src/sdk/utils/wit/encode-function-data.test.js.map +1 -0
  68. package/dist/src/sdk/utils/wit/encode-wit-parameters.js +261 -0
  69. package/dist/src/sdk/utils/wit/encode-wit-parameters.js.map +1 -0
  70. package/dist/src/sdk/utils/wit/get-wit-item.test.js +167 -0
  71. package/dist/src/sdk/utils/wit/get-wit-item.test.js.map +1 -0
  72. package/dist/src/wit/wit-parser/core/records.test.js +63 -0
  73. package/dist/src/wit/wit-parser/core/records.test.js.map +1 -0
  74. package/dist/src/wit/wit-parser/core/types/records.test-d.js +19 -0
  75. package/dist/src/wit/wit-parser/core/types/records.test-d.js.map +1 -0
  76. package/dist/src/wit/wit-parser/core/types/utils.js +2 -0
  77. package/dist/src/wit/wit-parser/core/types/utils.js.map +1 -0
  78. package/dist/src/wit/wit-parser/core/types/utils.test-d.js +159 -0
  79. package/dist/src/wit/wit-parser/core/types/utils.test-d.js.map +1 -0
  80. package/dist/src/wit/wit-parser/core/utils.js +213 -0
  81. package/dist/src/wit/wit-parser/core/utils.js.map +1 -0
  82. package/dist/src/wit/wit-parser/core/utils.test.js +316 -0
  83. package/dist/src/wit/wit-parser/core/utils.test.js.map +1 -0
  84. package/dist/src/wit/wit-parser/parse-wit-parameter.test.js +93 -0
  85. package/dist/src/wit/wit-parser/parse-wit-parameter.test.js.map +1 -0
  86. package/dist/src/wit/wit-parser/parse-wit.test.js +80 -0
  87. package/dist/src/wit/wit-parser/parse-wit.test.js.map +1 -0
  88. package/dist/tsconfig.tsbuildinfo +1 -0
  89. package/dist/types/exports/index.d.ts +1 -1
  90. package/dist/types/exports/index.d.ts.map +1 -1
  91. package/package.json +1 -1
  92. package/src/exports/index.test.ts +18 -1
  93. package/src/exports/index.ts +1 -1
  94. package/src/sdk/actions/kontor/public/call-view.ts +6 -14
  95. package/src/sdk/actions/kontor/public/proc-contract.test.ts +1 -1
  96. package/src/sdk/actions/kontor/public/view-contract.test-d.ts +2 -17
  97. package/src/sdk/actions/kontor/public/view-contract.ts +8 -3
  98. package/src/sdk/actions/kontor/wallet/sign-reveal.ts +3 -35
  99. package/src/sdk/clients/base/create-rpc-client.test.ts +10 -4
  100. package/src/sdk/clients/create-public-client.test.ts +11 -3
  101. package/src/sdk/clients/kontor/create-public-client.test.ts +4 -3
  102. package/src/sdk/contracts/wits.ts +1 -1
  103. package/src/sdk/types/contract.test-d.ts +29 -24
  104. package/src/sdk/utils/wit/codecs/u64.ts +73 -0
  105. package/src/sdk/utils/wit/decode-wit-parameter.ts +5 -0
  106. package/src/sdk/utils/wit/encode-function-data.test-d.ts +15 -11
  107. package/src/sdk/utils/wit/encode-function-data.test.ts +1 -1
  108. package/src/sdk/utils/wit/encode-wit-parameters.ts +16 -1
  109. package/src/sdk/utils/wit/get-wit-item.test.ts +58 -9
  110. package/src/wit/wit-parser/core/records.test.ts +24 -14
  111. package/src/wit/wit-parser/core/types/records.test-d.ts +141 -28
  112. package/src/wit/wit-parser/core/types/utils.test-d.ts +23 -2
  113. package/src/wit/wit-parser/core/types/utils.ts +8 -41
  114. package/src/wit/wit-parser/core/utils.test.ts +14 -0
  115. package/src/wit/wit-parser/core/utils.ts +1 -0
  116. package/src/wit/wit-parser/parse-wit-parameter.test.ts +30 -3
  117. package/src/wit/wit-parser/parse-wit.test.ts +93 -102
@@ -1,3 +1,7 @@
1
+ import type {
2
+ ResultResponse,
3
+ ViewResult,
4
+ } from "../../../../sdk/types/kontor.js";
1
5
  import type { ResolvedRegister } from "../../../../wit/register.js";
2
6
  import type { KontorPublicClient } from "../../../clients/kontor/create-public-client.js";
3
7
  import type { HttpTransport } from "../../../clients/transports/create-http-transport.js";
@@ -11,19 +15,7 @@ export type CallViewParameters<
11
15
  };
12
16
 
13
17
  // TODO: refine this type
14
- export type CallViewReturnType =
15
- | {
16
- result: {
17
- Ok: {
18
- value: string;
19
- };
20
- };
21
- }
22
- | {
23
- result: {
24
- Err: unknown;
25
- };
26
- };
18
+ export type CallViewReturnType = ResultResponse<ViewResult>;
27
19
 
28
20
  export async function callView<
29
21
  chain extends Chain | undefined = Chain | undefined,
@@ -42,7 +34,7 @@ export async function callView<
42
34
  body: { expr: wave },
43
35
  });
44
36
 
45
- console.log("callView response:", response);
37
+ console.log("callView response:", JSON.stringify(response, null, 2));
46
38
 
47
39
  // TODO: fix CallViwewReturnType
48
40
  // @ts-ignore
@@ -22,7 +22,7 @@ const input = [
22
22
 
23
23
  const wit = parseWit(input);
24
24
 
25
- test("e2e", async () => {
25
+ test.skip("e2e", async () => {
26
26
  const client = createPublicClient({
27
27
  transport: http("https://signet.kontor.network:35000/api"),
28
28
  // optional parameter
@@ -3,24 +3,9 @@ import { viewContract } from "./view-contract.js";
3
3
  import { parseWit } from "../../../../wit/wit-parser/parse-wit.js";
4
4
  import { createPublicClient } from "../../../clients/kontor/create-public-client.js";
5
5
  import { http } from "../../../clients/transports/http.js";
6
+ import { nativeToken } from "../../../contracts/wits.js";
6
7
 
7
- // define the wit
8
- const input = [
9
- "record balance { key: string, value: decimal }",
10
- // TODO: barfing on core-context
11
- // "export issuance: func(ctx: borrow<core-context>, n: decimal) -> result<_, error>;",
12
- // "export hold: func(ctx: borrow<core-context>, n: decimal) -> result<_, error>;",
13
- // "export burn-and-release: func(ctx: borrow<core-context>, n: decimal) -> result<_, error>;",
14
- "export init: func(ctx: borrow<proc-context>);",
15
- "export mint: func(ctx: borrow<proc-context>, n: decimal) -> result<_, error>;",
16
- "export burn: func(ctx: borrow<proc-context>, n: decimal) -> result<_, error>;",
17
- "export transfer: func(ctx: borrow<proc-context>, to: string, n: decimal) -> result<_, error>;",
18
- "export balance: func(ctx: borrow<view-context>, acc: string) -> option<decimal>;",
19
- "export balances: func(ctx: borrow<view-context>) -> list<balance>;",
20
- "export total-supply: func(ctx: borrow<view-context>) -> decimal;",
21
- ] as const;
22
-
23
- const wit = parseWit(input);
8
+ const wit = parseWit(nativeToken.raw);
24
9
 
25
10
  test("e2e", async () => {
26
11
  const client = createPublicClient({
@@ -82,11 +82,14 @@ export async function viewContract<
82
82
  wave: wave,
83
83
  });
84
84
 
85
- if ("Err" in response.result) {
86
- throw new Error("Contract view call failed");
85
+ console.log(response.result.type);
86
+
87
+ if (response.result.type == "Err") {
88
+ // TODO: add custom error
89
+ throw new Error(response.result.message);
87
90
  }
88
91
 
89
- const value = response.result.Ok.value;
92
+ const value = response.result.value;
90
93
 
91
94
  return decodeFunctionResult({
92
95
  wit: wit,
@@ -95,7 +98,9 @@ export async function viewContract<
95
98
  }) as ViewContractReturnType<wit, functionName>;
96
99
  //
97
100
  } catch (error) {
101
+ throw error;
98
102
  throw new Error("problems");
103
+ // TODO: add custom error
99
104
  // throw getContractError(error as BaseError, {
100
105
  // abi: wit,
101
106
  // address,
@@ -48,9 +48,7 @@ export async function signReveal<
48
48
  // TODO: assertRequest(account, paramatetrs )
49
49
  // TODO: assertCurrentChain(chanId, chain)
50
50
 
51
- const tx = Transaction.fromPSBT(hex.decode(params.psbt), {
52
- allowUnknownInputs: true,
53
- });
51
+ const tx = Transaction.fromPSBT(hex.decode(params.psbt));
54
52
 
55
53
  for (let i = 0; i < params.parcipantScripts.length; i++) {
56
54
  const { commit_tap_leaf_script } = params.parcipantScripts[i]!;
@@ -103,25 +101,8 @@ export async function signReveal<
103
101
  broadcast: false,
104
102
  });
105
103
 
106
- const signedTx = Transaction.fromPSBT(hex.decode(signed.psbt as string), {
107
- allowUnknownInputs: true,
108
- });
109
-
110
- // for (let i = 0; i < signedTx.inputsLength; i++) {
111
- // const input = signedTx.getInput(i);
112
- // if (input.tapLeafScript && input.tapLeafScript.length > 0) {
113
- // const [cbStruct, scriptWithVersion] = input.tapLeafScript[0]!;
114
- // console.log("SIGNED TAPLEAF", i, {
115
- // cbStruct,
116
- // cbEncoded: hex.encode(TaprootControlBlock.encode(cbStruct)),
117
- // scriptWithVersion: hex.encode(scriptWithVersion),
118
- // });
119
- // }
120
- // }
121
- //
122
- //
123
-
124
- // finalize
104
+ const signedTx = Transaction.fromPSBT(hex.decode(signed.psbt as string));
105
+
125
106
  for (let i = 0; i < params.parcipantScripts.length; i++) {
126
107
  const input = signedTx.getInput(i);
127
108
  const p = params.parcipantScripts[i]!;
@@ -129,12 +110,6 @@ export async function signReveal<
129
110
  const scriptHex = p.commit_tap_leaf_script.script;
130
111
  const cbHex = p.commit_tap_leaf_script.controlBlock;
131
112
 
132
- // console.log("USING SCRIPT/CB", {
133
- // scriptHex,
134
- // cbHex,
135
- // cbDecodedRoundtrip: hex.encode(hex.decode(cbHex)),
136
- // });
137
-
138
113
  signedTx.updateInput(i, {
139
114
  finalScriptWitness: [
140
115
  input.tapScriptSig![0]![1],
@@ -144,12 +119,5 @@ export async function signReveal<
144
119
  });
145
120
  }
146
121
 
147
- // for (let i = 0; i < signedTx.inputsLength; i++) {
148
- // const input = signedTx.getInput(i);
149
- // console.log("FINAL INPUT", i, {
150
- // finalScriptWitness: input.finalScriptWitness?.map((w) => hex.encode(w)),
151
- // });
152
- // }
153
-
154
122
  return signedTx;
155
123
  }
@@ -85,17 +85,23 @@ describe("transports", () => {
85
85
  "name": "Kontor",
86
86
  "symbol": "KOR",
87
87
  },
88
+ "networkConfig": {
89
+ "bech32": "tb",
90
+ "pubKeyHash": 111,
91
+ "scriptHash": 196,
92
+ "wif": 239,
93
+ },
88
94
  "serializers": undefined,
89
95
  "urls": {
90
96
  "default": {
91
97
  "bitcoinRpc": [
92
- "https://signet.kontor.network:38332",
98
+ "https://signet-staging.kontor.network:38332",
93
99
  ],
94
100
  "http": [
95
- "https://signet.kontor.network:35000/api",
101
+ "https://signet-staging.kontor.network:35000/api",
96
102
  ],
97
103
  "webSocket": [
98
- "wss://signet.kontor.network:35000/ws",
104
+ "wss://signet-staging.kontor.network:35000/ws",
99
105
  ],
100
106
  },
101
107
  },
@@ -115,7 +121,7 @@ describe("transports", () => {
115
121
  "retryDelay": 150,
116
122
  "timeout": 10000,
117
123
  "type": "http-rpc",
118
- "url": "https://signet.kontor.network:38332",
124
+ "url": "https://signet-staging.kontor.network:38332",
119
125
  },
120
126
  "type": "base",
121
127
  }
@@ -33,6 +33,7 @@ test("creates", () => {
33
33
  "pollingInterval": 4000,
34
34
  "request": [Function],
35
35
  "sendRawTransaction": [Function],
36
+ "testMempoolAccept": [Function],
36
37
  "transport": {
37
38
  "key": "mock",
38
39
  "methods": undefined,
@@ -87,17 +88,23 @@ test("with chain", () => {
87
88
  "name": "Kontor",
88
89
  "symbol": "KOR",
89
90
  },
91
+ "networkConfig": {
92
+ "bech32": "tb",
93
+ "pubKeyHash": 111,
94
+ "scriptHash": 196,
95
+ "wif": 239,
96
+ },
90
97
  "serializers": undefined,
91
98
  "urls": {
92
99
  "default": {
93
100
  "bitcoinRpc": [
94
- "https://signet.kontor.network:38332",
101
+ "https://signet-staging.kontor.network:38332",
95
102
  ],
96
103
  "http": [
97
- "https://signet.kontor.network:35000/api",
104
+ "https://signet-staging.kontor.network:35000/api",
98
105
  ],
99
106
  "webSocket": [
100
- "wss://signet.kontor.network:35000/ws",
107
+ "wss://signet-staging.kontor.network:35000/ws",
101
108
  ],
102
109
  },
103
110
  },
@@ -108,6 +115,7 @@ test("with chain", () => {
108
115
  "pollingInterval": 4000,
109
116
  "request": [Function],
110
117
  "sendRawTransaction": [Function],
118
+ "testMempoolAccept": [Function],
111
119
  "transport": {
112
120
  "key": "mock",
113
121
  "methods": undefined,
@@ -11,13 +11,14 @@ test("creates", async () => {
11
11
  expect(client).toMatchInlineSnapshot(`
12
12
  {
13
13
  "account": undefined,
14
- "buildComposeCall": [Function],
15
- "buildInstruction": [Function],
14
+ "buildComposeQuery": [Function],
15
+ "buildInstructionQuery": [Function],
16
16
  "cacheTime": 4000,
17
- "callProc": [Function],
17
+ "callCompose": [Function],
18
18
  "callView": [Function],
19
19
  "chain": undefined,
20
20
  "extend": [Function],
21
+ "inspect": [Function],
21
22
  "key": "kontor-http",
22
23
  "name": "KONTOR-HTTP Client",
23
24
  "pollingInterval": 4000,
@@ -23,7 +23,7 @@ const nativeTokenRaw = [
23
23
  "export balance: func(ctx: borrow<view-context>, acc: string) -> option<decimal>;",
24
24
  "export balances: func(ctx: borrow<view-context>) -> list<balance>;",
25
25
  "export total-supply: func(ctx: borrow<view-context>) -> decimal;",
26
- "export attach: func(ctx: borrow<proc-context>, vout: u64, amt: decimal) -> result<transfer, error>;",
26
+ // "export attach: func(ctx: borrow<proc-context>, vout: u64, amt: decimal) -> result<transfer, error>;",
27
27
  "export detach: func(ctx: borrow<proc-context>) -> result<transfer, error>;",
28
28
  ] as const;
29
29
 
@@ -4,8 +4,9 @@ import type {
4
4
  ContractFunctionArgs,
5
5
  ContractFunctionReturnType,
6
6
  } from "./contract.js";
7
+ import { nativeToken as nativeToken_ } from "../contracts/wits.js";
7
8
 
8
- import { nativeToken } from "~test/src/wits.js";
9
+ const nativeToken = nativeToken_.wit;
9
10
 
10
11
  test("ContractFunctionName", () => {
11
12
  type All = ContractFunctionName<typeof nativeToken>;
@@ -20,7 +21,7 @@ test("ContractFunctionName", () => {
20
21
  | "balance"
21
22
  | "balances"
22
23
  | "total-supply"
23
- | "attach"
24
+ // | "attach"
24
25
  | "detach"
25
26
  >;
26
27
 
@@ -32,7 +33,11 @@ test("ContractFunctionName", () => {
32
33
 
33
34
  type Proc = ContractFunctionName<typeof nativeToken, "proc">;
34
35
  expectTypeOf<Proc>().toEqualTypeOf<
35
- "mint" | "burn" | "transfer" | "attach" | "detach"
36
+ | "mint"
37
+ | "burn"
38
+ | "transfer"
39
+ //| "attach"
40
+ | "detach"
36
41
  >;
37
42
  });
38
43
 
@@ -68,8 +73,8 @@ test("ContractFunctionArgs", () => {
68
73
  >;
69
74
  expectTypeOf<TotalSupply>().toEqualTypeOf<readonly []>;
70
75
 
71
- type Attach = ContractFunctionArgs<typeof nativeToken, "proc", "attach">;
72
- expectTypeOf<Attach>().toEqualTypeOf<readonly [bigint, [bigint, number]]>;
76
+ // type Attach = ContractFunctionArgs<typeof nativeToken, "proc", "attach">;
77
+ // expectTypeOf<Attach>().toEqualTypeOf<readonly [bigint, [bigint, number]]>;
73
78
 
74
79
  type Detach = ContractFunctionArgs<typeof nativeToken, "proc", "detach">;
75
80
  expectTypeOf<Detach>().toEqualTypeOf<readonly []>;
@@ -209,25 +214,25 @@ test("ContractFunctionReturnType", () => {
209
214
  >;
210
215
  expectTypeOf<TotalSupply>().toEqualTypeOf<[bigint, number]>;
211
216
 
212
- type Attach = ContractFunctionReturnType<
213
- typeof nativeToken,
214
- "proc",
215
- "attach"
216
- >;
217
- expectTypeOf<Attach>().toEqualTypeOf<
218
- | {
219
- ok: false;
220
- error: string;
221
- }
222
- | {
223
- ok: true;
224
- value: {
225
- src: string;
226
- dst: string;
227
- amt: [bigint, number];
228
- };
229
- }
230
- >;
217
+ // type Attach = ContractFunctionReturnType<
218
+ // typeof nativeToken,
219
+ // "proc",
220
+ // "attach"
221
+ // >;
222
+ // expectTypeOf<Attach>().toEqualTypeOf<
223
+ // | {
224
+ // ok: false;
225
+ // error: string;
226
+ // }
227
+ // | {
228
+ // ok: true;
229
+ // value: {
230
+ // src: string;
231
+ // dst: string;
232
+ // amt: [bigint, number];
233
+ // };
234
+ // }
235
+ // >;
231
236
 
232
237
  type Detach = ContractFunctionReturnType<
233
238
  typeof nativeToken,
@@ -0,0 +1,73 @@
1
+ // u64.ts
2
+ import * as z from "zod";
3
+ import type { WaveExpr, CodecInstance, CodecConstructor } from "./types.js";
4
+
5
+ // You already have this in integer.ts; you can import it from there instead
6
+ const U64_MAX = (1n << 64n) - 1n;
7
+ export const U64 = z
8
+ .bigint()
9
+ .min(0n, { message: "must be ≥ 0" })
10
+ .max(U64_MAX, { message: "must be ≤ 2^64-1" });
11
+
12
+ /**
13
+ * Parse a Wave expression for a u64.
14
+ *
15
+ * - Accepts optional underscores for readability: 1_000_000
16
+ * - Must be a non-negative integer
17
+ * - Enforces the u64 range via zod
18
+ */
19
+ export function parseU64WaveExpr(expr: string): bigint {
20
+ const trimmed = expr.trim();
21
+ if (!trimmed) throw new Error("empty u64 expression");
22
+
23
+ // Allow underscores like "1_000_000"
24
+ const normalized = trimmed.replace(/_/g, "");
25
+
26
+ if (!/^[0-9]+$/.test(normalized)) {
27
+ throw new Error(`invalid u64 literal: "${expr}"`);
28
+ }
29
+
30
+ const value = BigInt(normalized);
31
+ return U64.parse(value); // range-check via zod
32
+ }
33
+
34
+ // --- U64Codec ---------------------------------------------------
35
+
36
+ class _U64Codec implements CodecInstance<bigint> {
37
+ public readonly value: bigint;
38
+
39
+ constructor(value: bigint) {
40
+ this.value = U64.parse(value);
41
+ }
42
+
43
+ static fromJs(value: bigint | number | string): _U64Codec {
44
+ let v: bigint;
45
+ if (typeof value === "bigint") v = value;
46
+ else if (typeof value === "number") v = BigInt(value);
47
+ else v = BigInt(value); // e.g. JSON string "12345"
48
+
49
+ return new _U64Codec(v);
50
+ }
51
+
52
+ static fromKontor(data: string | bigint | number): _U64Codec {
53
+ return _U64Codec.fromJs(data);
54
+ }
55
+
56
+ static parseWave(expr: string): bigint {
57
+ return parseU64WaveExpr(expr);
58
+ }
59
+
60
+ toWave(): WaveExpr {
61
+ return `${this.value}` as WaveExpr;
62
+ }
63
+
64
+ toJs(): bigint {
65
+ return this.value;
66
+ }
67
+ }
68
+
69
+ export const U64Codec = _U64Codec as unknown as CodecConstructor<
70
+ bigint,
71
+ _U64Codec,
72
+ bigint
73
+ >;
@@ -1,6 +1,7 @@
1
1
  import type { WitParameter, WitType } from "../../../wit/wit.js";
2
2
  import { InvalidWitEncodingTypeError } from "../../errors/wit.js";
3
3
  import { DecimalCodec } from "./codecs/decimal.js";
4
+ import { U64Codec } from "./codecs/u64.js";
4
5
  import { StringCodec } from "./codecs/string.js";
5
6
  import { OptionCodecFactory } from "./codecs/option.js";
6
7
  import { ListCodecFactory } from "./codecs/list.js";
@@ -11,11 +12,14 @@ import {
11
12
  type WaveExpr,
12
13
  } from "./codecs/types.js";
13
14
 
15
+ const u64Decoder = makeDecoder(U64Codec);
16
+
14
17
  const stringDecoder = makeDecoder(StringCodec);
15
18
  const decimalDecoder = makeDecoder(DecimalCodec);
16
19
  const optionOfDecimalDecoder = makeDecoder(
17
20
  OptionCodecFactory.create(DecimalCodec),
18
21
  );
22
+
19
23
  const optionOfTupleDecoder = (param: WitParameter) =>
20
24
  makeDecoder(OptionCodecFactory.create(makeTupleCodecConstructor(param)));
21
25
 
@@ -95,6 +99,7 @@ function decoderForParam(p: WitParameter) {
95
99
  case "u16":
96
100
  case "u32":
97
101
  case "u64":
102
+ return u64Decoder;
98
103
  case "integer":
99
104
  case "option<string>":
100
105
  case "option<bool>":
@@ -4,8 +4,9 @@ import {
4
4
  encodeFunctionData,
5
5
  } from "./encode-function-data.js";
6
6
 
7
- import { nativeToken } from "~test/src/wits.js";
8
- import { parseWit } from "src/wit/wit-parser/parse-wit.js";
7
+ import { nativeToken } from "../../contracts/wits.js";
8
+
9
+ import { parseWit } from "../../../wit/wit-parser/parse-wit.js";
9
10
 
10
11
  test("single, function name not required", () => {
11
12
  const singleRaw = [
@@ -25,23 +26,26 @@ test("single, function name not required", () => {
25
26
  });
26
27
 
27
28
  test("native token", () => {
28
- // @ts-expect-error functionName required
29
- encodeFunctionData({ wit: nativeToken });
29
+ try {
30
+ // @ts-expect-error functionName required
31
+ encodeFunctionData({ wit: nativeToken });
32
+ } catch (e) {}
33
+
34
+ type P = EncodeFunctionDataParameters<typeof nativeToken.wit>;
30
35
 
31
- type P = EncodeFunctionDataParameters<typeof nativeToken>;
32
36
  expectTypeOf<P["functionName"]>().not.toEqualTypeOf<undefined>();
37
+
33
38
  expectTypeOf<P["functionName"]>().toEqualTypeOf<
39
+ | "mint"
34
40
  | "issuance"
41
+ | "balance"
42
+ | "transfer"
43
+ | "burn"
35
44
  | "hold"
36
45
  | "release"
37
- // | "init" // init is not valid
38
- | "mint"
39
- | "burn"
40
- | "transfer"
41
- | "balance"
42
46
  | "balances"
43
47
  | "total-supply"
44
- | "attach"
45
48
  | "detach"
49
+ // | "attach" // TODO: need to handle attach parsing
46
50
  >();
47
51
  });
@@ -119,5 +119,5 @@ test("attach", () => {
119
119
  functionName: "attach",
120
120
  args: [7n, [7n, 18]],
121
121
  }),
122
- ).toEqual(`attach()`);
122
+ ).toEqual("attach(7, {r0: 7, r1: 0, r2: 0, r3: 0, sign: plus})");
123
123
  });
@@ -8,6 +8,8 @@ import {
8
8
  InvalidWitEncodingTypeError,
9
9
  WitEncodingLengthMismatchError,
10
10
  } from "../../errors/wit.js";
11
+
12
+ import { U64Codec } from "./codecs/u64.js";
11
13
  import { DecimalCodec } from "./codecs/decimal.js";
12
14
  import { IntegerCodec } from "./codecs/integer.js";
13
15
  import { ListCodecFactory } from "./codecs/list.js";
@@ -35,7 +37,6 @@ export function encodeWitParameters<
35
37
  params: params as readonly WitParameter[],
36
38
  values: values as any,
37
39
  });
38
-
39
40
  const encoded = encodeParams(preparedParams);
40
41
 
41
42
  return encoded;
@@ -242,6 +243,14 @@ function prepareParam<const param extends WitParameter>({
242
243
  // if (param.type.startsWith("bytes")) {
243
244
  // return encodeBytes(value as unknown as Hex, { param });
244
245
  // }
246
+ //
247
+
248
+ console.log(param);
249
+
250
+ if (param.type === "u64") {
251
+ return encodeU64(value as unknown as bigint);
252
+ }
253
+
245
254
  if (param.type === "integer") {
246
255
  return encodeInteger(value as unknown as bigint);
247
256
  }
@@ -298,6 +307,12 @@ export function getResultComponentsStrict(
298
307
  return [okType, "error"];
299
308
  }
300
309
 
310
+ function encodeU64(value: bigint): PreparedParam {
311
+ return {
312
+ encoded: U64Codec.fromJs(value).toWave(),
313
+ };
314
+ }
315
+
301
316
  function encodeInteger(value: bigint): PreparedParam {
302
317
  return {
303
318
  encoded: IntegerCodec.fromJs(value).toWave(),
@@ -32,8 +32,19 @@ describe("getWitItem", () => {
32
32
  name: "mint",
33
33
  type: "function",
34
34
  context: "proc",
35
- inputs: [{ type: "decimal", name: "n" }],
36
- outputs: [{ type: "result<_, error>" }],
35
+ inputs: [
36
+ {
37
+ internalType: "decimal",
38
+ type: "decimal",
39
+ name: "n",
40
+ },
41
+ ],
42
+ outputs: [
43
+ {
44
+ internalType: "result<_, error>",
45
+ type: "result<_, error>",
46
+ },
47
+ ],
37
48
  });
38
49
  });
39
50
  test("selects burn function by name", () => {
@@ -46,8 +57,19 @@ describe("getWitItem", () => {
46
57
  name: "burn",
47
58
  type: "function",
48
59
  context: "proc",
49
- inputs: [{ type: "decimal", name: "n" }],
50
- outputs: [{ type: "result<_, error>" }],
60
+ inputs: [
61
+ {
62
+ internalType: "decimal",
63
+ type: "decimal",
64
+ name: "n",
65
+ },
66
+ ],
67
+ outputs: [
68
+ {
69
+ internalType: "result<_, error>",
70
+ type: "result<_, error>",
71
+ },
72
+ ],
51
73
  });
52
74
  });
53
75
  test("selects transfer function by name", () => {
@@ -61,10 +83,23 @@ describe("getWitItem", () => {
61
83
  type: "function",
62
84
  context: "proc",
63
85
  inputs: [
64
- { type: "string", name: "to" },
65
- { type: "decimal", name: "n" },
86
+ {
87
+ internalType: "string",
88
+ type: "string",
89
+ name: "to",
90
+ },
91
+ {
92
+ internalType: "decimal",
93
+ type: "decimal",
94
+ name: "n",
95
+ },
96
+ ],
97
+ outputs: [
98
+ {
99
+ internalType: "result<_, error>",
100
+ type: "result<_, error>",
101
+ },
66
102
  ],
67
- outputs: [{ type: "result<_, error>" }],
68
103
  });
69
104
  });
70
105
 
@@ -78,8 +113,19 @@ describe("getWitItem", () => {
78
113
  name: "balance",
79
114
  type: "function",
80
115
  context: "view",
81
- inputs: [{ type: "string", name: "acc" }],
82
- outputs: [{ type: "option<decimal>" }],
116
+ inputs: [
117
+ {
118
+ internalType: "string",
119
+ type: "string",
120
+ name: "acc",
121
+ },
122
+ ],
123
+ outputs: [
124
+ {
125
+ internalType: "option<decimal>",
126
+ type: "option<decimal>",
127
+ },
128
+ ],
83
129
  });
84
130
  });
85
131
 
@@ -97,12 +143,15 @@ describe("getWitItem", () => {
97
143
  outputs: [
98
144
  {
99
145
  type: "list<tuple>",
146
+ internalType: "list<balance>",
100
147
  components: [
101
148
  {
149
+ internalType: "string",
102
150
  name: "key",
103
151
  type: "string",
104
152
  },
105
153
  {
154
+ internalType: "decimal",
106
155
  name: "value",
107
156
  type: "decimal",
108
157
  },