@weilliptic/weil-sdk 1.0.0

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 (57) hide show
  1. package/README.md +111 -0
  2. package/dist/contracts.d.ts +19 -0
  3. package/dist/contracts.js +86 -0
  4. package/dist/contracts.js.map +1 -0
  5. package/dist/errors.d.ts +11 -0
  6. package/dist/errors.js +13 -0
  7. package/dist/errors.js.map +1 -0
  8. package/dist/index.d.ts +6 -0
  9. package/dist/index.js +7 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/json.d.ts +7 -0
  12. package/dist/json.js +2 -0
  13. package/dist/json.js.map +1 -0
  14. package/dist/podTypes.d.ts +2 -0
  15. package/dist/podTypes.js +2 -0
  16. package/dist/podTypes.js.map +1 -0
  17. package/dist/pods/selectPod.d.ts +2 -0
  18. package/dist/pods/selectPod.js +25 -0
  19. package/dist/pods/selectPod.js.map +1 -0
  20. package/dist/schema.d.ts +29 -0
  21. package/dist/schema.js +48 -0
  22. package/dist/schema.js.map +1 -0
  23. package/dist/transactions/signature.d.ts +33 -0
  24. package/dist/transactions/signature.js +10 -0
  25. package/dist/transactions/signature.js.map +1 -0
  26. package/dist/types.d.ts +110 -0
  27. package/dist/utils/common.d.ts +8 -0
  28. package/dist/utils/common.js +45 -0
  29. package/dist/utils/common.js.map +1 -0
  30. package/dist/utils/contract.d.ts +5 -0
  31. package/dist/utils/contract.js +30 -0
  32. package/dist/utils/contract.js.map +1 -0
  33. package/dist/utils/formats.d.ts +6 -0
  34. package/dist/utils/formats.js +26 -0
  35. package/dist/utils/formats.js.map +1 -0
  36. package/dist/utils/index.d.ts +1 -0
  37. package/dist/utils/index.js +2 -0
  38. package/dist/utils/index.js.map +1 -0
  39. package/dist/utils/keys.d.ts +6 -0
  40. package/dist/utils/keys.js +12 -0
  41. package/dist/utils/keys.js.map +1 -0
  42. package/dist/utils/numberToBytes.d.ts +1 -0
  43. package/dist/utils/numberToBytes.js +15 -0
  44. package/dist/utils/numberToBytes.js.map +1 -0
  45. package/dist/utils/utils.test.d.ts +1 -0
  46. package/dist/utils/utils.test.js +23 -0
  47. package/dist/utils/utils.test.js.map +1 -0
  48. package/dist/wallet.d.ts +98 -0
  49. package/dist/wallet.js +248 -0
  50. package/dist/wallet.js.map +1 -0
  51. package/dist/walletCommon.d.ts +66 -0
  52. package/dist/walletCommon.js +92 -0
  53. package/dist/walletCommon.js.map +1 -0
  54. package/dist/walletConnection.d.ts +13 -0
  55. package/dist/walletConnection.js +31 -0
  56. package/dist/walletConnection.js.map +1 -0
  57. package/package.json +51 -0
