Skip to content

Commit

Permalink
Add get signatureHash (#10)
Browse files Browse the repository at this point in the history
* Add get signatureHash

* Update type

* Fix deserialize

* Update

* Remove recursive package
  • Loading branch information
anhcao142 authored Dec 8, 2020
1 parent c7356f1 commit 1e88108
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 247 deletions.
12 changes: 7 additions & 5 deletions hedera.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export declare namespace util {
function getTransfers(transaction: {}): { accountId: string, amount: string }[];
function getRecordTransfers(record: {}): { accountId: string, amount: string }[];
function deserializeTx(hex: string): Transaction
function serializeAccountID(accountId: AccountID): string
function serializeAccountID(accountId: AccountID): string;
function serializeTxID(transactionID: TransactionID): string;
}

export declare interface Duration {
Expand Down Expand Up @@ -147,9 +148,9 @@ export declare namespace Query {
export declare class Query {
constructor(options: { nodeAccountId: AccountID | string, operatorId: AccountID | string });

serialize(): Promise<string>;
static deserialize(hex: string): Promise<{}>;
signTransaction(privateKey: string): Promise<Query>;
serialize(): Buffer;
static deserialize(hex: string): {};
signTransaction(privateKey: string): Query;
toObject(): {};
}

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

get signatureHash(): Buffer;
addSignature(signature: string | Buffer, publicKey: string): Transaction;
serialize(): string;
serialize(): Buffer;
static deserialize(hex: string): Transaction;
static serializeBody(tx: any): Buffer;
toObject(): Transaction;
Expand Down
2 changes: 0 additions & 2 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const Long = require('long');

const TRANSACTION_RESPONSE_CODE = {
0: 'OK', // The transaction passed the precheck validations.
1: 'INVALID_TRANSACTION', // For any error not handled by specific error codes listed below.
Expand Down
4 changes: 2 additions & 2 deletions lib/cryptoGetAccountBalance.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class CryptoGetAccountBalance extends Query {
};
}

static async handleResponse(response) {
const res = await super.handleResponse(response);
static handleResponse(response) {
const res = super.handleResponse(response);
return res.cryptogetAccountBalance;
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/cryptoGetAccountRecords.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class CryptoGetAccountRecords extends Query {
};
}

static async handleResponse(response) {
const res = await super.handleResponse(response);
static handleResponse(response) {
const res = super.handleResponse(response);
return res.cryptoGetAccountRecords;
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/cryptoGetInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class CryptoGetInfo extends Query {
};
}

static async handleResponse(response) {
const res = await super.handleResponse(response);
static handleResponse(response) {
const res = super.handleResponse(response);
return res.cryptoGetInfo;
}
}
Expand Down
10 changes: 10 additions & 0 deletions lib/protoHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ function deserialize(msgName, hex) {
exports.SignedTransactionProto = {
serialize: (obj) => serialize('SignedTransaction', obj),
deserialize: (hex) => deserialize('SignedTransaction', hex),
deserializeDeep: (hex) => {
const signedTx = deserialize('SignedTransaction', hex);
const body = this.TransactionBodyProto.deserialize(signedTx.bodyBytes);
return { ...signedTx, body };
},
};

exports.TransactionBodyProto = {
Expand All @@ -39,3 +44,8 @@ exports.QueryProto = {
serialize: (obj) => serialize('Query', obj),
deserialize: (hex) => deserialize('Query', hex),
};

exports.TransactionIDProto = {
serialize: (obj) => serialize('TransactionID', obj),
deserialize: (hex) => deserialize('TransactionID', hex),
};
10 changes: 4 additions & 6 deletions lib/query.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const protobuf = require('protobufjs');
const path = require('path');
const Long = require('long');
const CryptoTransfer = require('./cryptoTransfer');
const AccountID = require('./accountId');
Expand All @@ -25,12 +23,12 @@ class Query {
});
}

async serialize() {
serialize() {
const queryObj = this.toObject();
return QueryProto.serialize(queryObj);
}

static async deserialize(hex) {
static deserialize(hex) {
return QueryProto.deserialize(hex);
}

Expand Down Expand Up @@ -86,8 +84,8 @@ class Query {
return res;
}

async signTransaction(privateKey, publicKey) {
await this.payment.signTransaction(privateKey, publicKey);
signTransaction(privateKey, publicKey) {
this.payment.signTransaction(privateKey, publicKey);
return this;
}

Expand Down
42 changes: 10 additions & 32 deletions lib/transaction.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
const protobuf = require('protobufjs');
const forge = require('node-forge');
const path = require('path');
const Long = require('long');
const duration = require('./duration');
const AccountID = require('./accountId');
const { SignedTransactionProto, TransactionBodyProto } = require('./protoHelper');
const { SignedTransactionProto, TransactionBodyProto, TransactionProto } = require('./protoHelper');
const { DEFAULT_TX_FEE, TRANSACTION_RESPONSE_CODE } = require('./constants');

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

class Transaction {
constructor({ operatorId, nodeAccountId }) {
Expand Down Expand Up @@ -40,35 +37,16 @@ class Transaction {
};
}

serialize() {
const TransactionProto = txRoot.lookup('proto.Transaction');
const error = TransactionProto.verify(this.tx);
if (error) throw Error(error);
get signatureHash() {
return TransactionBodyProto.serialize(this.txBody);
}

const message = TransactionProto.create(this.tx);
const buffer = TransactionProto.encode(message).finish();
return buffer.toString('hex');
serialize() {
return TransactionProto.serialize(this.tx);
}

static deserialize(hex) {
let signed;
let body;
const buffer = Buffer.from(hex, 'hex');
const TransactionProto = txRoot.lookup('proto.Transaction');
const tx = TransactionProto.decode(buffer);

const error = TransactionProto.verify(tx);
if (error) return undefined;

if (tx.signedTransactionBytes) {
signed = SignedTransactionProto.deserialize(tx.signedTransactionBytes);
}

if (signed.bodyBytes) {
body = TransactionBodyProto.deserialize(signed.bodyBytes);
}

return body;
return TransactionProto.deserialize(hex);
}

toObject() {
Expand All @@ -79,7 +57,7 @@ class Transaction {
return this.txBody.transactionID;
}

static async handleResponse(response) {
static handleResponse(response) {
if (response.nodeTransactionPrecheckCode === 0 || !response.nodeTransactionPrecheckCode) {
return TRANSACTION_RESPONSE_CODE[0];
}
Expand Down Expand Up @@ -108,12 +86,12 @@ class Transaction {
}

// sign and return tx with signature
async signTransaction(privateKey, publicKey) {
signTransaction(privateKey, publicKey) {
if (!this.txBody) {
throw Error('Missing transaction body. Must create transaction before adding signature');
}

const signature = await this.constructor.sign(this.txBody, privateKey);
const signature = this.constructor.sign(this.txBody, privateKey);
this.addSignature(signature, publicKey);
return this;
}
Expand Down
25 changes: 6 additions & 19 deletions lib/transactionId.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
const path = require('path');
const Long = require('long');
const protobuf = require('protobufjs');
const AccountID = require('./accountId');

const root = protobuf.loadSync(path.join(__dirname, '../hedera-proto/BasicTypes.proto'));
const { TransactionIDProto } = require('./protoHelper');

class TransactionID {
constructor(obj) {
Expand Down Expand Up @@ -43,33 +40,23 @@ class TransactionID {

return {
transactionValidStart,
accountID: new AccountID(accountId).toObject()
accountID: new AccountID(accountId).toObject(),
};
}

static async serialize(transactionId) {
const TransactionIDProto = root.lookup('proto.TransactionID');
static serialize(transactionId) {
const serializeObj = {
transactionValidStart: {
seconds: Long.fromValue(transactionId.transactionValidStart.seconds),
},
accountID: new AccountID(transactionId.accountID).toObject(),
};
const error = TransactionIDProto.verify(serializeObj);
if (error) throw Error(error);

const message = TransactionIDProto.create(serializeObj);
const buffer = TransactionIDProto.encode(message).finish();
return buffer.toString('hex');
return TransactionIDProto.serialize(serializeObj).toString('hex');
}

static async deserialize(hex) {
const buffer = Buffer.from(hex, 'hex');
const TransactionIDProto = root.lookup('proto.TransactionID');
const transactionId = TransactionIDProto.decode(buffer);
const error = TransactionIDProto.verify(transactionId);
if (error) throw error;
return transactionId;
static deserialize(hex) {
return TransactionIDProto.deserialize(hex);
}
}

Expand Down
4 changes: 3 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Long = require('long');
const Decimal = require('decimal.js');
const AccountID = require('./accountId');
const Transaction = require('./transaction');
const { SignedTransactionProto } = require('./protoHelper');
const { HBAR_TO_TINYBAR, DECIMALS } = require('./constants');
const { parsePrivateKey, parsePublicKey } = require('../crypto/asn1parser');

Expand Down Expand Up @@ -91,7 +92,8 @@ function getTransactionId(transaction) {
}

function deserializeTx(hex) {
return Transaction.deserialize(hex);
const tx = Transaction.deserialize(hex);
return { ...tx, ...SignedTransactionProto.deserializeDeep(tx.signedTransactionBytes) };
}

function serializeAccountID(accountId) {
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hedera-sdk-js",
"version": "0.4.0",
"version": "0.5.0",
"description": "Hedera js sdk",
"main": "index.js",
"scripts": {
Expand All @@ -15,7 +15,6 @@
"license": "ISC",
"dependencies": {
"@fidm/asn1": "^1.0.4",
"@hashgraph/sdk": "^0.5.1",
"bluebird": "^3.5.5",
"decimal.js": "^10.2.0",
"grpc": "^1.22.2",
Expand Down
Loading

0 comments on commit 1e88108

Please sign in to comment.