Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add decodeEventData and decodeEventKey functions and test cases (#23)
* Add read-biginit package

* Add decodeEventData and decodeEventKey functions

* Add test cases for decodeEventData and decodeEventKey

* Update starcoin.js version

* Udpate starcoin.js versiont to 1.0.0 after main network launch

* Remove decode-event.ts from onchain-events folder

* Move decode event data function to encoding folder

* Add examples of decoding event data and event key

* Remove decode_event export
  • Loading branch information
tarvos21 committed May 26, 2021
1 parent 6b0b45d commit a9bb710
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 5 deletions.
5 changes: 3 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "@starcoin/starcoin",
"version": "0.3.0",
"version": "1.0.0",
"description": "starcoin js sdk",
"author": "lerencao, wk3368, tarvos21",
"license": "Apache-2.0",
Expand Down Expand Up @@ -51,6 +51,7 @@
"js-sha3": "^0.8.0",
"jssha": "^3.2.0",
"noble-ed25519": "^1.0.4",
"read-bigint": "^0.1.6",
"ws": "7.2.3"
},
"devDependencies": {
Expand Down Expand Up @@ -99,4 +100,4 @@
"prettier": {
"singleQuote": true
}
}
}
69 changes: 68 additions & 1 deletion src/encoding/index.spec.ts
@@ -1,5 +1,5 @@
import { arrayify } from '@ethersproject/bytes';
import { addressToSCS, decodeEventKey, decodeTransactionPayload, decodeSignedUserTransaction, privateKeyToPublicKey, publicKeyToAuthKey, publicKeyToAddress, publicKeyToReceiptIdentifier, encodeReceiptIdentifier, decodeReceiptIdentifier } from '.';
import { addressToSCS, decodeEventData, decodeEventKey, decodeTransactionPayload, decodeSignedUserTransaction, privateKeyToPublicKey, publicKeyToAuthKey, publicKeyToAddress, publicKeyToReceiptIdentifier, encodeReceiptIdentifier, decodeReceiptIdentifier } from '.';
import { BcsSerializer } from '../lib/runtime/bcs';
import { toHexString } from '../utils/hex';
import { JsonrpcProvider } from '../providers/jsonrpc-provider';
Expand Down Expand Up @@ -184,3 +184,70 @@ test("encode && decode receipt identifier", () => {
})();

});

// To test and see the decoded event data structure, run:
// yarn test:unit src/encoding/index.spec.ts --testNamePattern="decode"

test('decode deposit event data', () => {
const eventName = 'DepositEvent';
const eventData = '0x00ca9a3b00000000000000000000000000000000000000000000000000000001035354430353544300';
const result = decodeEventData(eventName, eventData);
console.log(result.toJS());
});

test('decode withdraw event data', () => {
const eventName = 'WithdrawEvent';
const eventData = '0x00e1f50500000000000000000000000000000000000000000000000000000001035354430353544300';
const result = decodeEventData(eventName, eventData);
console.log(result.toJS());
});

test('decode new block event data', () => {
const eventName = 'NewBlockEvent';
const eventData = '0x440000000000000094e957321e7bb2d3eb08ff6be81a6fcdec8a9d73780100000000000000000000';
const result = decodeEventData(eventName, eventData);
console.log(result.toJS());
});

test('decode block reward event data', () => {
const eventName = 'BlockRewardEvent';
const eventData = '0x57fa0200000000006041c420010000000000000000000000000000000000000000000000000000009a306cd9afde5d249257c2c6e6f39103';
const result = decodeEventData(eventName, eventData);
console.log(result.toJS());
});

test('decode accept token event data', () => {
const eventName = 'AcceptTokenEvent';
const eventData = '0x000000000000000000000000000000010353544303535443';
const result = decodeEventData(eventName, eventData);
console.log(result.toJS());
});

test('decode mint event data', () => {
const eventName = 'MintEvent';
const eventData = '0x80cb29d0000000000000000000000000000000000000000000000000000000010353544303535443';
const result = decodeEventData(eventName, eventData);
console.log(result.toJS());
});

