Skip to content

Commit

Permalink
getTxTimestamp fixed (#237)
Browse files Browse the repository at this point in the history
Signed-off-by: Kestutis Gudynas <44440041+kemi04@users.noreply.github.com>

Co-authored-by: Matthew B White <mbwhite@users.noreply.github.com>
  • Loading branch information
kemi04 and mbwhite authored Nov 26, 2020
1 parent bfade8a commit bc9f669
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions apis/fabric-shim-api/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ declare module 'fabric-shim-api' {

getSignedProposal(): ChaincodeProposal.SignedProposal;
getTxTimestamp(): Timestamp;
getDateTimestamp(): Date;
getBinding(): string;

getState(key: string): Promise<Uint8Array>;
Expand Down
11 changes: 11 additions & 0 deletions libraries/fabric-shim/lib/stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,22 @@ class ChaincodeStub {
* Returns the timestamp when the transaction was created. This
* is taken from the transaction {@link ChannelHeader}, therefore it will indicate the
* client's timestamp, and will have the same value across all endorsers.
* Object returned: { seconds: [Long] { low: [int32], high: [int32], unsigned: [bool] }, nanos: [int32] }
*/
getTxTimestamp() {
return this.txTimestamp;
}

/**
* Returns the Date object of when the transaction was created. This
* is taken from the transaction {@link ChannelHeader}, therefore it will indicate the
* client's date, and will have the same value across all endorsers.
*/
getDateTimestamp() {
const date = new Date(this.txTimestamp.seconds * 1e3 + this.txTimestamp.nanos / 1e6);
return date;
}

/**
* Returns a HEX-encoded string of SHA256 hash of the transaction's nonce, creator and epoch concatenated, as a
* unique representation of the specific transaction. This value can be used to prevent replay attacks in chaincodes
Expand Down
11 changes: 11 additions & 0 deletions libraries/fabric-shim/test/unit/stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,17 @@ describe('Stub', () => {
});
});

describe('getDateTimestamp', () => {
it ('should return transaction date as Node.js Date object', () => {
const stub = new Stub('dummyClient', 'dummyChannelId', 'dummyTxid', {
args: []
});
stub.txTimestamp = {seconds: 1606233385, nanos: 54000000};

expect(stub.getDateTimestamp()).to.deep.equal(new Date(1606233385054));
});
});

describe('getBinding', () => {
it ('should return binding', () => {
const stub = new Stub('dummyClient', 'dummyChannelId', 'dummyTxid', {
Expand Down

0 comments on commit bc9f669

Please sign in to comment.