Skip to content

Commit

Permalink
feat(sdk-coin-zeta): zeta sdk init along with testcases
Browse files Browse the repository at this point in the history
Ticket: WIN-152
  • Loading branch information
grsind19 committed Jul 27, 2023
1 parent 24c5df6 commit e9aa40b
Show file tree
Hide file tree
Showing 34 changed files with 1,986 additions and 4 deletions.
11 changes: 7 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ COPY --from=builder /tmp/bitgo/modules/sdk-coin-ltc /var/modules/sdk-coin-ltc/
COPY --from=builder /tmp/bitgo/modules/sdk-coin-xlm /var/modules/sdk-coin-xlm/
COPY --from=builder /tmp/bitgo/modules/sdk-coin-xrp /var/modules/sdk-coin-xrp/
COPY --from=builder /tmp/bitgo/modules/sdk-coin-zec /var/modules/sdk-coin-zec/
COPY --from=builder /tmp/bitgo/modules/sdk-coin-zeta /var/modules/sdk-coin-zeta/

RUN cd /var/modules/sdk-core && yarn link && \
cd /var/modules/bls-dkg && yarn link && \
Expand Down Expand Up @@ -152,7 +153,8 @@ cd /var/modules/sdk-coin-ethw && yarn link && \
cd /var/modules/sdk-coin-ltc && yarn link && \
cd /var/modules/sdk-coin-xlm && yarn link && \
cd /var/modules/sdk-coin-xrp && yarn link && \
cd /var/modules/sdk-coin-zec && yarn link
cd /var/modules/sdk-coin-zec && yarn link && \
cd /var/modules/sdk-coin-zeta && yarn link
#COPY_END

#LINK_START
Expand Down Expand Up @@ -213,13 +215,14 @@ RUN cd /var/bitgo-express && \
yarn link @bitgo/sdk-coin-ltc && \
yarn link @bitgo/sdk-coin-xlm && \
yarn link @bitgo/sdk-coin-xrp && \
yarn link @bitgo/sdk-coin-zec
yarn link @bitgo/sdk-coin-zec && \
yarn link @bitgo/sdk-coin-zeta
#LINK_END

#LABEL_START
LABEL created="Thu, 13 Jul 2023 00:56:22 GMT"
LABEL created="Fri, 21 Jul 2023 10:54:35 GMT"
LABEL version=9.29.0
LABEL git_hash=8b129c3df223bcea9b3861b3c93e4342b0531803
LABEL git_hash=eeb3f889afb12ef1f2d9723d224e5e59a9ec59d3
#LABEL_END

USER node
Expand Down
1 change: 1 addition & 0 deletions modules/bitgo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"@bitgo/sdk-coin-xrp": "^1.5.6",
"@bitgo/sdk-coin-xtz": "^1.4.27",
"@bitgo/sdk-coin-zec": "^1.3.27",
"@bitgo/sdk-coin-zeta": "^1.0.0",
"@bitgo/sdk-core": "^8.10.0",
"@bitgo/sdk-lib-mpc": "^8.5.0",
"@bitgo/sjcl": "^1.0.1",
Expand Down
3 changes: 3 additions & 0 deletions modules/bitgo/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@
{
"path": "../sdk-coin-zec"
},
{
"path": "../sdk-coin-zeta"
},
{
"path": "../sdk-core"
},
Expand Down
5 changes: 5 additions & 0 deletions modules/sdk-coin-zeta/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
.idea
public
dist