test('decode vote changed event data', () => {
const eventName = 'VoteChangedEvent';
const eventData = '0x0a000000000000000000000000000000000000000a550c180000000000000000000000000a550c180100003426f56b1c000000000000000000';
const result = decodeEventData(eventName, eventData);
console.log(result.toJS());
});

test('decode proposal created event data', () => {
const eventName = 'ProposalCreatedEvent';
const eventData = '0x03000000000000000000000000000000000000000a550c18';
const result = decodeEventData(eventName, eventData);
console.log(result.toJS());
});


// Decode event key
test("decode event key", () => {
const eventKeyInHex = "0x000000000000000063af4e1cf4e6345df840f4c57597a0f6";
const eventKey = decodeEventKey(eventKeyInHex);
console.log(eventKey);
})
17 changes: 16 additions & 1 deletion src/encoding/index.ts
@@ -1,6 +1,7 @@
import { arrayify, BytesLike, hexlify } from '@ethersproject/bytes';
import { addHexPrefix, stripHexPrefix } from 'ethereumjs-util';
import * as ed from 'noble-ed25519';
import { readBigUInt64LE } from "read-bigint";
import { BcsDeserializer, BcsSerializer } from '../lib/runtime/bcs';
import * as starcoin_types from '../lib/runtime/starcoin_types';
import * as serde from '../lib/runtime/serde';
Expand All @@ -17,6 +18,8 @@ import {
import { fromHexString, toHexString } from '../utils/hex';
import { createUserTransactionHasher } from '../crypto_hash';
import { Deserializer } from '../lib/runtime/serde';
import '../onchain-events';
import * as onchain_events_types from '../lib/runtime/onchain_events';

const jsSHA = require('jssha/dist/sha3');

Expand Down Expand Up @@ -170,7 +173,9 @@ export function decodeEventKey(
);
}
const saltBytes = bytes.slice(0, EVENT_KEY_LENGTH - ACCOUNT_ADDRESS_LENGTH);
const salt = Buffer.from(saltBytes).readBigUInt64LE();
const buff = Buffer.from(saltBytes);
// const salt = buff.readBigUInt64LE();
const salt = readBigUInt64LE(buff);
const addressBytes = bytes.slice(EVENT_KEY_LENGTH - ACCOUNT_ADDRESS_LENGTH);
const address = toHexString(addressBytes);
return { address, salt };
Expand Down Expand Up @@ -314,6 +319,16 @@ export function publicKeyToReceiptIdentifier(publicKey: string): string {
return receiptIdentifier
}


export function decodeEventData(eventName: string, eventData: string): any {
const eventType = onchain_events_types[eventName];
const d = bcsDecode(
eventType,
eventData
);
return d;
}

// export function txnArgFromSCS(data: starcoin_types.TransactionArgument): TransactionArgument {
// if (data instanceof starcoin_types.TransactionArgumentVariantBool) {
// return { Bool: data.value };
Expand Down
2 changes: 1 addition & 1 deletion src/onchain-events/index.ts
Expand Up @@ -131,4 +131,4 @@ onchain_events.DepositEvent.prototype.toJS = function (): DepositEvent {
metadata: this.metadata,
token_code: this.token_code.toJS(),
};
};
};
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -6464,6 +6464,11 @@ react-is@^17.0.1:
resolved "https://registry.npm.taobao.org/react-is/download/react-is-17.0.2.tgz"
integrity "sha1-5pHUqOnHiTZWVVOas3J2Kw77VPA= sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w=="

read-bigint@^0.1.6:
version "0.1.6"
resolved "https://registry.yarnpkg.com/read-bigint/-/read-bigint-0.1.6.tgz#451fdb5ef5a9bb425fba1f92511038e2a54e5f26"
integrity sha512-WHfCMBWes972LRSGYb1/mTW6yy83DQ28Lj94TfVM1HIhFf2I7Di09bUoc8+pPsx4JXgEge2r0d9y+7lj6x3j4g==

read-pkg-up@^2.0.0:
version "2.0.0"
resolved "https://registry.npm.taobao.org/read-pkg-up/download/read-pkg-up-2.0.0.tgz"
Expand Down

0 comments on commit a9bb710

Please sign in to comment.