package/README.md ADDED
@@ -0,0 +1,111 @@
1
+ # Weil SDK
2
+
3
+ Welcome to Weil SDK for TypeScript / JavaScript.
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ npm i @weilliptic/weil-sdk
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ Note: all hex-addresses in the SDK won't have `0x` prefix
14
+
15
+ ### Import (with private key)
16
+
17
+ ```typescript
18
+ import { WeilWallet } from '@weilliptic/wallet-sdk'
19
+
20
+ const wallet = new WeilWallet({
21
+ privateKey: '...', // hex-encoded private key,
22
+ sentinelEndpoint: 'https://sentinel.unweil.me',
23
+ })
24
+ ```
25
+
26
+
27
+ ### Import in DAPP (with wallet integration)
28
+
29
+ ```typescript
30
+ import { WeilWalletConnection } from '@weilliptic/wallet-sdk'
31
+
32
+ const wallet = new WeilWalletConnection({
33
+ walletProvider: window.WeilWallet,
34
+ })
35
+ ```
36
+
37
+ ### Deploy contract
38
+
39
+ ```typescript
40
+ const binPath = './my-applet.wasm'
41
+ const widlPath = './my-applet.widl'
42
+
43
+ const fileToHex = async (filePath: string) => {
44
+ const buffer = await fs.readFile(filePath)
45
+ return buffer.toString('hex')
46
+ }
47
+
48
+ const filesAsHex = Promise.all([binPath, widlPath].map(fileToHex))
49
+ const fileContent = await fs.readFile(filePath)
50
+
51
+ const result = await wallet.contracts.deploy(
52
+ ...filesAsHex,
53
+ {
54
+ author: 'Me'
55
+ }
56
+ )
57
+
58
+ console.log(result)
59
+ ```
60
+
61
+ ### Execute contract
62
+
63
+ ```typescript
64
+
65
+ const yutakaAppletAddress = '' // hex
66
+ const transferResult = await wallet.contracts.execute(
67
+ yutakaAppletAddress,
68
+ 'transfer',
69
+ {
70
+ to_addr: '...', // hex
71
+ amount: 1000,
72
+ },
73
+ )
74
+
75
+ console.log('-------------')
76
+ console.log(transferResult)
77
+
78
+ const balanceResult = await wallet.contracts.execute(
79
+ yutakaAppletAddress,
80
+ 'balance_for',
81
+ {
82
+ addr: '...', // hex
83
+ },
84
+ )
85
+
86
+ console.log('-------------')
87
+ console.log(balanceResult)
88
+ ```
89
+
90
+ ### Execute contract using generated bindings
91
+
92
+ ```typescript
93
+ import { Yutaka } from './bindings.ts'
94
+
95
+ const yutakaAppletAddress = '' // hex
96
+ const yutaka = Yutaka(wallet, appletAddress)
97
+
98
+ const transferResult = await yutaka.transfer({
99
+ to_addr: '...', // hex
100
+ amount: 1000,
101
+ })
102
+ console.log('-------------')
103
+ console.log(transferResult)
104
+
105
+ const balance = await yutaka.balance_for({
106
+ addr: '...' // hex
107
+ })
108
+
109
+ console.log('-------------')
110
+ console.log(balanceResult)
111
+ ```
@@ -0,0 +1,19 @@
1
+ import { WeilWallet } from './wallet';
2
+ import { z } from 'zod';
3
+ export type Contract = {
4
+ [key: string]: (...params: any) => Promise<any>;
5
+ };
6
+ export type ContractFactory = (wallet: WeilWallet, contractAddress: string) => Contract;
7
+ export type Result<T, E> = {
8
+ Ok: T;
9
+ } | {
10
+ Err: E;
11
+ };
12
+ export declare function Ok<T, E>(success: T): Result<T, E>;
13
+ export declare function Err<T, E>(err: E): Result<T, E>;
14
+ export type Option<T> = T | undefined;
15
+ type ExecuteResult = Awaited<ReturnType<WeilWallet['contracts']['execute']>>;
16
+ export type NonStreamExecuteResult = Exclude<ExecuteResult, AsyncIterable<string>>;
17
+ export declare function parseExecutionResultToAnyObject(result: NonStreamExecuteResult): any;
18
+ export declare function parseExecutionResult<T extends z.ZodSchema>(result: NonStreamExecuteResult, schema: T): ReturnType<T["parse"]>;
19
+ export {};
@@ -0,0 +1,86 @@
1
+ import { ZodVoid } from 'zod';
2
+ import { WeilError } from './errors.js';
3
+ import { parseSchema } from './schema.js';
4
+ export function Ok(success) {
5
+ return { Ok: success };
6
+ }
7
+ export function Err(err) {
8
+ return { Err: err };
9
+ }
10
+ export function parseExecutionResultToAnyObject(result) {
11
+ if (result.status !== 'Finalized') {
12
+ if ('txn_result' in result && typeof result.txn_result === 'string') {
13
+ let errorDetails;
14
+ if (typeof result.txn_result === 'string') {
15
+ errorDetails = result.txn_result;
16
+ }
17
+ else {
18
+ const response = JSON.parse(result.txn_result);
19
+ if ('Err' in response) {
20
+ try {
21
+ errorDetails = JSON.stringify(response.Err);
22
+ }
23
+ catch (e) {
24
+ errorDetails = response.Err?.toString();
25
+ }
26
+ }
27
+ }
28
+ if (errorDetails) {
29
+ throw new WeilError(`Transaction resulted in error: ${errorDetails}`, {
30
+ type: 'ExecutionFailure',
31
+ code: 'transaction-error',
32
+ });
33
+ }
34
+ }
35
+ if ('message' in result && typeof result.message === 'string') {
36
+ throw new WeilError(`Transaction resulted in error: ${result.message}`, {
37
+ type: 'ExecutionFailure',
38
+ code: 'transaction-error',
39
+ });
40
+ }
41
+ if ('Error' in result && typeof result.Error === 'string') {
42
+ throw new WeilError(`Transaction resulted in error: ${result.Error}`, {
43
+ type: 'ExecutionFailure',
44
+ code: 'transaction-error',
45
+ });
46
+ }
47
+ throw new WeilError(`Transaction failed with status ${result.status}`, {
48
+ type: 'ExecutionFailure',
49
+ code: 'transaction-failed-status',
50
+ });
51
+ }
52
+ const response = JSON.parse(result.txn_result);
53
+ if (!('Ok' in response)) {
54
+ if ('Err' in response) {
55
+ let errorDetails;
56
+ try {
57
+ errorDetails = JSON.stringify(response.Err);
58
+ }
59
+ catch (e) {
60
+ errorDetails = response.Err?.toString();
61
+ }
62
+ throw new WeilError(`Transaction resulted in error: ${errorDetails}`, {
63
+ type: 'ExecutionFailure',
64
+ code: 'transaction-error',
65
+ });
66
+ }
67
+ else {
68
+ throw new WeilError(`Transaction result doesn't contain either 'Ok' or 'Err' fields`, {
69
+ type: 'ExecutionFailure',
70
+ code: 'transaction-response-format',
71
+ });
72
+ }
73
+ }
74
+ // validate response
75
+ return JSON.parse(response.Ok);
76
+ }
77
+ export function parseExecutionResult(result, schema) {
78
+ // validate response
79
+ const parsedResult = parseExecutionResultToAnyObject(result);
80
+ // for void schema, allow other return types
81
+ if (schema instanceof ZodVoid) {
82
+ return null;
83
+ }
84
+ return parseSchema(schema, parsedResult);
85
+ }
86
+ //# sourceMappingURL=contracts.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contracts.js","sourceRoot":"","sources":["../src/contracts.ts"],"names":[],"mappings":"AACA,OAAO,EAAK,OAAO,EAAE,MAAM,KAAK,CAAA;AAChC,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AACpC,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AAatC,MAAM,UAAU,EAAE,CAAO,OAAU;IACjC,OAAO,EAAE,EAAE,EAAE,OAAO,EAAE,CAAA;AACxB,CAAC;AAED,MAAM,UAAU,GAAG,CAAO,GAAM;IAC9B,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,CAAA;AACrB,CAAC;AAUD,MAAM,UAAU,+BAA+B,CAC7C,MAA8B;IAE9B,IAAI,MAAM,CAAC,MAAM,KAAK,WAAW,EAAE,CAAC;QAClC,IAAI,YAAY,IAAI,MAAM,IAAI,OAAO,MAAM,CAAC,UAAU,KAAK,QAAQ,EAAE,CAAC;YACpE,IAAI,YAAoB,CAAA;YAExB,IAAI,OAAO,MAAM,CAAC,UAAU,KAAK,QAAQ,EAAE,CAAC;gBAC1C,YAAY,GAAG,MAAM,CAAC,UAAU,CAAA;YAClC,CAAC;iBAAM,CAAC;gBACN,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC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C,IAAI,IAAI,QAAQ,CAAC,EAAE,CAAC;QACxB,IAAI,KAAK,IAAI,QAAQ,EAAE,CAAC;YACtB,IAAI,YAAoB,CAAA;YACxB,IAAI,CAAC;gBACH,YAAY,GAAG,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAA;YAC7C,CAAC;YAAC,OAAO,CAAC,EAAE,CAAC;gBACX,YAAY,GAAG,QAAQ,CAAC,GAAG,EAAE,QAAQ,EAAE,CAAA;YACzC,CAAC;YACD,MAAM,IAAI,SAAS,CAAC,kCAAkC,YAAY,EAAE,EAAE;gBACpE,IAAI,EAAE,kBAAkB;gBACxB,IAAI,EAAE,mBAAmB;aAC1B,CAAC,CAAA;QACJ,CAAC;aAAM,CAAC;YACN,MAAM,IAAI,SAAS,CACjB,gEAAgE,EAChE;gBACE,IAAI,EAAE,kBAAkB;gBACxB,IAAI,EAAE,6BAA6B;aACpC,CACF,CAAA;QACH,CAAC;IACH,CAAC;IAED,oBAAoB;IACpB,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAA;AAChC,CAAC;AAED,MAAM,UAAU,oBAAoB,CAClC,MAA8B,EAC9B,MAAS;IAET,oBAAoB;IACpB,MAAM,YAAY,GAAG,+BAA+B,CAAC,MAAM,CAAC,CAAA;IAE5D,4CAA4C;IAC5C,IAAI,MAAM,YAAY,OAAO,EAAE,CAAC;QAC9B,OAAO,IAAI,CAAA;IACb,CAAC;IAED,OAAO,WAAW,CAAC,MAAM,EAAE,YAAY,CAAC,CAAA;AAC1C,CAAC"}
@@ -0,0 +1,11 @@
1
+ export type WeilErrorType = 'Validation' | 'ExecutionFailure' | 'Generic';
2
+ interface WeilErrorOptions {
3
+ type: WeilErrorType;
4
+ code?: string;
5
+ }
6
+ export declare class WeilError extends Error {
7
+ type: WeilErrorType;
8
+ code?: string;
9
+ constructor(message?: string, options?: WeilErrorOptions);
10
+ }
11
+ export {};
package/dist/errors.js ADDED
@@ -0,0 +1,13 @@
1
+ export class WeilError extends Error {
2
+ type;
3
+ code;
4
+ constructor(message = '', options = { type: 'Generic' }) {
5
+ super(message);
6
+ this.type = options.type;
7
+ this.code = options.code;
8
+ this.message = message;
9
+ // Set the prototype explicitly.
10
+ Object.setPrototypeOf(this, WeilError.prototype);
11
+ }
12
+ }
13
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAOA,MAAM,OAAO,SAAU,SAAQ,KAAK;IAClC,IAAI,CAAe;IACnB,IAAI,CAAS;IAEb,YAAY,OAAO,GAAG,EAAE,EAAE,UAA4B,EAAE,IAAI,EAAE,SAAS,EAAE;QACvE,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAA;QACxB,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAA;QACxB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;QAEtB,gCAAgC;QAChC,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,SAAS,CAAC,SAAS,CAAC,CAAA;IAClD,CAAC;CACF"}
@@ -0,0 +1,6 @@
1
+ export * from './wallet';
2
+ export * from './walletConnection';
3
+ export * from './schema';
4
+ export * from './contracts';
5
+ export * from './errors';
6
+ export * from './utils/index';
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ export * from './wallet.js';
2
+ export * from './walletConnection.js';
3
+ export * from './schema.js';
4
+ export * from './contracts.js';
5
+ export * from './errors.js';
6
+ export * from './utils/index.js';
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAA;AACxB,cAAc,oBAAoB,CAAA;AAClC,cAAc,UAAU,CAAA;AACxB,cAAc,aAAa,CAAA;AAC3B,cAAc,UAAU,CAAA;AACxB,cAAc,eAAe,CAAA"}
package/dist/json.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ type JsonValue = string | number | boolean | null | JsonArray | JsonObject;
2
+ interface JsonArray extends Array<JsonValue> {
3
+ }
4
+ export interface JsonObject {
5
+ [key: string]: JsonValue;
6
+ }
7
+ export {};
package/dist/json.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=json.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json.js","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export type PodPolicy = 'all' | 'non-senate';
2
+ export type PodSpecifier = PodPolicy | string | string[];
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=podTypes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"podTypes.js","sourceRoot":"","sources":["../src/podTypes.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ import { PodSpecifier } from '../podTypes';
2
+ export declare const selectPod: (pods: string[], podSpecifier?: PodSpecifier) => string[];
@@ -0,0 +1,25 @@
1
+ import { isNonSenatePodId } from '../utils/index.js';
2
+ export const selectPod = (pods, podSpecifier = 'all') => {
3
+ const nonSenatePods = pods.filter(isNonSenatePodId);
4
+ if (podSpecifier === 'all') {
5
+ return pods;
6
+ }
7
+ else if (podSpecifier === 'non-senate') {
8
+ return nonSenatePods;
9
+ }
10
+ else if (typeof podSpecifier === 'string') {
11
+ if (!pods.includes(podSpecifier)) {
12
+ throw new Error(`Pod not found: ${podSpecifier}`);
13
+ }
14
+ return [podSpecifier];
15
+ }
16
+ else {
17
+ for (const pod of podSpecifier) {
18
+ if (!pods.includes(pod)) {
19
+ throw new Error(`Pod not found: ${pod}`);
20
+ }
21
+ }
22
+ return [...podSpecifier];
23
+ }
24
+ };
25
+ //# sourceMappingURL=selectPod.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"selectPod.js","sourceRoot":"","sources":["../../src/pods/selectPod.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAA;AAEjD,MAAM,CAAC,MAAM,SAAS,GAAG,CACvB,IAAc,EACd,eAA6B,KAAK,EAClC,EAAE;IACF,MAAM,aAAa,GAAG,IAAI,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAA;IAEnD,IAAI,YAAY,KAAK,KAAK,EAAE,CAAC;QAC3B,OAAO,IAAI,CAAA;IACb,CAAC;SAAM,IAAI,YAAY,KAAK,YAAY,EAAE,CAAC;QACzC,OAAO,aAAa,CAAA;IACtB,CAAC;SAAM,IAAI,OAAO,YAAY,KAAK,QAAQ,EAAE,CAAC;QAC5C,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,YAAY,CAAC,EAAE,CAAC;YACjC,MAAM,IAAI,KAAK,CAAC,kBAAkB,YAAY,EAAE,CAAC,CAAA;QACnD,CAAC;QACD,OAAO,CAAC,YAAY,CAAC,CAAA;IACvB,CAAC;SAAM,CAAC;QACN,KAAK,MAAM,GAAG,IAAI,YAAY,EAAE,CAAC;YAC/B,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;gBACxB,MAAM,IAAI,KAAK,CAAC,kBAAkB,GAAG,EAAE,CAAC,CAAA;YAC1C,CAAC;QACH,CAAC;QACD,OAAO,CAAC,GAAG,YAAY,CAAC,CAAA;IAC1B,CAAC;AACH,CAAC,CAAA"}
@@ -0,0 +1,29 @@
1
+ import { z } from 'zod';
2
+ export { infer } from 'zod';
3
+ export declare const Schema: {
4
+ u8: z.ZodNumber;
5
+ u16: z.ZodNumber;
6
+ u32: z.ZodNumber;
7
+ u64: z.ZodNumber;
8
+ uint: z.ZodNumber;
9
+ i8: z.ZodNumber;
10
+ i16: z.ZodNumber;
11
+ i32: z.ZodNumber;
12
+ i64: z.ZodNumber;
13
+ int: z.ZodNumber;
14
+ bool: z.ZodBoolean;
15
+ string: z.ZodString;
16
+ void: z.ZodNull;
17
+ array: typeof z.array;
18
+ args: typeof z.object;
19
+ tuple: typeof z.tuple;
20
+ record: typeof z.record;
21
+ object: typeof z.object;
22
+ result: <T extends z.ZodTypeAny, E extends z.ZodTypeAny>(t: T, e: E) => z.ZodUnion<readonly [z.ZodObject<{
23
+ Ok: T;
24
+ }, z.core.$strip>, z.ZodObject<{
25
+ Err: E;
26
+ }, z.core.$strip>]>;
27
+ option: typeof z.optional;
28
+ };
29
+ export declare function parseSchema<T extends z.ZodSchema>(schema: T, value: any): ReturnType<T["parse"]>;
package/dist/schema.js ADDED
@@ -0,0 +1,48 @@
1
+ import { z, ZodError } from 'zod';
2
+ import { WeilError } from './errors.js';
3
+ export const Schema = {
4
+ u8: z.number(),
5
+ u16: z.number(),
6
+ u32: z.number(),
7
+ // TODO switch to bigint
8
+ u64: z.number(),
9
+ uint: z.number(),
10
+ i8: z.number(),
11
+ i16: z.number(),
12
+ i32: z.number(),
13
+ // TODO switch to bigint
14
+ i64: z.number(),
15
+ int: z.number(),
16
+ bool: z.boolean(),
17
+ string: z.string(),
18
+ void: z.null(),
19
+ array: z.array,
20
+ args: z.object,
21
+ tuple: z.tuple,
22
+ record: z.record,
23
+ object: z.object,
24
+ result: (t, e) => z.union([
25
+ z.object({
26
+ Ok: t,
27
+ }),
28
+ z.object({
29
+ Err: e,
30
+ }),
31
+ ]),
32
+ option: z.optional,
33
+ };
34
+ export function parseSchema(schema, value) {
35
+ try {
36
+ return schema.parse(value);
37
+ }
38
+ catch (error) {
39
+ if (error instanceof ZodError) {
40
+ const message = z.prettifyError(error);
41
+ throw new WeilError(message, {
42
+ type: 'Validation',
43
+ });
44
+ }
45
+ throw new WeilError(`An unexpected error occurred: ${error}`);
46
+ }
47
+ }
48
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,QAAQ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A2B,CAAA;IACtD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,QAAQ,EAAE,CAAC;YAC9B,MAAM,OAAO,GAAG,CAAC,CAAC,aAAa,CAAC,KAAK,CAAC,CAAA;YACtC,MAAM,IAAI,SAAS,CAAC,OAAO,EAAE;gBAC3B,IAAI,EAAE,YAAY;aACnB,CAAC,CAAA;QACJ,CAAC;QAED,MAAM,IAAI,SAAS,CAAC,iCAAiC,KAAK,EAAE,CAAC,CAAA;IAC/D,CAAC;AACH,CAAC"}
@@ -0,0 +1,33 @@
1
+ export interface ExecutionTxPayload {
2
+ type: 'SmartContractExecutor';
3
+ contract_address: string;
4
+ contract_method: string;
5
+ contract_input_bytes: string;
6
+ }
7
+ export interface DeploymentTxPayload {
8
+ type: 'SmartContractCreator';
9
+ contract_body: string;
10
+ smart_contract_header_metadata: {
11
+ contract_address: string;
12
+ name: string;
13
+ contract_body_len: number;
14
+ contract_widl: string;
15
+ contract_config: string | null;
16
+ contract_context: string | null;
17
+ upgrade_contract: boolean;
18
+ author: string | null;
19
+ description: string | null;
20
+ organization: string | null;
21
+ logo: string | null;
22
+ };
23
+ init_args?: string;
24
+ is_audit_log_enabled?: boolean;
25
+ is_outcall_enabled?: boolean;
26
+ }
27
+ export interface SignableTransaction<TxPayload = ExecutionTxPayload | DeploymentTxPayload> {
28
+ nonce: number;
29
+ from_addr: string;
30
+ to_addr: string;
31
+ user_txn: TxPayload;
32
+ }
33
+ export declare function getSignature<TxPayload>(signableTransaction: SignableTransaction<TxPayload>, privateKeyHex: string): string;
@@ -0,0 +1,10 @@
1
+ import { sha256 } from '@noble/hashes/sha256';
2
+ import { secp256k1 } from '@noble/curves/secp256k1';
3
+ import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
4
+ export function getSignature(signableTransaction, privateKeyHex) {
5
+ const messageToSign = JSON.stringify(signableTransaction);
6
+ const messageHash = sha256(messageToSign);
7
+ const signature = secp256k1.sign(messageHash, hexToBytes(privateKeyHex));
8
+ return bytesToHex(signature);
9
+ }
10
+ //# sourceMappingURL=signature.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signature.js","sourceRoot":"","sources":["../../src/transactions/signature.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAA;AAC7C,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACnD,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAA;AAuC5D,MAAM,UAAU,YAAY,CAC1B,mBAAmD,EACnD,aAAqB;IAErB,MAAM,aAAa,GAAG,IAAI,CAAC,SAAS,CAAC,mBAAmB,CAAC,CAAA;IAEzD,MAAM,WAAW,GAAG,MAAM,CAAC,aAAa,CAAC,CAAA;IACzC,MAAM,SAAS,GAAG,SAAS,CAAC,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC,aAAa,CAAC,CAAC,CAAA;IAExE,OAAO,UAAU,CAAC,SAAS,CAAC,CAAA;AAC9B,CAAC"}
@@ -0,0 +1,110 @@
1
+ // global.d.ts
2
+
3
+ type JsonValue = string | number | boolean | null | JsonValue[] | JsonObject
4
+
5
+ export interface JsonObject {
6
+ [key: string]: JsonValue
7
+ }
8
+
9
+ interface DeploymentContractParams {
10
+ name?: string
11
+ body: string
12
+ widl: string
13
+ init_args?: string
14
+ }
15
+
16
+ interface ExecutionContractParams {
17
+ address: string
18
+ method: string
19
+ arguments: JsonObject
20
+ }
21
+
22
+ export interface SubmitTransactionParams {
23
+ toAddress: string
24
+ contract:
25
+ | ({ type: 'SmartContractCreator' } & DeploymentContractParams & {
26
+ pod: string
27
+ })
28
+ | ({ type: 'SmartContractExecutor' } & ExecutionContractParams)
29
+ nonce?: number
30
+ }
31
+
32
+ interface AccountsRequestParams {
33
+ method: 'weil_requestAccounts'
34
+ }
35
+
36
+ interface AccountRequestParams {
37
+ method: 'weil_accounts'
38
+ }
39
+
40
+ interface CheckConnectionRequestParams {
41
+ method: 'check_connection'
42
+ }
43
+
44
+ interface DisconnectRequestParams {
45
+ method: 'wallet_disconnect'
46
+ }
47
+
48
+ interface GetRegistryRequestParams {
49
+ method: 'wallet_getRegistry'
50
+ }
51
+
52
+ interface UpdateRegistryRequestParams {
53
+ method: 'wallet_updateRegistry'
54
+ params: Record<string, Record<string, string>>
55
+ }
56
+
57
+ interface PermissionsRequestParams {
58
+ method: 'wallet_requestPermissions'
59
+ }
60
+
61
+ interface SettingsRequestParams {
62
+ method: 'wallet_getSettings'
63
+ }
64
+
65
+ interface SendTransactionRequestParams {
66
+ method: 'weil_sendTransaction'
67
+ params: SubmitTransactionParams[]
68
+ }
69
+
70
+ interface UnlockWalletRequestParams {
71
+ method: 'wallet_unlock'
72
+ }
73
+
74
+ interface OpenWalletRequestParams {
75
+ method: 'wallet_open'
76
+ }
77
+
78
+ type WalletRequest = (
79
+ params:
80
+ | AccountsRequestParams
81
+ | AccountRequestParams
82
+ | CheckConnectionRequestParams
83
+ | DisconnectRequestParams
84
+ | GetRegistryRequestParams
85
+ | UpdateRegistryRequestParams
86
+ | PermissionsRequestParams
87
+ | SettingsRequestParams
88
+ | SendTransactionRequestParams
89
+ | UnlockWalletRequestParams
90
+ | OpenWalletRequestParams,
91
+ ) => any
92
+
93
+ export type WeilWalletProvider = {
94
+ request: WalletRequest
95
+ on: (eventName: string, callback: any) => void
96
+ off: (eventName: string, callback: any) => void
97
+ onResponse: (requestId: string, callback: any) => void
98
+ offResponse: (requestId: string, callback: any) => void
99
+ isConnected: () => boolean
100
+ isSetUp: () => Promise<boolean>
101
+ isUnlocked: () => Promise<boolean>
102
+ }
103
+
104
+ declare global {
105
+ interface Window {
106
+ WeilWallet: WeilWalletProvider
107
+ }
108
+ }
109
+
110
+ export {}
@@ -0,0 +1,8 @@
1
+ export type TransactionTransport = 'multipart' | 'post';
2
+ export declare const defaultTransport = "multipart";
3
+ export declare const isNonSenatePodId: (podId: string) => boolean;
4
+ export declare const isSenatePodId: (podId: string) => boolean;
5
+ export declare const constructRestEndpoint: (baseUrl: string) => string;
6
+ export declare const constructTransactionEndpoint: (baseUrl: string, contractType: "SmartContractCreator" | "SmartContractExecutor", transport?: TransactionTransport) => string;
7
+ export declare const gzip: (input: string) => Promise<Uint8Array<ArrayBuffer>>;
8
+ export declare const formatPayloadForPrint: (originalPayload: any) => string;
@@ -0,0 +1,45 @@
1
+ import pako from 'pako';
2
+ export const defaultTransport = 'multipart';
3
+ export const isNonSenatePodId = (podId) => podId.startsWith('POD_');
4
+ export const isSenatePodId = (podId) => !isNonSenatePodId(podId);
5
+ export const constructRestEndpoint = (baseUrl) => {
6
+ return [baseUrl, '/rest'].join('');
7
+ };
8
+ const apiRouteMap = {
9
+ SmartContractCreator: '/contracts/upload_smartcontract',
10
+ SmartContractExecutor: '/contracts/execute_smartcontract',
11
+ };
12
+ export const constructTransactionEndpoint = (baseUrl, contractType, transport = defaultTransport) => [
13
+ baseUrl,
14
+ apiRouteMap[contractType],
15
+ transport === 'post' ? '_mobile' : undefined,
16
+ ]
17
+ .filter(Boolean)
18
+ .join('');
19
+ export const gzip = async (input) => {
20
+ // Step 1: Encode the string into a Uint8Array
21
+ const encoder = new TextEncoder();
22
+ const uint8Array = encoder.encode(input);
23
+ return pako.gzip(uint8Array);
24
+ };
25
+ export const formatPayloadForPrint = (originalPayload) => {
26
+ const payload = JSON.parse(JSON.stringify(originalPayload));
27
+ const STRING_CHAR_LIMIT = 512;
28
+ function cleanupForPrint(obj) {
29
+ for (let k in obj) {
30
+ if (typeof obj[k] === 'object') {
31
+ cleanupForPrint(obj[k]);
32
+ }
33
+ else if (typeof obj[k] === 'string' &&
34
+ obj[k].length > STRING_CHAR_LIMIT) {
35
+ obj[k] = [
36
+ obj[k].substring(0, STRING_CHAR_LIMIT / 2 - 2),
37
+ obj[k].substring(obj[k].length - (STRING_CHAR_LIMIT / 2 - 2)),
38
+ ].join(' [ … ] ');
39
+ }
40
+ }
41
+ return obj;
42
+ }
43
+ return JSON.stringify(cleanupForPrint(payload), null, 2);
44
+ };
45
+ //# sourceMappingURL=common.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"common.js","sourceRoot":"","sources":["../../src/utils/common.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAA;AAGvB,MAAM,CAAC,MAAM,gBAAgB,GAAG,WAAW,CAAA;AAE3C,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,KAAa,EAAE,EAAE,CAAC,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC,CAAA;AAC3E,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,KAAa,EAAE,EAAE,CAAC,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAA;AAExE,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,OAAe,EAAE,EAAE;IACvD,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;AACpC,CAAC,CAAA;AAGD,MAAM,WAAW,GAAiC;IAChD,oBAAoB,EAAE,iCAAiC;IACvD,qBAAqB,EAAE,kCAAkC;CAC1D,CAAA;AAED,MAAM,CAAC,MAAM,4BAA4B,GAAG,CAC1C,OAAe,EACf,YAA8D,EAC9D,YAAkC,gBAAgB,EAClD,EAAE,CACF;IACE,OAAO;IACP,WAAW,CAAC,YAAY,CAAC;IACzB,SAAS,KAAK,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS;CAC7C;KACE,MAAM,CAAC,OAAO,CAAC;KACf,IAAI,CAAC,EAAE,CAAC,CAAA;AAEb,MAAM,CAAC,MAAM,IAAI,GAAG,KAAK,EAAE,KAAa,EAAE,EAAE;IAC1C,8CAA8C;IAC9C,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAA;IACjC,MAAM,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;IAExC,OAAO,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;AAC9B,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,eAAoB,EAAE,EAAE;IAC5D,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,eAAe,CAAC,CAAC,CAAA;IAC3D,MAAM,iBAAiB,GAAG,GAAG,CAAA;IAE7B,SAAS,eAAe,CAAC,GAAQ;QAC/B,KAAK,IAAI,CAAC,IAAI,GAAG,EAAE,CAAC;YAClB,IAAI,OAAO,GAAG,CAAC,CAAC,CAAC,KAAK,QAAQ,EAAE,CAAC;gBAC/B,eAAe,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;YACzB,CAAC;iBAAM,IACL,OAAO,GAAG,CAAC,CAAC,CAAC,KAAK,QAAQ;gBAC1B,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,GAAG,iBAAiB,EACjC,CAAC;gBACD,GAAG,CAAC,CAAC,CAAC,GAAG;oBACP,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,EAAE,iBAAiB,GAAG,CAAC,GAAG,CAAC,CAAC;oBAC9C,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,iBAAiB,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;iBAC9D,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;YACnB,CAAC;QACH,CAAC;QAED,OAAO,GAAG,CAAA;IACZ,CAAC;IAED,OAAO,IAAI,CAAC,SAAS,CAAC,eAAe,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,CAAA;AAC1D,CAAC,CAAA"}
@@ -0,0 +1,5 @@
1
+ export declare const decodeContractAddress: (contractAddress: string) => {
2
+ contractAddress: string;
3
+ counter: number;
4
+ };
5
+ export declare const createContractAddress: (publicKey: string, nonce: number, podCounter: number) => string;