3 changes: 3 additions & 0 deletions modules/sdk-coin-zeta/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
.idea/
dist/
8 changes: 8 additions & 0 deletions modules/sdk-coin-zeta/.mocharc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require: 'ts-node/register'
timeout: '60000'
reporter: 'min'
reporter-option:
- 'cdn=true'
- 'json=false'
exit: true
spec: ['test/unit/**/*.ts']
14 changes: 14 additions & 0 deletions modules/sdk-coin-zeta/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
!dist/
dist/test/
dist/tsconfig.tsbuildinfo
.idea/
.prettierrc.yml
tsconfig.json
src/
test/
scripts/
.nyc_output
CODEOWNERS
node_modules/
.prettierignore
.mocharc.js
2 changes: 2 additions & 0 deletions modules/sdk-coin-zeta/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.nyc_output/
dist/
3 changes: 3 additions & 0 deletions modules/sdk-coin-zeta/.prettierrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
printWidth: 120
singleQuote: true
trailingComma: 'es5'
30 changes: 30 additions & 0 deletions modules/sdk-coin-zeta/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# BitGo sdk-coin-zeta

SDK coins provide a modular approach to a monolithic architecture. This and all BitGoJS SDK coins allow developers to use only the coins needed for a given project.

## Installation

All coins are loaded traditionally through the `bitgo` package. If you are using coins individually, you will be accessing the coin via the `@bitgo/sdk-api` package.

In your project install both `@bitgo/sdk-api` and `@bitgo/sdk-coin-zeta`.

```shell
npm i @bitgo/sdk-api @bitgo/sdk-coin-zeta
```

Next, you will be able to initialize an instance of "bitgo" through `@bitgo/sdk-api` instead of `bitgo`.

```javascript
import { BitGoAPI } from '@bitgo/sdk-api';
import { Zeta } from '@bitgo/sdk-coin-zeta';

const sdk = new BitGoAPI();

sdk.register('zeta', Zeta.createInstance);
```

## Development

Most of the coin implementations are derived from `@bitgo/sdk-core`, `@bitgo/statics`, and coin specific packages. These implementations are used to interact with the BitGo API and BitGo platform services.

You will notice that the basic version of common class extensions have been provided to you and must be resolved before the package build will succeed. Upon initiation of a given SDK coin, you will need to verify that your coin has been included in the root `tsconfig.packages.json` and that the linting, formatting, and testing succeeds when run both within the coin and from the root of BitGoJS.
56 changes: 56 additions & 0 deletions modules/sdk-coin-zeta/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "@bitgo/sdk-coin-zeta",
"version": "1.0.0",
"description": "BitGo SDK coin library for Zeta",
"main": "./dist/src/index.js",
"types": "./dist/src/index.d.ts",
"scripts": {
"build": "yarn tsc --build --incremental --verbose .",
"fmt": "prettier --write .",
"check-fmt": "prettier --check .",
"clean": "rm -r ./dist",
"lint": "eslint --quiet .",
"prepare": "npm run build",
"test": "npm run coverage",
"coverage": "nyc -- npm run unit-test",
"unit-test": "mocha"
},
"author": "BitGo SDK Team <[email protected]>",
"license": "MIT",
"engines": {
"node": ">=16 <19"
},
"repository": {
"type": "git",
"url": "https://github.com/BitGo/BitGoJS.git",
"directory": "modules/sdk-coin-zeta"
},
"lint-staged": {
"*.{js,ts}": [
"yarn prettier --write",
"yarn eslint --fix"
]
},
"publishConfig": {
"access": "public"
},
"nyc": {
"extension": [
".ts"
]
},
"dependencies": {
"@bitgo/abstract-cosmos": "^1.3.0",
"@bitgo/sdk-core": "^8.10.0",
"@bitgo/statics": "^17.0.1",
"@cosmjs/amino": "^0.29.5",
"@cosmjs/encoding": "^0.29.5",
"@cosmjs/stargate": "^0.29.5",
"bignumber.js": "^9.1.1"
},
"devDependencies": {
"@bitgo/sdk-api": "^1.11.2",
"@bitgo/sdk-test": "^1.2.27",
"@types/lodash": "^4.14.183"
}
}
4 changes: 4 additions & 0 deletions modules/sdk-coin-zeta/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './zeta';
export * from './lib';
export * from './register';
export * from './tzeta';
4 changes: 4 additions & 0 deletions modules/sdk-coin-zeta/src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const validDenoms = ['azeta'];
export const accountAddressRegex = /^(zeta)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l']{38})$/;
export const validatorAddressRegex = /^(zetavaloper)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l']{38})$/;
export const contractAddressRegex = /^(zeta)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l']+)$/;
10 changes: 10 additions & 0 deletions modules/sdk-coin-zeta/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as Constants from './constants';
import * as Utils from './utils';

