Skip to content

Commit 1e88108

Browse files
authored
Add get signatureHash (#10)
* Add get signatureHash * Update type * Fix deserialize * Update * Remove recursive package
1 parent c7356f1 commit 1e88108

12 files changed

+49
-247
lines changed

hedera.d.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export declare namespace util {
2323
function getTransfers(transaction: {}): { accountId: string, amount: string }[];
2424
function getRecordTransfers(record: {}): { accountId: string, amount: string }[];
2525
function deserializeTx(hex: string): Transaction
26-
function serializeAccountID(accountId: AccountID): string
26+
function serializeAccountID(accountId: AccountID): string;
27+
function serializeTxID(transactionID: TransactionID): string;
2728
}
2829

2930
export declare interface Duration {
@@ -147,9 +148,9 @@ export declare namespace Query {
147148
export declare class Query {
148149
constructor(options: { nodeAccountId: AccountID | string, operatorId: AccountID | string });
149150

150-
serialize(): Promise<string>;
151-
static deserialize(hex: string): Promise<{}>;
152-
signTransaction(privateKey: string): Promise<Query>;
151+
serialize(): Buffer;
152+
static deserialize(hex: string): {};
153+
signTransaction(privateKey: string): Query;
153154
toObject(): {};
154155
}
155156

@@ -161,8 +162,9 @@ export declare interface Transaction {
161162
export declare class Transaction {
162163
constructor(options: { operatorId: AccountID | string, nodeAccountId: AccountID | string});
163164

165+
get signatureHash(): Buffer;
164166
addSignature(signature: string | Buffer, publicKey: string): Transaction;
165-
serialize(): string;
167+
serialize(): Buffer;
166168
static deserialize(hex: string): Transaction;
167169
static serializeBody(tx: any): Buffer;
168170
toObject(): Transaction;

lib/constants.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const Long = require('long');
2-
31
const TRANSACTION_RESPONSE_CODE = {
42
0: 'OK', // The transaction passed the precheck validations.
53
1: 'INVALID_TRANSACTION', // For any error not handled by specific error codes listed below.

lib/cryptoGetAccountBalance.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class CryptoGetAccountBalance extends Query {
1515
};
1616
}
1717

18-
static async handleResponse(response) {
19-
const res = await super.handleResponse(response);
18+
static handleResponse(response) {
19+
const res = super.handleResponse(response);
2020
return res.cryptogetAccountBalance;
2121
}
2222
}

lib/cryptoGetAccountRecords.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class CryptoGetAccountRecords extends Query {
1515
};
1616
}
1717

18-
static async handleResponse(response) {
19-
const res = await super.handleResponse(response);
18+
static handleResponse(response) {
19+
const res = super.handleResponse(response);
2020
return res.cryptoGetAccountRecords;
2121
}
2222
}

lib/cryptoGetInfo.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class CryptoGetInfo extends Query {
1515
};
1616
}
1717

18-
static async handleResponse(response) {
19-
const res = await super.handleResponse(response);
18+
static handleResponse(response) {
19+
const res = super.handleResponse(response);
2020
return res.cryptoGetInfo;
2121
}
2222
}

lib/protoHelper.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ function deserialize(msgName, hex) {
2323
exports.SignedTransactionProto = {
2424
serialize: (obj) => serialize('SignedTransaction', obj),
2525
deserialize: (hex) => deserialize('SignedTransaction', hex),
26+
deserializeDeep: (hex) => {
27+
const signedTx = deserialize('SignedTransaction', hex);
28+
const body = this.TransactionBodyProto.deserialize(signedTx.bodyBytes);
29+
return { ...signedTx, body };
30+
},
2631
};
2732

2833
exports.TransactionBodyProto = {
@@ -39,3 +44,8 @@ exports.QueryProto = {
3944
serialize: (obj) => serialize('Query', obj),
4045
deserialize: (hex) => deserialize('Query', hex),
4146
};
47+
48+
exports.TransactionIDProto = {
49+
serialize: (obj) => serialize('TransactionID', obj),
50+
deserialize: (hex) => deserialize('TransactionID', hex),
51+
};

lib/query.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const protobuf = require('protobufjs');
2-
const path = require('path');
31
const Long = require('long');
42
const CryptoTransfer = require('./cryptoTransfer');
53
const AccountID = require('./accountId');
@@ -25,12 +23,12 @@ class Query {
2523
});
2624
}
2725

28-
async serialize() {
26+
serialize() {
2927
const queryObj = this.toObject();
3028
return QueryProto.serialize(queryObj);
3129
}
3230

33-
static async deserialize(hex) {
31+
static deserialize(hex) {
3432
return QueryProto.deserialize(hex);
3533
}
3634

@@ -86,8 +84,8 @@ class Query {
8684
return res;
8785
}
8886

89-
async signTransaction(privateKey, publicKey) {
90-
await this.payment.signTransaction(privateKey, publicKey);
87+
signTransaction(privateKey, publicKey) {
88+
this.payment.signTransaction(privateKey, publicKey);
9189
return this;
9290
}
9391

lib/transaction.js

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
const protobuf = require('protobufjs');
21
const forge = require('node-forge');
3-
const path = require('path');
42
const Long = require('long');
53
const duration = require('./duration');
64
const AccountID = require('./accountId');
7-
const { SignedTransactionProto, TransactionBodyProto } = require('./protoHelper');
5+
const { SignedTransactionProto, TransactionBodyProto, TransactionProto } = require('./protoHelper');
86
const { DEFAULT_TX_FEE, TRANSACTION_RESPONSE_CODE } = require('./constants');
97

108
const { ed25519 } = forge;
11-
const txRoot = protobuf.loadSync(path.join(__dirname, '../hedera-proto/Transaction.proto'));
129

1310
class Transaction {
1411
constructor({ operatorId, nodeAccountId }) {
@@ -40,35 +37,16 @@ class Transaction {
4037
};
4138
}
4239

43-
serialize() {
44-
const TransactionProto = txRoot.lookup('proto.Transaction');
45-
const error = TransactionProto.verify(this.tx);
46-
if (error) throw Error(error);
40+
get signatureHash() {
41+
return TransactionBodyProto.serialize(this.txBody);
42+
}
4743

48-
const message = TransactionProto.create(this.tx);
49-
const buffer = TransactionProto.encode(message).finish();
50-
return buffer.toString('hex');
44+
serialize() {
45+
return TransactionProto.serialize(this.tx);
5146
}
5247

5348
static deserialize(hex) {
54-
let signed;
55-
let body;
56-
const buffer = Buffer.from(hex, 'hex');
57-
const TransactionProto = txRoot.lookup('proto.Transaction');
58-
const tx = TransactionProto.decode(buffer);
59-
60-
const error = TransactionProto.verify(tx);
61-
if (error) return undefined;
62-
63-
if (tx.signedTransactionBytes) {
64-
signed = SignedTransactionProto.deserialize(tx.signedTransactionBytes);
65-
}
66-
67-
if (signed.bodyBytes) {
68-
body = TransactionBodyProto.deserialize(signed.bodyBytes);
69-
}
70-
71-
return body;
49+
return TransactionProto.deserialize(hex);
7250
}
7351

7452
toObject() {
@@ -79,7 +57,7 @@ class Transaction {
7957
return this.txBody.transactionID;
8058
}
8159

82-
static async handleResponse(response) {
60+
static handleResponse(response) {
8361
if (response.nodeTransactionPrecheckCode === 0 || !response.nodeTransactionPrecheckCode) {
8462
return TRANSACTION_RESPONSE_CODE[0];
8563
}
@@ -108,12 +86,12 @@ class Transaction {
10886
}
10987

11088
// sign and return tx with signature
111-
async signTransaction(privateKey, publicKey) {
89+
signTransaction(privateKey, publicKey) {
11290
if (!this.txBody) {
11391
throw Error('Missing transaction body. Must create transaction before adding signature');
11492
}
11593

116-
const signature = await this.constructor.sign(this.txBody, privateKey);
94+
const signature = this.constructor.sign(this.txBody, privateKey);
11795
this.addSignature(signature, publicKey);
11896
return this;
11997
}

lib/transactionId.js

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
const path = require('path');
21
const Long = require('long');
3-
const protobuf = require('protobufjs');
42
const AccountID = require('./accountId');
5-
6-
const root = protobuf.loadSync(path.join(__dirname, '../hedera-proto/BasicTypes.proto'));
3+
const { TransactionIDProto } = require('./protoHelper');
74

85
class TransactionID {
96
constructor(obj) {
@@ -43,33 +40,23 @@ class TransactionID {
4340

4441
return {
4542
transactionValidStart,
46-
accountID: new AccountID(accountId).toObject()
43+
accountID: new AccountID(accountId).toObject(),
4744
};
4845
}
4946

50-
static async serialize(transactionId) {
51-
const TransactionIDProto = root.lookup('proto.TransactionID');
47+
static serialize(transactionId) {
5248
const serializeObj = {
5349
transactionValidStart: {
5450
seconds: Long.fromValue(transactionId.transactionValidStart.seconds),
5551
},
5652
accountID: new AccountID(transactionId.accountID).toObject(),
5753
};
58-
const error = TransactionIDProto.verify(serializeObj);
59-
if (error) throw Error(error);
6054

61-
const message = TransactionIDProto.create(serializeObj);
62-
const buffer = TransactionIDProto.encode(message).finish();
63-
return buffer.toString('hex');
55+
return TransactionIDProto.serialize(serializeObj).toString('hex');
6456
}
6557

66-
static async deserialize(hex) {
67-
const buffer = Buffer.from(hex, 'hex');
68-
const TransactionIDProto = root.lookup('proto.TransactionID');
69-
const transactionId = TransactionIDProto.decode(buffer);
70-
const error = TransactionIDProto.verify(transactionId);
71-
if (error) throw error;
72-
return transactionId;
58+
static deserialize(hex) {
59+
return TransactionIDProto.deserialize(hex);
7360
}
7461
}
7562

lib/util.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const Long = require('long');
33
const Decimal = require('decimal.js');
44
const AccountID = require('./accountId');
55
const Transaction = require('./transaction');
6+
const { SignedTransactionProto } = require('./protoHelper');
67
const { HBAR_TO_TINYBAR, DECIMALS } = require('./constants');
78
const { parsePrivateKey, parsePublicKey } = require('../crypto/asn1parser');
89

@@ -91,7 +92,8 @@ function getTransactionId(transaction) {
9192
}
9293

9394
function deserializeTx(hex) {
94-
return Transaction.deserialize(hex);
95+
const tx = Transaction.deserialize(hex);
96+
return { ...tx, ...SignedTransactionProto.deserializeDeep(tx.signedTransactionBytes) };
9597
}
9698

9799
function serializeAccountID(accountId) {

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hedera-sdk-js",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"description": "Hedera js sdk",
55
"main": "index.js",
66
"scripts": {
@@ -15,7 +15,6 @@
1515
"license": "ISC",
1616
"dependencies": {
1717
"@fidm/asn1": "^1.0.4",
18-
"@hashgraph/sdk": "^0.5.1",
1918
"bluebird": "^3.5.5",
2019
"decimal.js": "^10.2.0",
2120
"grpc": "^1.22.2",

0 commit comments

Comments
 (0)