@t-0/provider-sdk 1.0.19

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.
package/README.md ADDED
@@ -0,0 +1,24 @@
1
+ ## Usage instructions
2
+ Usage instruction can be found in [NodeJS SDK docs](https://t-0-network.github.io/docs/integration-guidance/sdk-integration/ts-sdk/)
3
+
4
+ ## Development instructions
5
+ 1. Prepare
6
+ ```bash
7
+ git clone git@github.com:t-0-network/provider-sdk-ts.git
8
+ cd provider-sdk-ts
9
+ npm i
10
+ ```
11
+ 2. Copy `.env.example` into `.env` and set PROVIDER_PRIVATE_KEY and NETWORK_PUBLIC_KEY variables
12
+ ```dotenv
13
+ PROVIDER_PRIVATE_KEY=0x....
14
+ NETWORK_PUBLIC_KEY=0x....
15
+
16
+ ```
17
+ 3. Try using network client
18
+ ```bash
19
+ npx tsx examples/update-quote.ts
20
+ ```
21
+ 4. Start dummy service implementation
22
+ ```bash
23
+ npx tsx examples/server.ts
24
+ ```
@@ -0,0 +1,46 @@
1
+ export declare const DEFAULT_ENDPOINT = "https://api.t-0.network";
2
+ export declare function createNetworkClient(signer: string | Buffer | SignerFunction, endpoint?: string): import("@connectrpc/connect").Client<import("@bufbuild/protobuf/codegenv2").GenService<{
3
+ updateQuote: {
4
+ methodKind: "unary";
5
+ input: typeof import("../common/gen/network/network_pb").UpdateQuoteRequestSchema;
6
+ output: typeof import("../common/gen/network/network_pb").UpdateQuoteResponseSchema;
7
+ };
8
+ getPayoutQuote: {
9
+ methodKind: "unary";
10
+ input: typeof import("../common/gen/network/network_pb").GetPayoutQuoteRequestSchema;
11
+ output: typeof import("../common/gen/network/network_pb").GetPayoutQuoteResponseSchema;
12
+ };
13
+ createPayment: {
14
+ methodKind: "unary";
15
+ input: typeof import("../common/gen/network/network_pb").CreatePaymentRequestSchema;
16
+ output: typeof import("../common/gen/network/network_pb").CreatePaymentResponseSchema;
17
+ };
18
+ updatePayout: {
19
+ methodKind: "unary";
20
+ input: typeof import("../common/gen/network/network_pb").UpdatePayoutRequestSchema;
21
+ output: typeof import("../common/gen/network/network_pb").UpdatePayoutResponseSchema;
22
+ };
23
+ createPayIn: {
24
+ methodKind: "unary";
25
+ input: typeof import("../common/gen/network/network_pb").CreatePayInRequestSchema;
26
+ output: typeof import("../common/gen/network/network_pb").CreatePayInResponseSchema;
27
+ };
28
+ getKycData: {
29
+ methodKind: "unary";
30
+ input: typeof import("../common/gen/network/network_pb").GetKycDataRequestSchema;
31
+ output: typeof import("../common/gen/network/network_pb").GetKycDataResponseSchema;
32
+ };
33
+ }>>;
34
+ /**
35
+ * Signature with metadata for particular request
36
+ */
37
+ export interface Signature {
38
+ signature: Buffer;
39
+ publicKey: Buffer;
40
+ }
41
+ /**
42
+ * Signature function for signing requests to T-0 API. Accepts any data in string format and return signature
43
+ * with metadata
44
+ */
45
+ export type SignerFunction = (data: Buffer) => Promise<Signature>;
46
+ export default createNetworkClient;
@@ -0,0 +1,40 @@
1
+ import { createClient } from "@connectrpc/connect";
2
+ import { createConnectTransport } from "@connectrpc/connect-web";
3
+ import { keccak_256 } from "@noble/hashes/sha3";
4
+ import { NetworkService } from "../common/gen/network/network_pb";
5
+ import CreateSigner from "./signer";
6
+ import NetworkHeaders from "../common/headers";
7
+ export const DEFAULT_ENDPOINT = "https://api.t-0.network";
8
+ export function createNetworkClient(signer, endpoint) {
9
+ let customFetch;
10
+ endpoint = endpoint || DEFAULT_ENDPOINT;
11
+ if (typeof signer === "string" || Buffer.isBuffer(signer)) {
12
+ signer = CreateSigner(signer);
13
+ }
14
+ customFetch = async (r, init) => {
15
+ if (!init?.body || !((init.body) instanceof Uint8Array)) {
16
+ throw "unsupported body type";
17
+ }
18
+ const ts = Date.now();
19
+ // 64‑bit little‑endian timestamp
20
+ const tsBuf = Buffer.alloc(8);
21
+ tsBuf.writeBigUInt64LE(BigInt(ts));
22
+ const hash = keccak_256.create()
23
+ .update(init.body)
24
+ .update(tsBuf);
25
+ const hashHex = Buffer.from(hash.digest());
26
+ const sig = await signer(hashHex);
27
+ const headers = new Headers(init?.headers);
28
+ headers.append(NetworkHeaders.Signature, "0x" + sig.signature.toString('hex'));
29
+ headers.append(NetworkHeaders.PublicKey, "0x" + sig.publicKey.toString('hex'));
30
+ headers.append(NetworkHeaders.SignatureTimestamp, ts.toString());
31
+ const modifiedInit = { ...init, headers };
32
+ return fetch(r, modifiedInit);
33
+ };
34
+ const transport = createConnectTransport({
35
+ baseUrl: endpoint || DEFAULT_ENDPOINT,
36
+ fetch: customFetch,
37
+ });
38
+ return createClient(NetworkService, transport);
39
+ }
40
+ export default createNetworkClient;
@@ -0,0 +1,3 @@
1
+ import { Signature } from "./client";
2
+ export declare const CreateSigner: (privateKey: string | Buffer) => (data: Buffer) => Promise<Signature>;
3
+ export default CreateSigner;
@@ -0,0 +1,32 @@
1
+ import * as secp from '@noble/secp256k1';
2
+ export const CreateSigner = (privateKey) => {
3
+ privateKey = parsePrivateKey(privateKey);
4
+ const publicKey = Buffer.from(secp.getPublicKey(privateKey, false));
5
+ return async (data) => {
6
+ // Ensure hash is 32 bytes
7
+ if (data.length !== 32) {
8
+ throw new Error('Message hash must be 32 bytes');
9
+ }
10
+ // Sign the hash
11
+ const signature = await secp.signAsync(data, privateKey);
12
+ return {
13
+ signature: Buffer.from(signature.toBytes()),
14
+ publicKey: publicKey,
15
+ };
16
+ };
17
+ };
18
+ const parsePrivateKey = (privateKey) => {
19
+ if (typeof privateKey == 'string') {
20
+ privateKey = privateKey.replace(/^0x/, '');
21
+ if (!/^[0-9a-fA-F]{64}$/.test(privateKey)) {
22
+ throw new Error('Private key must be 64 hex characters');
23
+ }
24
+ privateKey = Buffer.from(privateKey, 'hex');
25
+ }
26
+ // Validate private key
27
+ if (!secp.utils.isValidPrivateKey(privateKey)) {
28
+ throw new Error('Invalid private key');
29
+ }
30
+ return privateKey;
31
+ };
32
+ export default CreateSigner;
@@ -0,0 +1,26 @@
1
+ import type { GenFile, GenMessage } from "@bufbuild/protobuf/codegenv2";
2
+ import type { Message } from "@bufbuild/protobuf";
3
+ /**
4
+ * Describes the file common/common.proto.
5
+ */
6
+ export declare const file_common_common: GenFile;
7
+ /**
8
+ * Decimal 123.45 equals to unscaled=12345 and exponent=-2 (e.g. unscaled * 10^exponent, 123.45 = 12345 * 10^-2)
9
+ *
10
+ * @generated from message tzero.v1.common.Decimal
11
+ */
12
+ export type Decimal = Message<"tzero.v1.common.Decimal"> & {
13
+ /**
14
+ * @generated from field: int64 unscaled = 10;
15
+ */
16
+ unscaled: bigint;
17
+ /**
18
+ * @generated from field: int32 exponent = 20;
19
+ */
20
+ exponent: number;
21
+ };
22
+ /**
23
+ * Describes the message tzero.v1.common.Decimal.
24
+ * Use `create(DecimalSchema)` to create a new message.
25
+ */
26
+ export declare const DecimalSchema: GenMessage<Decimal>;
@@ -0,0 +1,13 @@
1
+ // @generated by protoc-gen-es v2.6.0 with parameter "target=ts"
2
+ // @generated from file common/common.proto (package tzero.v1.common, syntax proto3)
3
+ /* eslint-disable */
4
+ import { fileDesc, messageDesc } from "@bufbuild/protobuf/codegenv2";
5
+ /**
6
+ * Describes the file common/common.proto.
7
+ */
8
+ export const file_common_common = /*@__PURE__*/ fileDesc("ChNjb21tb24vY29tbW9uLnByb3RvEg90emVyby52MS5jb21tb24iLQoHRGVjaW1hbBIQCgh1bnNjYWxlZBgKIAEoAxIQCghleHBvbmVudBgUIAEoBUKAAQoTY29tLnR6ZXJvLnYxLmNvbW1vbkILQ29tbW9uUHJvdG9QAaICA1RWQ6oCD1R6ZXJvLlYxLkNvbW1vbsoCD1R6ZXJvXFYxXENvbW1vbuICG1R6ZXJvXFYxXENvbW1vblxHUEJNZXRhZGF0YeoCEVR6ZXJvOjpWMTo6Q29tbW9uYgZwcm90bzM");
9
+ /**
10
+ * Describes the message tzero.v1.common.Decimal.
11
+ * Use `create(DecimalSchema)` to create a new message.
12
+ */
13
+ export const DecimalSchema = /*@__PURE__*/ messageDesc(file_common_common, 0);
@@ -0,0 +1,51 @@
1
+ import type { GenFile, GenMessage } from "@bufbuild/protobuf/codegenv2";
2
+ import type { Message } from "@bufbuild/protobuf";
3
+ /**
4
+ * Describes the file common/payment_method.proto.
5
+ */
6
+ export declare const file_common_payment_method: GenFile;
7
+ /**
8
+ * @generated from message tzero.v1.common.PaymentMethod
9
+ */
10
+ export type PaymentMethod = Message<"tzero.v1.common.PaymentMethod"> & {
11
+ /**
12
+ * @generated from oneof tzero.v1.common.PaymentMethod.details
13
+ */
14
+ details: {
15
+ /**
16
+ * @generated from field: tzero.v1.common.SepaPaymentMethod sepa = 10;
17
+ */
18
+ value: SepaPaymentMethod;
19
+ case: "sepa";
20
+ } | {
21
+ case: undefined;
22
+ value?: undefined;
23
+ };
24
+ };
25
+ /**
26
+ * Describes the message tzero.v1.common.PaymentMethod.
27
+ * Use `create(PaymentMethodSchema)` to create a new message.
28
+ */
29
+ export declare const PaymentMethodSchema: GenMessage<PaymentMethod>;
30
+ /**
31
+ * @generated from message tzero.v1.common.SepaPaymentMethod
32
+ */
33
+ export type SepaPaymentMethod = Message<"tzero.v1.common.SepaPaymentMethod"> & {
34
+ /**
35
+ * @generated from field: string iban = 10;
36
+ */
37
+ iban: string;
38
+ /**
39
+ * @generated from field: string payment_reference = 20;
40
+ */
41
+ paymentReference: string;
42
+ /**
43
+ * @generated from field: string name = 30;
44
+ */
45
+ name: string;
46
+ };
47
+ /**
48
+ * Describes the message tzero.v1.common.SepaPaymentMethod.
49
+ * Use `create(SepaPaymentMethodSchema)` to create a new message.
50
+ */
51
+ export declare const SepaPaymentMethodSchema: GenMessage<SepaPaymentMethod>;
@@ -0,0 +1,18 @@
1
+ // @generated by protoc-gen-es v2.6.0 with parameter "target=ts"
2
+ // @generated from file common/payment_method.proto (package tzero.v1.common, syntax proto3)
3
+ /* eslint-disable */
4
+ import { fileDesc, messageDesc } from "@bufbuild/protobuf/codegenv2";
5
+ /**
6
+ * Describes the file common/payment_method.proto.
7
+ */
8
+ export const file_common_payment_method = /*@__PURE__*/ fileDesc("Chtjb21tb24vcGF5bWVudF9tZXRob2QucHJvdG8SD3R6ZXJvLnYxLmNvbW1vbiJOCg1QYXltZW50TWV0aG9kEjIKBHNlcGEYCiABKAsyIi50emVyby52MS5jb21tb24uU2VwYVBheW1lbnRNZXRob2RIAEIJCgdkZXRhaWxzIkoKEVNlcGFQYXltZW50TWV0aG9kEgwKBGliYW4YCiABKAkSGQoRcGF5bWVudF9yZWZlcmVuY2UYFCABKAkSDAoEbmFtZRgeIAEoCUKHAQoTY29tLnR6ZXJvLnYxLmNvbW1vbkISUGF5bWVudE1ldGhvZFByb3RvUAGiAgNUVkOqAg9UemVyby5WMS5Db21tb27KAg9UemVyb1xWMVxDb21tb27iAhtUemVyb1xWMVxDb21tb25cR1BCTWV0YWRhdGHqAhFUemVybzo6VjE6OkNvbW1vbmIGcHJvdG8z");
9
+ /**
10
+ * Describes the message tzero.v1.common.PaymentMethod.
11
+ * Use `create(PaymentMethodSchema)` to create a new message.
12
+ */
13
+ export const PaymentMethodSchema = /*@__PURE__*/ messageDesc(file_common_payment_method, 0);
14
+ /**
15
+ * Describes the message tzero.v1.common.SepaPaymentMethod.
16
+ * Use `create(SepaPaymentMethodSchema)` to create a new message.
17
+ */
18
+ export const SepaPaymentMethodSchema = /*@__PURE__*/ messageDesc(file_common_payment_method, 1);