export {
CosmosTransaction as Transaction,
CosmosTransactionBuilder as TransactionBuilder,
} from '@bitgo/abstract-cosmos';
export { KeyPair } from './keyPair';
export { TransactionBuilderFactory } from './transactionBuilderFactory';
export { Constants, Utils };
25 changes: 25 additions & 0 deletions modules/sdk-coin-zeta/src/lib/keyPair.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { KeyPairOptions } from '@bitgo/sdk-core';
import { pubkeyToAddress } from '@cosmjs/amino';

import { CosmosKeyPair } from '@bitgo/abstract-cosmos';

/**
* Zeta keys and address management.
*/
export class KeyPair extends CosmosKeyPair {
constructor(source?: KeyPairOptions) {
super(source);
}

/** @inheritdoc */
getAddress(): string {
const base64String = Buffer.from(this.getKeys().pub.slice(0, 66), 'hex').toString('base64');
return pubkeyToAddress(
{
type: 'tendermint/PubKeySecp256k1',
value: base64String,
},
'zeta'
);
}
}
85 changes: 85 additions & 0 deletions modules/sdk-coin-zeta/src/lib/transactionBuilderFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import {
CosmosTransaction,
CosmosTransactionBuilder,
CosmosTransferBuilder,
StakingActivateBuilder,
StakingDeactivateBuilder,
StakingWithdrawRewardsBuilder,
ContractCallBuilder,
} from '@bitgo/abstract-cosmos';
import { BaseTransactionBuilderFactory, InvalidTransactionError, TransactionType } from '@bitgo/sdk-core';
import { BaseCoin as CoinConfig } from '@bitgo/statics';
import zetaUtils from './utils';

export class TransactionBuilderFactory extends BaseTransactionBuilderFactory {
constructor(_coinConfig: Readonly<CoinConfig>) {
super(_coinConfig);
}

/** @inheritdoc */
from(raw: string): CosmosTransactionBuilder {
const tx = new CosmosTransaction(this._coinConfig, zetaUtils);
tx.enrichTransactionDetailsFromRawTransaction(raw);
try {
switch (tx.type) {
case TransactionType.Send:
return this.getTransferBuilder(tx);
case TransactionType.StakingActivate:
return this.getStakingActivateBuilder(tx);
case TransactionType.StakingDeactivate:
return this.getStakingDeactivateBuilder(tx);
case TransactionType.StakingWithdraw:
return this.getStakingWithdrawRewardsBuilder(tx);
case TransactionType.ContractCall:
return this.getContractCallBuilder(tx);
default:
throw new InvalidTransactionError('Invalid transaction');
}
} catch (e) {
throw new InvalidTransactionError('Invalid transaction: ' + e.message);
}
}

/** @inheritdoc */
getTransferBuilder(tx?: CosmosTransaction): CosmosTransferBuilder {
return this.initializeBuilder(tx, new CosmosTransferBuilder(this._coinConfig, zetaUtils));
}

/** @inheritdoc */
getStakingActivateBuilder(tx?: CosmosTransaction): StakingActivateBuilder {
return this.initializeBuilder(tx, new StakingActivateBuilder(this._coinConfig, zetaUtils));
}

/** @inheritdoc */
getStakingDeactivateBuilder(tx?: CosmosTransaction): StakingDeactivateBuilder {
return this.initializeBuilder(tx, new StakingDeactivateBuilder(this._coinConfig, zetaUtils));
}

/** @inheritdoc */
getStakingWithdrawRewardsBuilder(tx?: CosmosTransaction): StakingWithdrawRewardsBuilder {
return this.initializeBuilder(tx, new StakingWithdrawRewardsBuilder(this._coinConfig, zetaUtils));
}

getContractCallBuilder(tx?: CosmosTransaction): ContractCallBuilder {
return this.initializeBuilder(tx, new ContractCallBuilder(this._coinConfig, zetaUtils));
}

/** @inheritdoc */
getWalletInitializationBuilder(): void {
throw new Error('Method not implemented.');
}

/**
* Initialize the builder with the given transaction
*
* @param {CosmosTransaction | undefined} tx - the transaction used to initialize the builder
* @param {CosmosTransactionBuilder} builder - the builder to be initialized
* @returns {CosmosTransactionBuilder} the builder initialized
*/
protected initializeBuilder<T extends CosmosTransactionBuilder>(tx: CosmosTransaction | undefined, builder: T): T {
if (tx) {
builder.initBuilder(tx);
}
return builder;
}
}
38 changes: 38 additions & 0 deletions modules/sdk-coin-zeta/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { InvalidTransactionError } from '@bitgo/sdk-core';
import { Coin } from '@cosmjs/stargate';
import BigNumber from 'bignumber.js';

