postchain-client 1.0.2 → 1.1.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.
- package/README.md +30 -49
- package/built/cjs/index.js +2571 -0
- package/built/cjs/index.js.map +1 -0
- package/built/esm/index.js +29927 -0
- package/built/esm/index.js.map +1 -0
- package/built/index.js +21 -3
- package/built/index.js.map +1 -0
- package/built/src/chromia/chromiaClientProvider.js +35 -0
- package/built/src/chromia/chromiaClientProvider.js.map +1 -0
- package/built/src/chromia/errors.js +6 -0
- package/built/src/chromia/errors.js.map +1 -0
- package/built/src/chromia/interfaces.js +2 -0
- package/built/src/chromia/interfaces.js.map +1 -0
- package/built/src/encryption/encryption.js +105 -0
- package/built/src/encryption/encryption.js.map +1 -0
- package/built/src/encryption/errors.js +11 -0
- package/built/src/encryption/errors.js.map +1 -0
- package/built/src/encryption/types.js +2 -0
- package/built/src/encryption/types.js.map +1 -0
- package/built/src/formatter.d.ts +2 -0
- package/built/src/formatter.js +119 -0
- package/built/src/formatter.js.map +1 -0
- package/built/src/gtv/definition.js +20 -0
- package/built/src/gtv/definition.js.map +1 -0
- package/built/src/gtv/index.d.ts +1 -0
- package/built/src/gtv/index.js +11 -0
- package/built/src/gtv/index.js.map +1 -0
- package/built/src/gtv/types.d.ts +2 -3
- package/built/src/gtv/types.js +2 -0
- package/built/src/gtv/types.js.map +1 -0
- package/built/src/gtx/errors.js +26 -0
- package/built/src/gtx/errors.js.map +1 -0
- package/built/src/gtx/gtx.d.ts +1 -1
- package/built/src/gtx/gtx.js +115 -0
- package/built/src/gtx/gtx.js.map +1 -0
- package/built/src/gtx/gtxclient.js +77 -0
- package/built/src/gtx/gtxclient.js.map +1 -0
- package/built/src/gtx/index.js +5 -0
- package/built/src/gtx/index.js.map +1 -0
- package/built/src/gtx/interfaces.js +2 -0
- package/built/src/gtx/interfaces.js.map +1 -0
- package/built/src/gtx/serialization.d.ts +3 -2
- package/built/src/gtx/serialization.js +95 -0
- package/built/src/gtx/serialization.js.map +1 -0
- package/built/src/gtx/types.d.ts +10 -1
- package/built/src/gtx/types.js +2 -0
- package/built/src/gtx/types.js.map +1 -0
- package/built/src/logger.d.ts +2 -1
- package/built/src/logger.js +53 -0
- package/built/src/logger.js.map +1 -0
- package/built/src/merkle/binarytree.d.ts +137 -0
- package/built/src/merkle/binarytree.js +146 -0
- package/built/src/merkle/binarytree.js.map +1 -0
- package/built/src/merkle/binarytreefactory.d.ts +86 -0
- package/built/src/merkle/binarytreefactory.js +235 -0
- package/built/src/merkle/binarytreefactory.js.map +1 -0
- package/built/src/merkle/merkleHelper.js +109 -0
- package/built/src/merkle/merkleHelper.js.map +1 -0
- package/built/src/merkle/merklehashcalculator.d.ts +35 -0
- package/built/src/merkle/merklehashcalculator.js +71 -0
- package/built/src/merkle/merklehashcalculator.js.map +1 -0
- package/built/src/merkle/path.d.ts +155 -0
- package/built/src/merkle/path.js +306 -0
- package/built/src/merkle/path.js.map +1 -0
- package/built/src/merkle/proof/merklehashcarrier.d.ts +17 -0
- package/built/src/merkle/proof/merklehashcarrier.js +23 -0
- package/built/src/merkle/proof/merklehashcarrier.js.map +1 -0
- package/built/src/merkle/proof/merklehashsummaryfactory.d.ts +53 -0
- package/built/src/merkle/proof/merklehashsummaryfactory.js +81 -0
- package/built/src/merkle/proof/merklehashsummaryfactory.js.map +1 -0
- package/built/src/merkle/proof/merkleproof.d.ts +35 -0
- package/built/src/merkle/proof/merkleproof.js +59 -0
- package/built/src/merkle/proof/merkleproof.js.map +1 -0
- package/built/src/merkle/proof/merkleprooftree.d.ts +130 -0
- package/built/src/merkle/proof/merkleprooftree.js +116 -0
- package/built/src/merkle/proof/merkleprooftree.js.map +1 -0
- package/built/src/merkle/proof/merkleprooftreefactory.d.ts +47 -0
- package/built/src/merkle/proof/merkleprooftreefactory.js +121 -0
- package/built/src/merkle/proof/merkleprooftreefactory.js.map +1 -0
- package/built/src/merkle/types.js +2 -0
- package/built/src/merkle/types.js.map +1 -0
- package/built/src/restclient/errors.js +54 -0
- package/built/src/restclient/errors.js.map +1 -0
- package/built/src/restclient/interfaces.d.ts +2 -1
- package/built/src/restclient/interfaces.js +2 -0
- package/built/src/restclient/interfaces.js.map +1 -0
- package/built/src/restclient/restclient.js +309 -0
- package/built/src/restclient/restclient.js.map +1 -0
- package/built/src/restclient/restclientutil.d.ts +4 -0
- package/built/src/restclient/restclientutil.js +137 -0
- package/built/src/restclient/restclientutil.js.map +1 -0
- package/built/src/restclient/types.d.ts +4 -0
- package/built/src/restclient/types.js +13 -0
- package/built/src/restclient/types.js.map +1 -0
- package/built/umd/index.js +29931 -0
- package/built/umd/index.js.map +1 -0
- package/package.json +30 -17
- package/built/index.js.LICENSE.txt +0 -657
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"uBAAqB,iBAAiB;kBAA1B,GAAG;6BACY,iBAAiB;wBAAhC,SAAS;uBACA,eAAe;kBAAxB,GAAG;6BACY,qBAAqB;wBAApC,SAAS;8BACO,6BAA6B;yBAA7C,UAAU;0BACE,cAAc;qBAA1B,MAAM;0BACM,2BAA2B;qBAAvC,MAAM;8BACU,6BAA6B;yBAA7C,UAAU;kCACU,iCAAiC;6BAArD,cAAc;iCACK,qCAAqC;4BAAxD,aAAa"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import * as pc from "../../index";
|
|
11
|
+
import { BlockchainUrlUndefinedException } from "./errors";
|
|
12
|
+
/**
|
|
13
|
+
* Provides postchain clients that can be used to communicate with dapps within the chromia network
|
|
14
|
+
* @param chain0BRID brid of chain0
|
|
15
|
+
* @param rest rest client configured to node running chain0
|
|
16
|
+
*/
|
|
17
|
+
export function chromiaClientProvider(chain0BRID, rest) {
|
|
18
|
+
const chain0Client = pc.gtxClient.createClient(rest, chain0BRID, []);
|
|
19
|
+
return {
|
|
20
|
+
blockchainConnection: function (dappBRID) {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
const queryObject = {
|
|
23
|
+
type: "cm_get_blockchain_api_urls",
|
|
24
|
+
blockchain_rid: dappBRID,
|
|
25
|
+
};
|
|
26
|
+
const baseUrls = yield chain0Client.query(queryObject);
|
|
27
|
+
if (!baseUrls.length) {
|
|
28
|
+
throw new BlockchainUrlUndefinedException(dappBRID);
|
|
29
|
+
}
|
|
30
|
+
return pc.restClient.createRestClient(baseUrls, dappBRID);
|
|
31
|
+
});
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=chromiaClientProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chromiaClientProvider.js","sourceRoot":"","sources":["../../../src/chromia/chromiaClientProvider.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,KAAK,EAAE,MAAM,aAAa,CAAC;AAIlC,OAAO,EAAE,+BAA+B,EAAE,MAAM,UAAU,CAAC;AAE3D;;;;GAIG;AAEH,MAAM,UAAU,qBAAqB,CACnC,UAAkB,EAClB,IAAgB;IAEhB,MAAM,YAAY,GAAG,EAAE,CAAC,SAAS,CAAC,YAAY,CAAC,IAAI,EAAE,UAAU,EAAE,EAAE,CAAC,CAAC;IAErE,OAAO;QACL,oBAAoB,EAAE,UACpB,QAAgB;;gBAEhB,MAAM,WAAW,GAAgB;oBAC/B,IAAI,EAAE,4BAA4B;oBAClC,cAAc,EAAE,QAAQ;iBACzB,CAAC;gBAEF,MAAM,QAAQ,GAAa,MAAM,YAAY,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;gBACjE,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE;oBACpB,MAAM,IAAI,+BAA+B,CAAC,QAAQ,CAAC,CAAC;iBACrD;gBACD,OAAO,EAAE,CAAC,UAAU,CAAC,gBAAgB,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;YAC5D,CAAC;SAAA;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../src/chromia/errors.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,+BAAgC,SAAQ,KAAK;IACxD,YAAY,IAAY;QACtB,KAAK,CAAC,qDAAqD,IAAI,EAAE,CAAC,CAAC;IACrE,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../../src/chromia/interfaces.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import * as crypto from "crypto";
|
|
3
|
+
import * as secp256k1 from "secp256k1";
|
|
4
|
+
import { ensureBuffer } from "../formatter";
|
|
5
|
+
import { MissingPrivKeyArgumentException, PrivKeyFormatException, } from "./errors";
|
|
6
|
+
export function createPublicKey(privKey) {
|
|
7
|
+
validatePrivKeyFormat(privKey);
|
|
8
|
+
return Buffer.from(secp256k1.publicKeyCreate(privKey, true).buffer);
|
|
9
|
+
}
|
|
10
|
+
export function randomBytes(size) {
|
|
11
|
+
return crypto.randomBytes(size);
|
|
12
|
+
}
|
|
13
|
+
export function sha256(buffer) {
|
|
14
|
+
return crypto.createHash("sha256").update(buffer).digest();
|
|
15
|
+
}
|
|
16
|
+
export const hash256 = sha256;
|
|
17
|
+
export function hashConcat(items) {
|
|
18
|
+
return hash256(Buffer.concat(items));
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* @param content the content that the signature signs. It will be digested before validating.
|
|
22
|
+
* @param pubKey The pubKey to validate the signature with
|
|
23
|
+
* @param signature the signature to validate
|
|
24
|
+
*
|
|
25
|
+
* @return true if signature ok, false otherwise
|
|
26
|
+
*/
|
|
27
|
+
export function checkSignature(content, pubKey, signature) {
|
|
28
|
+
const digest = hash256(content);
|
|
29
|
+
return checkDigestSignature(digest, pubKey, signature);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* @param digest the signed digest. It will not be digested before validating.
|
|
33
|
+
* @param pubKey The pubKey to validate the signature with
|
|
34
|
+
* @param signature the signature to validate
|
|
35
|
+
*
|
|
36
|
+
* @return true if signature ok, false otherwise
|
|
37
|
+
*/
|
|
38
|
+
export function checkDigestSignature(digest, pubKey, signature) {
|
|
39
|
+
return secp256k1.ecdsaVerify(signature, digest, pubKey);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* @param content to sign. It will be digested before signing.
|
|
43
|
+
* @param privKey The private key to sign the content with
|
|
44
|
+
*
|
|
45
|
+
* @return the signature
|
|
46
|
+
*/
|
|
47
|
+
export function sign(content, privKey) {
|
|
48
|
+
validatePrivKeyFormat(privKey);
|
|
49
|
+
const digestBuffer = sha256(content);
|
|
50
|
+
return signDigest(digestBuffer, privKey);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* @param digestBuffer to sign. It will not be digested before signing.
|
|
54
|
+
* @param privKey The private key to sign the digest with
|
|
55
|
+
*
|
|
56
|
+
* @return the signature
|
|
57
|
+
*/
|
|
58
|
+
export function signDigest(digestBuffer, privKey) {
|
|
59
|
+
return Buffer.from(secp256k1.ecdsaSign(digestBuffer, privKey).signature);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Creates a key pair (which usually represents one user)
|
|
63
|
+
* @param privKey to create key pair based on
|
|
64
|
+
* @returns {{pubKey: Buffer, privKey: Buffer}}
|
|
65
|
+
*/
|
|
66
|
+
export function makeKeyPair(privKey) {
|
|
67
|
+
let pubKey;
|
|
68
|
+
if (privKey) {
|
|
69
|
+
privKey = ensureBuffer(privKey);
|
|
70
|
+
pubKey = createPublicKey(privKey);
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
do {
|
|
74
|
+
privKey = randomBytes(32);
|
|
75
|
+
} while (!secp256k1.privateKeyVerify(privKey));
|
|
76
|
+
pubKey = Buffer.from(secp256k1.publicKeyCreate(privKey).buffer);
|
|
77
|
+
}
|
|
78
|
+
return { pubKey, privKey };
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Generates a 16bytes TUID (Text unique ID) (a 32characters long string)
|
|
82
|
+
* @returns string
|
|
83
|
+
*/
|
|
84
|
+
export function makeTuid() {
|
|
85
|
+
return randomBytes(16).toString("hex");
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Verify that keypair is correct. Providing the private key, this function returns its associated public key
|
|
89
|
+
* @param privKey: Buffer
|
|
90
|
+
* @returns {{pubKey: Buffer, privKey: Buffer}}
|
|
91
|
+
*/
|
|
92
|
+
export function verifyKeyPair(privKey) {
|
|
93
|
+
validatePrivKeyFormat(privKey);
|
|
94
|
+
const pubKey = Buffer.from(secp256k1.publicKeyCreate(privKey).buffer);
|
|
95
|
+
return { pubKey, privKey };
|
|
96
|
+
}
|
|
97
|
+
function validatePrivKeyFormat(privKey) {
|
|
98
|
+
if (!privKey) {
|
|
99
|
+
throw new MissingPrivKeyArgumentException();
|
|
100
|
+
}
|
|
101
|
+
if (!Buffer.isBuffer(privKey) || privKey.length !== 32) {
|
|
102
|
+
throw new PrivKeyFormatException(privKey);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=encryption.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encryption.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AAO,CAAC,CAAC,MAAM,CAAC,CAAC;IACtE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;AAC7B,CAAC;AAED,SAAS,qBAAqB,CAAC,OAAe;IAC5C,IAAI,CAAC,OAAO,EAAE;QACZ,MAAM,IAAI,+BAA+B,EAAE,CAAC;KAC7C;IACD,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,MAAM,KAAK,EAAE,EAAE;QACtD,MAAM,IAAI,sBAAsB,CAAC,OAAO,CAAC,CAAC;KAC3C;AACH,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export class PrivKeyFormatException extends Error {
|
|
2
|
+
constructor(privKey) {
|
|
3
|
+
super(`Invalid key length. Expected 32, but got ${privKey.length}`);
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
export class MissingPrivKeyArgumentException extends Error {
|
|
7
|
+
constructor() {
|
|
8
|
+
super(`Missing argument privKey`);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../src/encryption/errors.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,sBAAuB,SAAQ,KAAK;IAC/C,YAAY,OAAe;QACzB,KAAK,CAAC,4CAA4C,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IACtE,CAAC;CACF;AAED,MAAM,OAAO,+BAAgC,SAAQ,KAAK;IACxD;QACE,KAAK,CAAC,0BAA0B,CAAC,CAAC;IACpC,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/encryption/types.ts"],"names":[],"mappings":""}
|
package/built/src/formatter.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { GTX, RawGtx } from "./gtx/types";
|
|
3
|
+
import { QueryObject, QueryObjectGTV } from "./restclient/types";
|
|
3
4
|
export declare function pgBytes(buffer: Buffer): string;
|
|
4
5
|
/**
|
|
5
6
|
* Converts hex string to Buffer
|
|
@@ -13,6 +14,7 @@ export declare function toBuffer(key: string): Buffer;
|
|
|
13
14
|
* @returns {string}
|
|
14
15
|
*/
|
|
15
16
|
export declare function toString(buffer: Buffer): string;
|
|
17
|
+
export declare function toQueryObjectGTV(object: QueryObject): QueryObjectGTV;
|
|
16
18
|
export declare class PgBytesInputException extends Error {
|
|
17
19
|
constructor(buffer: Buffer);
|
|
18
20
|
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
export function pgBytes(buffer) {
|
|
2
|
+
if (!Buffer.isBuffer(buffer)) {
|
|
3
|
+
throw new PgBytesInputException(buffer);
|
|
4
|
+
}
|
|
5
|
+
return "\\x" + buffer.toString("hex");
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Converts hex string to Buffer
|
|
9
|
+
* @param key: string
|
|
10
|
+
* @returns {Buffer}
|
|
11
|
+
*/
|
|
12
|
+
export function toBuffer(key) {
|
|
13
|
+
return Buffer.from(key, "hex");
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Converts Buffer to hex string
|
|
17
|
+
* @param buffer: Buffer
|
|
18
|
+
* @returns {string}
|
|
19
|
+
*/
|
|
20
|
+
export function toString(buffer) {
|
|
21
|
+
return buffer.toString("hex");
|
|
22
|
+
}
|
|
23
|
+
export function toQueryObjectGTV(object) {
|
|
24
|
+
const objectCopy = Object.assign({}, object);
|
|
25
|
+
const name = objectCopy.type;
|
|
26
|
+
delete objectCopy.type;
|
|
27
|
+
return [name, objectCopy];
|
|
28
|
+
}
|
|
29
|
+
export class PgBytesInputException extends Error {
|
|
30
|
+
constructor(buffer) {
|
|
31
|
+
super(`util.pgBytes expects a buffer, but got ${typeof buffer}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export function ensureBuffer(value) {
|
|
35
|
+
if (value instanceof Buffer) {
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
return toBuffer(value);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export function checkGtvType(value) {
|
|
43
|
+
try {
|
|
44
|
+
if (value == null) {
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
if (Buffer.isBuffer(value)) {
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
if (typeof value === "string") {
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
if (typeof value === "number") {
|
|
54
|
+
if (!Number.isInteger(value)) {
|
|
55
|
+
throw Error("User error: Only integers are supported");
|
|
56
|
+
}
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
if (typeof value === "bigint") {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
if (value.constructor === Array) {
|
|
63
|
+
value.map((item) => checkGtvType(item));
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
if (typeof value === "object") {
|
|
67
|
+
Object.keys(value).map(function (key) {
|
|
68
|
+
checkGtvType(value[key]);
|
|
69
|
+
});
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
throw new Error(`Failed to check type: ${error}`);
|
|
75
|
+
}
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
export function rawGtxToGtx(rawGtx) {
|
|
79
|
+
const rawGtxBody = rawGtx[0];
|
|
80
|
+
const signatures = rawGtx[1];
|
|
81
|
+
const gtxBody = {
|
|
82
|
+
blockchainRID: rawGtxBody[0],
|
|
83
|
+
operations: rawGtxBody[1].map((operation) => ({
|
|
84
|
+
opName: operation[0],
|
|
85
|
+
args: operation[1],
|
|
86
|
+
})),
|
|
87
|
+
signers: rawGtxBody[2],
|
|
88
|
+
};
|
|
89
|
+
return {
|
|
90
|
+
blockchainRID: gtxBody.blockchainRID,
|
|
91
|
+
operations: gtxBody.operations,
|
|
92
|
+
signers: gtxBody.signers,
|
|
93
|
+
signatures,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
export function checkGtxType(value) {
|
|
97
|
+
try {
|
|
98
|
+
rawGtxToGtx(value);
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
export function removeDuplicateSigners(signers) {
|
|
106
|
+
const signersAsString = [];
|
|
107
|
+
signers.forEach((item) => {
|
|
108
|
+
const itemAsString = item.toString("hex");
|
|
109
|
+
if (!signersAsString.includes(itemAsString)) {
|
|
110
|
+
signersAsString.push(itemAsString);
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
const result = [];
|
|
114
|
+
signersAsString.forEach((item) => {
|
|
115
|
+
result.push(Buffer.from(item, "hex"));
|
|
116
|
+
});
|
|
117
|
+
return result;
|
|
118
|
+
}
|
|
119
|
+
//# sourceMappingURL=formatter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatter.js","sourceRoot":"","sources":["../../src/formatter.ts"],"names":[],"mappings":"AAGA,MAAM,UAAU,OAAO,CAAC,MAAc;IACpC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE;QAC5B,MAAM,IAAI,qBAAqB,CAAC,MAAM,CAAC,CAAC;KACzC;IACD,OAAO,KAAK,GAAG,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;AACxC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,QAAQ,CAAC,GAAW;IAClC,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;AACjC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,QAAQ,CAAC,MAAc;IACrC,OAAO,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;AA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AO,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;QACvB,MAAM,YAAY,GAAG,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QAC1C,IAAI,CAAC,eAAe,CAAC,QAAQ,CAAC,YAAY,CAAC,EAAE;YAC3C,eAAe,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;SACpC;IACH,CAAC,CAAC,CAAC;IACH,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,eAAe,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;QAC/B,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;IACxC,CAAC,CAAC,CAAC;IACH,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
2
|
+
const asn = require("asn1.js/lib/asn1");
|
|
3
|
+
export const ASNDictPair = asn.define("DictPair", function () {
|
|
4
|
+
this.seq().obj(this.key("name").utf8str(), this.key("value").use(rawGTV));
|
|
5
|
+
});
|
|
6
|
+
export const rawGTV = asn.define("GtvValue", function () {
|
|
7
|
+
this.choice({
|
|
8
|
+
null: this.explicit(0).null_(),
|
|
9
|
+
byteArray: this.explicit(1).octstr(),
|
|
10
|
+
string: this.explicit(2).utf8str(),
|
|
11
|
+
integer: this.explicit(3).int(),
|
|
12
|
+
dict: this.explicit(4).seqof(ASNDictPair),
|
|
13
|
+
array: this.explicit(5).seqof(rawGTV),
|
|
14
|
+
bigInteger: this.explicit(6).int(),
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
export const ASNBuffer = asn.define("Buffer", function () {
|
|
18
|
+
this.octstr();
|
|
19
|
+
});
|
|
20
|
+
//# sourceMappingURL=definition.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"definition.js","sourceRoot":"","sources":["../../../src/gtv/defin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 -0,0 +1,11 @@
|
|
|
1
|
+
import { MerkleHashCalculator, CryptoSystem, } from "../merkle/merklehashcalculator";
|
|
2
|
+
const theMerkleHashCalculator = new MerkleHashCalculator(new CryptoSystem());
|
|
3
|
+
import { merkleHashSummary } from "../merkle/proof/merkleproof";
|
|
4
|
+
import * as serialization from "../gtx/serialization";
|
|
5
|
+
const gtvHash = (obj) => {
|
|
6
|
+
return merkleHashSummary(obj, theMerkleHashCalculator).merkleHash;
|
|
7
|
+
};
|
|
8
|
+
const encode = serialization.encodeValue;
|
|
9
|
+
const decode = serialization.decodeValue;
|
|
10
|
+
export { encode, decode, gtvHash };
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/gtv/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,YAAY,GACb,MAAM,gCAAgC,CAAC;AACxC,MAAM,uBAAuB,GAAG,IAAI,oBAAoB,CAAC,IAAI,YAAY,EAAE,CAAC,CAAC;AAC7E,OAAO,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,KAAK,aAAa,MAAM,sBAAsB,CAAC;AAEtD,MAAM,OAAO,GAAG,CAAC,GAAQ,EAAE,EAAE;IAC3B,OAAO,iBAAiB,CAAC,GAAG,EAAE,uBAAuB,CAAC,CAAC,UAAU,CAAC;AACpE,CAAC,CAAC;AACF,MAAM,MAAM,GAAG,aAAa,CAAC,WAAW,CAAC;AACzC,MAAM,MAAM,GAAG,aAAa,CAAC,WAAW,CAAC;AAEzC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC"}
|
package/built/src/gtv/types.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
export type RawGtv = null | Buffer | string | number | DictPair | Array<RawGtv> | bigint;
|
|
2
|
+
export type RawGtv = null | boolean | Buffer | string | number | DictPair | Array<RawGtv> | bigint;
|
|
3
3
|
export type DictPair = {
|
|
4
|
-
|
|
5
|
-
value: RawGtv;
|
|
4
|
+
[x: string]: RawGtv;
|
|
6
5
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/gtv/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export class UnexpectedArgumentTypeError extends Error {
|
|
2
|
+
constructor(typedArg) {
|
|
3
|
+
super(`Cannot parse typedArg ${JSON.stringify(typedArg)}. Unknown type ${typedArg.type}`);
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
export class MissingGtxException extends Error {
|
|
7
|
+
constructor() {
|
|
8
|
+
super(`Missing instance of gtx protocol (used for communicating with postchain) to add operation to`);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export class AlreadySignedTransactionException extends Error {
|
|
12
|
+
constructor(operation) {
|
|
13
|
+
super(`Cannot add ${operation} calls to an already signed gtx`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export class NumberOfSignersAndSignaturesException extends Error {
|
|
17
|
+
constructor() {
|
|
18
|
+
super(`Not matching number of signers and signatures`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export class MissingSignerException extends Error {
|
|
22
|
+
constructor() {
|
|
23
|
+
super(`No such signer, remember to call addSignerToGtx() before adding a signature`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../src/gtx/errors.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,2BAA4B,SAAQ,KAAK;IACpD,YAAY,QAAa;QACvB,KAAK,CAAC,yBAAyB,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,kBAAkB,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;IAC5F,CAAC;CACF;AAED,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IAC5C;QACE,KAAK,CACH,8FAA8F,CAC/F,CAAC;IACJ,CAAC;CACF;AAED,MAAM,OAAO,iCAAkC,SAAQ,KAAK;IAC1D,YAAY,SAAiB;QAC3B,KAAK,CAAC,cAAc,SAAS,iCAAiC,CAAC,CAAC;IAClE,CAAC;CACF;AAED,MAAM,OAAO,qCAAsC,SAAQ,KAAK;IAC9D;QACE,KAAK,CAAC,+CAA+C,CAAC,CAAC;IACzD,CAAC;CACF;AAED,MAAM,OAAO,sBAAuB,SAAQ,KAAK;IAC/C;QACE,KAAK,CACH,6EAA6E,CAC9E,CAAC;IACJ,CAAC;CACF"}
|
package/built/src/gtx/gtx.d.ts
CHANGED
|
@@ -24,7 +24,7 @@ export declare function gtvTxBody(gtx: GTX): RawGtxBody;
|
|
|
24
24
|
* for situations where you don't have to ask someone else to sign.
|
|
25
25
|
*/
|
|
26
26
|
export declare function sign(privKey: Buffer, pubKey: Buffer, gtx: any): void;
|
|
27
|
-
export declare function signRawTransaction(
|
|
27
|
+
export declare function signRawTransaction(_keyPair: Buffer, _rawTransaction: Buffer): void;
|
|
28
28
|
/**
|
|
29
29
|
* Adds a signature to the gtx
|
|
30
30
|
*/
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import * as gtv from "../gtv";
|
|
2
|
+
import { signDigest, checkDigestSignature } from "../encryption/encryption";
|
|
3
|
+
import { AlreadySignedTransactionException, MissingGtxException, MissingSignerException, NumberOfSignersAndSignaturesException, } from "./errors";
|
|
4
|
+
import { encode as gtxEncode, decode as gtxDecode } from "./index";
|
|
5
|
+
export function emptyGtx(blockchainRID) {
|
|
6
|
+
return { blockchainRID: blockchainRID, operations: [], signers: [] };
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Adds a function call to a GTX. Creates a new GTX if none specified.
|
|
10
|
+
* This function will throw Error if gtx is already signed
|
|
11
|
+
* @param opName the name of the function to call
|
|
12
|
+
* @param args the array of arguments of the function call. If no args, this must be an empty array
|
|
13
|
+
* @param gtx the function call will be added to this gtx
|
|
14
|
+
* @returns the gtx
|
|
15
|
+
* @throws if gtx is null or if gtx is already signed
|
|
16
|
+
*/
|
|
17
|
+
export function addTransactionToGtx(opName, args, gtx) {
|
|
18
|
+
if (gtx == null) {
|
|
19
|
+
throw new MissingGtxException();
|
|
20
|
+
}
|
|
21
|
+
if (gtx.signatures) {
|
|
22
|
+
throw new AlreadySignedTransactionException("function");
|
|
23
|
+
}
|
|
24
|
+
gtx.operations.push({ opName: opName, args: args });
|
|
25
|
+
return gtx;
|
|
26
|
+
}
|
|
27
|
+
export function addSignerToGtx(signer, gtx) {
|
|
28
|
+
if (gtx.signatures) {
|
|
29
|
+
throw new AlreadySignedTransactionException("signer");
|
|
30
|
+
}
|
|
31
|
+
gtx.signers.push(signer);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Serializes the gtx for signing
|
|
35
|
+
* @param gtx the gtx to serialize
|
|
36
|
+
*/
|
|
37
|
+
export function getDigestToSign(gtx) {
|
|
38
|
+
return gtv.gtvHash(gtvTxBody(gtx));
|
|
39
|
+
}
|
|
40
|
+
export function gtvTxBody(gtx) {
|
|
41
|
+
return [
|
|
42
|
+
gtx.blockchainRID,
|
|
43
|
+
gtx.operations.map((op) => [op.opName, op.args]),
|
|
44
|
+
gtx.signers,
|
|
45
|
+
];
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Signs the gtx with the provided privKey. This is a convenience function
|
|
49
|
+
* for situations where you don't have to ask someone else to sign.
|
|
50
|
+
*/
|
|
51
|
+
export function sign(privKey, pubKey, gtx) {
|
|
52
|
+
const digestToSign = getDigestToSign(gtx);
|
|
53
|
+
const signature = signDigest(digestToSign, privKey);
|
|
54
|
+
addSignature(pubKey, signature, gtx);
|
|
55
|
+
}
|
|
56
|
+
export function signRawTransaction(_keyPair, _rawTransaction) {
|
|
57
|
+
throw Error("TODO");
|
|
58
|
+
//TODO
|
|
59
|
+
//const gtx = module.exports.deserialize(rawTransaction);
|
|
60
|
+
//module.exports.sign(keyPair.privKey, keyPair.pubKey, gtx);
|
|
61
|
+
// return module.exports.serialize(gtx)
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Adds a signature to the gtx
|
|
65
|
+
*/
|
|
66
|
+
export function addSignature(pubKeyBuffer, signatureBuffer, gtx) {
|
|
67
|
+
if (!gtx.signatures) {
|
|
68
|
+
gtx.signatures = Array(gtx.signers.length).fill(Buffer.alloc(64));
|
|
69
|
+
}
|
|
70
|
+
if (gtx.signers.length !== gtx.signatures.length) {
|
|
71
|
+
throw new NumberOfSignersAndSignaturesException();
|
|
72
|
+
}
|
|
73
|
+
const signerIndex = gtx.signers.findIndex((signer) => pubKeyBuffer.equals(signer));
|
|
74
|
+
if (signerIndex === -1) {
|
|
75
|
+
throw new MissingSignerException();
|
|
76
|
+
}
|
|
77
|
+
gtx.signatures[signerIndex] = signatureBuffer;
|
|
78
|
+
}
|
|
79
|
+
export function serialize(gtx) {
|
|
80
|
+
if (!gtx.signatures) {
|
|
81
|
+
// TODO
|
|
82
|
+
// The gtx is not signed, but we must include
|
|
83
|
+
// the signatures attribute, so let's add that.
|
|
84
|
+
gtx.signatures = [];
|
|
85
|
+
}
|
|
86
|
+
return gtxEncode([gtvTxBody(gtx), gtx.signatures]);
|
|
87
|
+
}
|
|
88
|
+
export function deserialize(gtxBytes) {
|
|
89
|
+
const deserializedTx = gtxDecode(gtxBytes);
|
|
90
|
+
const body = deserializedTx[0];
|
|
91
|
+
const gtvTxBody = {
|
|
92
|
+
blockchainRID: body[0],
|
|
93
|
+
operations: body[1].map((operation) => ({
|
|
94
|
+
opName: operation[0],
|
|
95
|
+
args: operation[1],
|
|
96
|
+
})),
|
|
97
|
+
signers: body[2],
|
|
98
|
+
};
|
|
99
|
+
const signatures = deserializedTx[1];
|
|
100
|
+
return {
|
|
101
|
+
blockchainRID: gtvTxBody.blockchainRID,
|
|
102
|
+
operations: gtvTxBody.operations,
|
|
103
|
+
signers: gtvTxBody.signers,
|
|
104
|
+
signatures,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
export function checkGTXSignatures(txHash, gtx) {
|
|
108
|
+
for (const i in gtx.signers) {
|
|
109
|
+
const signValid = checkDigestSignature(txHash, gtx.signers[i], gtx.signatures[i]);
|
|
110
|
+
if (!signValid)
|
|
111
|
+
return signValid;
|
|
112
|
+
}
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
//# sourceMappingURL=gtx.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gtx.js","sourceRoot":"","sources":["../../../src/gtx/gtx.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,GAAG,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAE,UAAU,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAC5E,OAAO,EACL,iCAAiC,EACjC,mBAAmB,EACnB,sBAAsB,EACtB,qCAAqC,GACtC,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,MAAM,IAAI,SAAS,EAAE,MAAM,IAAI,SAAS,EAAE,MAAM,SAAS,CAAC;AAEnE,MAAM,UAAU,QAAQ,CAAC,aAAqB;IAC5C,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,UAAU,EAAE,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC;AACvE,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,mBAAmB,CACjC,MAAc,EACd,IAAc,EACd,GAAQ;IAER,IAAI,GAAG,IAAI,IAAI,EAAE;QACf,MAAM,IAAI,mBAAmB,EAAE,CAAC;KACjC;IACD,IAAI,GAAG,CAAC,UAAU,EAAE;QAClB,MAAM,IAAI,iCAAiC,CAAC,UAAU,CAAC,CAAC;KACzD;IACD,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,IAA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SACnB,CAAC,CAAC;QACH,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;KACjB,CAAC;IACF,MAAM,UAAU,GAAG,cAAc,CAAC,CAAC,CAAC,CAAC;IAErC,OAAO;QACL,aAAa,EAAE,SAAS,CAAC,aAAa;QACtC,UAAU,EAAE,SAAS,CAAC,UAAU;QAChC,OAAO,EAAE,SAAS,CAAC,OAAO;QAC1B,UAAU;KACX,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,MAAc,EAAE,GAAQ;IACzD,KAAK,MAAM,CAAC,IAAI,GAAG,CAAC,OAAO,EAAE;QAC3B,MAAM,SAAS,GAAG,oBAAoB,CACpC,MAAM,EACN,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,EACd,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAClB,CAAC;QACF,IAAI,CAAC,SAAS;YAAE,OAAO,SAAS,CAAC;KAClC;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import * as gtxTool from "./gtx";
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
4
|
+
const secp256k1 = require("secp256k1");
|
|
5
|
+
import * as logger from "../logger";
|
|
6
|
+
import { removeDuplicateSigners } from "../formatter";
|
|
7
|
+
export function createClient(restApiClient, blockchainRID, functionNames) {
|
|
8
|
+
functionNames.push("message");
|
|
9
|
+
function transaction(gtx) {
|
|
10
|
+
return {
|
|
11
|
+
gtx,
|
|
12
|
+
sign: function (privKey, pubKey) {
|
|
13
|
+
let pub = pubKey;
|
|
14
|
+
if (!pub) {
|
|
15
|
+
logger.debug(`pubKey not provided, will instead be generated using secp256k1`);
|
|
16
|
+
pub = Buffer.from(secp256k1.publicKeyCreate(privKey));
|
|
17
|
+
}
|
|
18
|
+
logger.debug(`signing transaction with privKey: ${privKey}, pubKey: ${pub}`);
|
|
19
|
+
gtxTool.sign(privKey, pub, this.gtx);
|
|
20
|
+
},
|
|
21
|
+
getTxRID: function () {
|
|
22
|
+
return this.getDigestToSign();
|
|
23
|
+
},
|
|
24
|
+
getDigestToSign: function () {
|
|
25
|
+
return gtxTool.getDigestToSign(this.gtx);
|
|
26
|
+
},
|
|
27
|
+
addSignature: function (pubKey, signature) {
|
|
28
|
+
gtxTool.addSignature(pubKey, signature, this.gtx);
|
|
29
|
+
},
|
|
30
|
+
// raw call
|
|
31
|
+
addOperation: function (name, ...args) {
|
|
32
|
+
gtxTool.addTransactionToGtx(name, args, this.gtx);
|
|
33
|
+
},
|
|
34
|
+
postAndWaitConfirmation() {
|
|
35
|
+
return restApiClient.postAndWaitConfirmation(gtxTool.serialize(this.gtx), this.getTxRID());
|
|
36
|
+
},
|
|
37
|
+
send: function (callback) {
|
|
38
|
+
const gtxBytes = gtxTool.serialize(this.gtx);
|
|
39
|
+
restApiClient.postTransaction(gtxBytes, callback);
|
|
40
|
+
this.gtx = null;
|
|
41
|
+
this.gtxBytes = gtxBytes;
|
|
42
|
+
},
|
|
43
|
+
encode: function () {
|
|
44
|
+
return gtxTool.serialize(this.gtx);
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function addFunctions(req) {
|
|
49
|
+
functionNames.forEach((functionName) => {
|
|
50
|
+
req[functionName] = function (...args) {
|
|
51
|
+
gtxTool.addTransactionToGtx(functionName, args, this.gtx);
|
|
52
|
+
};
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
const client = {
|
|
56
|
+
newTransaction: function (signers) {
|
|
57
|
+
signers = removeDuplicateSigners(signers);
|
|
58
|
+
const newGtx = gtxTool.emptyGtx(Buffer.from(blockchainRID, "hex"));
|
|
59
|
+
signers.forEach((signer) => gtxTool.addSignerToGtx(signer, newGtx));
|
|
60
|
+
const req = transaction(newGtx);
|
|
61
|
+
addFunctions(req);
|
|
62
|
+
return req;
|
|
63
|
+
},
|
|
64
|
+
transactionFromRawTransaction: function (rawTransaction) {
|
|
65
|
+
const gtx = gtxTool.deserialize(rawTransaction);
|
|
66
|
+
logger.debug(`Output from deserializing a raw transaction: ${JSON.stringify(gtx)}`);
|
|
67
|
+
const req = transaction(gtx);
|
|
68
|
+
addFunctions(req);
|
|
69
|
+
return req;
|
|
70
|
+
},
|
|
71
|
+
query: function (queryObject, callback) {
|
|
72
|
+
return restApiClient.query(queryObject, callback);
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
return client;
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=gtxclient.js.map
|