-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk-coin-zeta): zeta sdk init along with testcases
Ticket: WIN-152
- Loading branch information
Showing
34 changed files
with
1,986 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,6 +155,9 @@ | |
{ | ||
"path": "../sdk-coin-zec" | ||
}, | ||
{ | ||
"path": "../sdk-coin-zeta" | ||
}, | ||
{ | ||
"path": "../sdk-core" | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
node_modules | ||
.idea | ||
public | ||
dist | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules/ | ||
.idea/ | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.nyc_output/ | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
printWidth: 120 | ||
singleQuote: true | ||
trailingComma: 'es5' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']+)$/; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
85
modules/sdk-coin-zeta/src/lib/transactionBuilderFactory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.