import { CosmosUtils } from '@bitgo/abstract-cosmos';
import * as constants from './constants';

export class ZetaUtils extends CosmosUtils {
/** @inheritdoc */
isValidAddress(address: string): boolean {
return constants.accountAddressRegex.test(address);
}

/** @inheritdoc */
isValidValidatorAddress(address: string): boolean {
return constants.validatorAddressRegex.test(address);
}

/** @inheritdoc */
isValidContractAddress(address: string): boolean {
return constants.contractAddressRegex.test(address);
}

/** @inheritdoc */
validateAmount(amount: Coin): void {
const amountBig = BigNumber(amount.amount);
if (amountBig.isLessThanOrEqualTo(0)) {
throw new InvalidTransactionError('transactionBuilder: validateAmount: Invalid amount: ' + amount.amount);
}
if (!constants.validDenoms.find((denom) => denom === amount.denom)) {
throw new InvalidTransactionError('transactionBuilder: validateAmount: Invalid denom: ' + amount.denom);
}
}
}

const zetaUtils: CosmosUtils = new ZetaUtils();

export default zetaUtils;
8 changes: 8 additions & 0 deletions modules/sdk-coin-zeta/src/register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { BitGoBase } from '@bitgo/sdk-core';
import { Zeta } from './zeta';
import { Tzeta } from './tzeta';

export const register = (sdk: BitGoBase): void => {
sdk.register('zeta', Zeta.createInstance);
sdk.register('tzeta', Tzeta.createInstance);
};
25 changes: 25 additions & 0 deletions modules/sdk-coin-zeta/src/tzeta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Testnet Zeta
*
* @format
*/
import { BaseCoin, BitGoBase } from '@bitgo/sdk-core';
import { BaseCoin as StaticsBaseCoin } from '@bitgo/statics';
import { Zeta } from './zeta';

export class Tzeta extends Zeta {
protected readonly _staticsCoin: Readonly<StaticsBaseCoin>;
protected constructor(bitgo: BitGoBase, staticsCoin?: Readonly<StaticsBaseCoin>) {
super(bitgo, staticsCoin);

if (!staticsCoin) {
throw new Error('missing required constructor parameter staticsCoin');
}

this._staticsCoin = staticsCoin;
}

static createInstance(bitgo: BitGoBase, staticsCoin?: Readonly<StaticsBaseCoin>): BaseCoin {
return new Tzeta(bitgo, staticsCoin);
}
}
Loading

0 comments on commit e9aa40b

Please sign in to comment.