From 6fce21067203d7f64eb9850f7435c388788a54bc Mon Sep 17 00:00:00 2001 From: Segfault Date: Fri, 2 Apr 2021 11:57:37 +0200 Subject: [PATCH 1/6] CI/CD pipes improvement, added contributing file --- .github/workflows/{cd.yml => merge.yml} | 14 ++-- .../workflows/{ci.yml => pull_request.yml} | 3 +- CONTRIBUTING.md | 73 +++++++++++++++++++ 3 files changed, 81 insertions(+), 9 deletions(-) rename .github/workflows/{cd.yml => merge.yml} (88%) rename .github/workflows/{ci.yml => pull_request.yml} (87%) create mode 100644 CONTRIBUTING.md diff --git a/.github/workflows/cd.yml b/.github/workflows/merge.yml similarity index 88% rename from .github/workflows/cd.yml rename to .github/workflows/merge.yml index db1370a..2e3c716 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/merge.yml @@ -1,4 +1,4 @@ -name: CD +name: Send built code in production when a merge commit is created on: push: @@ -8,8 +8,8 @@ on: jobs: build: name: Release & Publish - runs-on: ubuntu-latest + environment: production strategy: matrix: @@ -27,21 +27,19 @@ jobs: - name: Install dependencies run: yarn --frozen-lockfile - - name: Run code formatter - run: yarn format - - name: Run code linter - run: yarn lint - - name: Run unit tests - run: yarn test - name: Run clean run: yarn clean + - name: Run build lib and types run: yarn build:lib + - name: Copy LICENSE run: cp LICENSE build/LICENSE + - name: Copy README run: cp README.md build/README.md + - name: Copy package.json run: cp package.json build/package.json diff --git a/.github/workflows/ci.yml b/.github/workflows/pull_request.yml similarity index 87% rename from .github/workflows/ci.yml rename to .github/workflows/pull_request.yml index 0aa8bca..b000e11 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/pull_request.yml @@ -1,10 +1,11 @@ -name: CI +name: Format and unit test the code when a pull request is opened on: pull_request: branches: - master - develop + types: [opened, edited, synchronize, reopened] jobs: build: diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..da82749 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,73 @@ +# Contributing + +When contributing to this repository, please first discuss the change you wish to make via issue, +email, or any other method with the owners of this repository before making a change. + +Please note we have a code of conduct, please follow it in all your interactions with the project. + +## Pull Request Process + +1. Ensure any install or build dependencies are removed before the end of the layer when doing a build. +2. Update the README.md with details of changes to the interface, this includes new environment variables, exposed ports, useful file locations and container parameters. +3. Increase the version numbers in any examples files and the README.md to the new version that this Pull Request would represent. The versioning scheme we use is [SemVer](http://semver.org/). +4. You may merge the Pull Request in once you have the approval of one core team member, or if you do not have permission to do that, you may request a reviewer to merge it for you. + +## Code of Conduct + +### Our Pledge + +In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and +our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, +nationality, personal appearance, race, religion, or sexual identity and orientation. + +### Our Standards + +Examples of behavior that contributes to creating a positive environment +include: + +* Using welcoming language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a professional setting + +### Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in +response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions +that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, +threatening, offensive, or harmful. + +### Scope + +This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. +Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed +representative at an online or offline event. +Representation of a project may be further defined and clarified by project maintainers. + +### Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at contact [ at ] lum.network. +All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. +The project team is obligated to maintain confidentiality with regard to the reporter of an incident. +Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other +members of the project's leadership. + +### Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] + +[homepage]: http://contributor-covenant.org +[version]: http://contributor-covenant.org/version/1/4/ \ No newline at end of file From 2a39963997f450bef44c6f328f2b8f71be3f1c78 Mon Sep 17 00:00:00 2001 From: Fabrice Bascoulergue Date: Tue, 13 Apr 2021 18:04:06 +0200 Subject: [PATCH 2/6] Move types declaration dependencies in production from dev dependencies --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index d51a733..0cfbcc8 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,8 @@ "@cosmjs/utils": "^0.24.0-alpha.26", "@ledgerhq/hw-app-cosmos": "^5.46.0", "@ledgerhq/hw-transport": "^5.46.0", + "@types/crypto-js": "^4.0.1", + "@types/ledgerhq__hw-transport": "^4.21.3", "@types/uuid": "^8.3.0", "crypto-browserify": "^3.12.0", "crypto-js": "^4.0.0", @@ -63,9 +65,7 @@ "@babel/plugin-transform-runtime": "^7.12.10", "@babel/preset-env": "^7.8.3", "@babel/preset-typescript": "^7.8.3", - "@types/crypto-js": "^4.0.1", "@types/jest": "^26.0.20", - "@types/ledgerhq__hw-transport": "^4.21.3", "@types/ledgerhq__hw-transport-node-hid": "^4.22.2", "cross-env": "^7.0.3", "eslint": "^7.19.0", From 0bb4864bb6e1d1bc7ba56f622cef857f58d6d46c Mon Sep 17 00:00:00 2001 From: Fabrice Bascoulergue Date: Tue, 13 Apr 2021 18:04:34 +0200 Subject: [PATCH 3/6] Update auto generated docs --- docs/lib/modules/lumutils.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/docs/lib/modules/lumutils.md b/docs/lib/modules/lumutils.md index 5578c79..d83a2ea 100644 --- a/docs/lib/modules/lumutils.md +++ b/docs/lib/modules/lumutils.md @@ -585,11 +585,14 @@ ___ ▸ `Const`**verifySignMsg**(`msg`: [*SignMsg*](../interfaces/lumtypes.signmsg.md)): *Promise* +Verify that a message is signed by the provided publicKey +Will also verify that the address is indeed derivated by the provided publicKey + #### Parameters: -Name | Type | -:------ | :------ | -`msg` | [*SignMsg*](../interfaces/lumtypes.signmsg.md) | +Name | Type | Description | +:------ | :------ | :------ | +`msg` | [*SignMsg*](../interfaces/lumtypes.signmsg.md) | Message to verify such as generated by the LumWallet.signMessage method | **Returns:** *Promise* @@ -599,13 +602,13 @@ ___ ▸ `Const`**verifySignature**(`signature`: *Uint8Array*, `signedBytes`: *Uint8Array*, `publicKey`: *Uint8Array*): *Promise* -Verify that a transaction signature is valid +Verify that a signature is valid #### Parameters: Name | Type | Description | :------ | :------ | :------ | -`signature` | *Uint8Array* | transaction signature (as generated by the generateSignature function) | +`signature` | *Uint8Array* | signature (as generated by the generateSignature function) | `signedBytes` | *Uint8Array* | signed bytes (as generated by the generateSignDocBytes function or by the signMessage function) | `publicKey` | *Uint8Array* | public key of the signing key pair (secp256k1) | From ce234b385a1ada0f328ad07f7989f085f44041c8 Mon Sep 17 00:00:00 2001 From: Fabrice Bascoulergue Date: Tue, 13 Apr 2021 18:05:05 +0200 Subject: [PATCH 4/6] 0.3.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0cfbcc8..cc71334 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lum-network/sdk-javascript", - "version": "0.3.3", + "version": "0.3.4", "license": "Apache-2.0", "description": "Javascript SDK library for NodeJS and Web browsers to interact with the Lum Network.", "homepage": "https://github.com/lum-network/sdk-javascript#readme", From cb37d8d3f6dc3be702c5c3c3201792f1f1f92458 Mon Sep 17 00:00:00 2001 From: Fabrice Bascoulergue Date: Mon, 19 Apr 2021 18:25:33 +0200 Subject: [PATCH 5/6] Add support for ulum base denom --- docs/lib/modules/lumconstants.md | 19 +++++++++++++++ docs/lib/modules/lumutils.md | 23 +++++++++++++++++ src/constants/index.ts | 11 +++++++++ src/utils/index.ts | 1 + src/utils/units.ts | 42 ++++++++++++++++++++++++++++++++ tests/client.test.ts | 4 +-- tests/faucet.test.ts | 2 +- tests/ledger.test.ts | 2 +- tests/utils.test.ts | 15 ++++++++++++ 9 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 src/utils/units.ts create mode 100644 tests/utils.test.ts diff --git a/docs/lib/modules/lumconstants.md b/docs/lib/modules/lumconstants.md index 6fcc445..d423134 100644 --- a/docs/lib/modules/lumconstants.md +++ b/docs/lib/modules/lumconstants.md @@ -16,7 +16,9 @@ - [LumBech32PrefixValAddr](lumconstants.md#lumbech32prefixvaladdr) - [LumBech32PrefixValPub](lumconstants.md#lumbech32prefixvalpub) - [LumDenom](lumconstants.md#lumdenom) +- [LumExponent](lumconstants.md#lumexponent) - [LumWalletSigningVersion](lumconstants.md#lumwalletsigningversion) +- [MicroLumDenom](lumconstants.md#microlumdenom) - [PrivateKeyLength](lumconstants.md#privatekeylength) ### Functions @@ -93,6 +95,15 @@ Lum Coin denomination ___ +### LumExponent + +• `Const` **LumExponent**: *6*= 6 + +Lum Exponent +1 lum = 10^6 ulum + +___ + ### LumWalletSigningVersion • `Const` **LumWalletSigningVersion**: *1*= '1' @@ -101,6 +112,14 @@ Signing version of the SDK ___ +### MicroLumDenom + +• `Const` **MicroLumDenom**: *ulum*= 'ulum' + +Micro Lum Coin denomination + +___ + ### PrivateKeyLength • `Const` **PrivateKeyLength**: *32*= 32 diff --git a/docs/lib/modules/lumutils.md b/docs/lib/modules/lumutils.md index d83a2ea..efcff6d 100644 --- a/docs/lib/modules/lumutils.md +++ b/docs/lib/modules/lumutils.md @@ -10,6 +10,7 @@ - [broadcastTxCommitSuccess](lumutils.md#broadcasttxcommitsuccess) - [broadcastTxSyncSuccess](lumutils.md#broadcasttxsyncsuccess) +- [convertUnit](lumutils.md#convertunit) - [generateAuthInfoBytes](lumutils.md#generateauthinfobytes) - [generateKeyStore](lumutils.md#generatekeystore) - [generateMnemonic](lumutils.md#generatemnemonic) @@ -78,6 +79,28 @@ Name | Type | ___ +### convertUnit + +▸ `Const`**convertUnit**(`coin`: [*Coin*](../interfaces/lumtypes.coin.md), `toDenom`: *string*): *string* + +Converts the Coin amount into the destination denom. +This method does not do any actual math and only "move" the floating precision of the amoun in order to avoid any +possible floating point precision issue. +It does nothing if src denom = dst denom. + +#### Parameters: + +Name | Type | Description | +:------ | :------ | :------ | +`coin` | [*Coin*](../interfaces/lumtypes.coin.md) | Coin to convert into toDenom | +`toDenom` | *string* | destination denom to convert into | + +**Returns:** *string* + +the amount converted + +___ + ### generateAuthInfoBytes ▸ `Const`**generateAuthInfoBytes**(`publicKey`: *Uint8Array*, `fee`: [*Fee*](../interfaces/lumtypes.fee.md), `sequence`: *number*, `signMode`: SignMode): *Uint8Array* diff --git a/src/constants/index.ts b/src/constants/index.ts index 2d2e2c6..24b36a8 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -1,8 +1,19 @@ +/** + * Lum Exponent + * 1 lum = 10^6 ulum + */ +export const LumExponent = 6; + /** * Lum Coin denomination */ export const LumDenom = 'lum'; +/** + * Micro Lum Coin denomination + */ +export const MicroLumDenom = 'ulum'; + /** * Lum Network Bech32 prefix of an account's address */ diff --git a/src/utils/index.ts b/src/utils/index.ts index 292207a..631b023 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -6,3 +6,4 @@ export * from './transactions'; export * from './broadcast'; export * from './search'; export * from './txlogs'; +export * from './units'; diff --git a/src/utils/units.ts b/src/utils/units.ts new file mode 100644 index 0000000..102def0 --- /dev/null +++ b/src/utils/units.ts @@ -0,0 +1,42 @@ +import { LumTypes, LumConstants } from '..'; + +/** + * Converts the Coin amount into the destination denom. + * This method does not do any actual math and only "move" the floating precision of the amoun in order to avoid any + * possible floating point precision issue. + * It does nothing if src denom = dst denom. + * + * @param coin Coin to convert into toDenom + * @param toDenom destination denom to convert into + * @returns the amount converted + */ +export const convertUnit = (coin: LumTypes.Coin, toDenom: string): string => { + const parts = coin.amount.split('.'); + if (parts.length > 2) { + throw new Error('More than one separator found'); + } + + if (coin.denom.startsWith('u') && coin.denom.endsWith(toDenom)) { + // from micro to base + if (parts.length !== 1) { + throw new Error('Micro units cannot have floating precision'); + } + let res = parts[0]; + for (let i = res.length; res.length <= LumConstants.LumExponent; i++) { + res = '0' + res; + } + const floatIdx = res.length - LumConstants.LumExponent; + return (res.substring(0, floatIdx) + '.' + res.substring(floatIdx)).replace(/0+$/, ''); + } else if (toDenom.startsWith('u') && toDenom.endsWith(coin.denom)) { + // form base to micro + if (parts.length === 2 && parts[1].length > LumConstants.LumExponent) { + throw new Error(`Floating precision cannot exceed ${LumConstants.LumExponent} digits`); + } + let res = parts[0] + (parts[1] || ''); + for (let i = parts.length === 2 ? parts[1].length : 0; i < LumConstants.LumExponent; i++) { + res += '0'; + } + return res.replace(/^0+/, ''); + } + return coin.amount; +}; diff --git a/tests/client.test.ts b/tests/client.test.ts index 7fa1bfb..de2233b 100644 --- a/tests/client.test.ts +++ b/tests/client.test.ts @@ -40,7 +40,7 @@ describe('LumClient', () => { const supplies = await clt.queryClient.bank.unverified.totalSupply(); expect(supplies).toBeTruthy(); expect(supplies.length).toBeGreaterThan(0); - const lumSupply = supplies.filter((c) => c.denom === LumConstants.LumDenom)[0]; + const lumSupply = supplies.filter((c) => c.denom === LumConstants.MicroLumDenom)[0]; expect(lumSupply).toBeTruthy(); expect(parseFloat(lumSupply.amount)).toBeGreaterThan(0); }); @@ -80,7 +80,7 @@ describe('LumClient', () => { const balances = await clt.getAllBalancesUnverified(account.address); expect(balances).toBeTruthy(); expect(balances.length).toBeGreaterThan(0); - const lumBalance = balances.filter((b) => b.denom === LumConstants.LumDenom)[0]; + const lumBalance = balances.filter((b) => b.denom === LumConstants.MicroLumDenom)[0]; expect(lumBalance).toBeTruthy(); expect(parseFloat(lumBalance.amount)).toBeGreaterThan(0); }); diff --git a/tests/faucet.test.ts b/tests/faucet.test.ts index ac9984d..acf7c1a 100644 --- a/tests/faucet.test.ts +++ b/tests/faucet.test.ts @@ -17,7 +17,7 @@ describe('Faucet', () => { // WIP // const mintMsg = LumMessages.BuildMsgMintAndSend(w1.getAddress(), new Date()); // const fee = { - // amount: [{ denom: LumConstants.LumDenom, amount: '0' }], + // amount: [{ denom: LumConstants.MicroLumDenom, amount: '0' }], // gas: '100000', // }; // const chainId = await clt.getChainId(); diff --git a/tests/ledger.test.ts b/tests/ledger.test.ts index 5e2ca43..273a77f 100644 --- a/tests/ledger.test.ts +++ b/tests/ledger.test.ts @@ -27,7 +27,7 @@ describe('Ledger', () => { // const chainId = await clt.getChainId(); // const sendMsg = LumMessages.BuildMsgSend(w1.getAddress(), 'lum1lsagfzrm4gz28he4wunt63sts5xzmczwjttsr9', [{ denom: 'lum', amount: '3' }]); // const fee = { - // amount: [{ denom: LumConstants.LumDenom, amount: '1' }], + // amount: [{ denom: LumConstants.MicroLumDenom, amount: '1' }], // gas: '100000', // }; // const doc = { diff --git a/tests/utils.test.ts b/tests/utils.test.ts new file mode 100644 index 0000000..5946e93 --- /dev/null +++ b/tests/utils.test.ts @@ -0,0 +1,15 @@ +import { LumConstants, LumUtils } from '../src'; + +describe('Utils', () => { + it('Unit conversion should output consistent results', () => { + expect(LumUtils.convertUnit({ denom: LumConstants.LumDenom, amount: '23.456789' }, LumConstants.LumDenom)).toEqual('23.456789'); + expect(LumUtils.convertUnit({ denom: LumConstants.LumDenom, amount: '23.456789' }, LumConstants.MicroLumDenom)).toEqual('23456789'); + expect(LumUtils.convertUnit({ denom: LumConstants.LumDenom, amount: '23456789' }, LumConstants.MicroLumDenom)).toEqual('23456789000000'); + expect(LumUtils.convertUnit({ denom: LumConstants.MicroLumDenom, amount: '123456789000' }, LumConstants.LumDenom)).toEqual('123456.789'); + expect(LumUtils.convertUnit({ denom: LumConstants.MicroLumDenom, amount: '123456789111' }, LumConstants.LumDenom)).toEqual('123456.789111'); + expect(LumUtils.convertUnit({ denom: LumConstants.MicroLumDenom, amount: '123456' }, LumConstants.LumDenom)).toEqual('0.123456'); + expect(LumUtils.convertUnit({ denom: LumConstants.MicroLumDenom, amount: '23456' }, LumConstants.LumDenom)).toEqual('0.023456'); + expect(LumUtils.convertUnit({ denom: LumConstants.LumDenom, amount: '0.000001' }, LumConstants.MicroLumDenom)).toEqual('1'); + expect(LumUtils.convertUnit({ denom: LumConstants.MicroLumDenom, amount: '1' }, LumConstants.LumDenom)).toEqual('0.000001'); + }); +}); From f3e9c9183640bd41c1af7c8c5d28ccff7f5deb3e Mon Sep 17 00:00:00 2001 From: Fabrice Bascoulergue Date: Mon, 19 Apr 2021 18:26:16 +0200 Subject: [PATCH 6/6] 0.3.5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index cc71334..65c65b2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lum-network/sdk-javascript", - "version": "0.3.4", + "version": "0.3.5", "license": "Apache-2.0", "description": "Javascript SDK library for NodeJS and Web browsers to interact with the Lum Network.", "homepage": "https://github.com/lum-network/sdk-javascript#readme",