Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk-coin-baby): add SDK support for babylon #5382

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
/modules/sdk-coin-atom/ @BitGo/ethalt-team
/modules/sdk-coin-avaxc/ @BitGo/ethalt-team
/modules/sdk-coin-avaxp/ @BitGo/ethalt-team
/modules/sdk-coin-baby/ @BitGo/ethalt-team
/modules/sdk-coin-bera/ @BitGo/ethalt-team
/modules/sdk-coin-bsc/ @BitGo/ethalt-team
/modules/sdk-coin-coredao/ @BitGo/ethalt-team
Expand Down
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ COPY --from=builder /tmp/bitgo/modules/sdk-coin-atom /var/modules/sdk-coin-atom/
COPY --from=builder /tmp/bitgo/modules/abstract-cosmos /var/modules/abstract-cosmos/
COPY --from=builder /tmp/bitgo/modules/sdk-coin-avaxc /var/modules/sdk-coin-avaxc/
COPY --from=builder /tmp/bitgo/modules/sdk-coin-avaxp /var/modules/sdk-coin-avaxp/
COPY --from=builder /tmp/bitgo/modules/sdk-coin-baby /var/modules/sdk-coin-baby/
COPY --from=builder /tmp/bitgo/modules/sdk-coin-eth /var/modules/sdk-coin-eth/
COPY --from=builder /tmp/bitgo/modules/sdk-coin-bera /var/modules/sdk-coin-bera/
COPY --from=builder /tmp/bitgo/modules/sdk-coin-bld /var/modules/sdk-coin-bld/
Expand Down Expand Up @@ -140,6 +141,7 @@ cd /var/modules/sdk-coin-atom && yarn link && \
cd /var/modules/abstract-cosmos && yarn link && \
cd /var/modules/sdk-coin-avaxc && yarn link && \
cd /var/modules/sdk-coin-avaxp && yarn link && \
cd /var/modules/sdk-coin-baby && yarn link && \
cd /var/modules/sdk-coin-eth && yarn link && \
cd /var/modules/sdk-coin-bera && yarn link && \
cd /var/modules/sdk-coin-bld && yarn link && \
Expand Down Expand Up @@ -220,6 +222,7 @@ RUN cd /var/bitgo-express && \
yarn link @bitgo/abstract-cosmos && \
yarn link @bitgo/sdk-coin-avaxc && \
yarn link @bitgo/sdk-coin-avaxp && \
yarn link @bitgo/sdk-coin-baby && \
yarn link @bitgo/sdk-coin-eth && \
yarn link @bitgo/sdk-coin-bera && \
yarn link @bitgo/sdk-coin-bld && \
Expand Down
1 change: 1 addition & 0 deletions modules/account-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@bitgo/sdk-coin-atom": "^13.1.15",
"@bitgo/sdk-coin-avaxc": "^5.2.9",
"@bitgo/sdk-coin-avaxp": "^5.0.58",
"@bitgo/sdk-coin-baby": "^1.0.0",
"@bitgo/sdk-coin-bera": "^2.3.3",
"@bitgo/sdk-coin-bld": "^3.0.28",
"@bitgo/sdk-coin-bsc": "^22.2.12",
Expand Down
5 changes: 5 additions & 0 deletions modules/account-lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ export { Coreum };
import * as Rune from '@bitgo/sdk-coin-rune';
export { Rune };

import * as Baby from '@bitgo/sdk-coin-baby';
export { Baby };

import * as Sol from '@bitgo/sdk-coin-sol';
export { Sol };

Expand Down Expand Up @@ -219,6 +222,8 @@ const coinBuilderMap = {
tcoredao: Coredao.TransactionBuilder,
apt: Apt.TransactionBuilder,
tapt: Apt.TransactionBuilder,
baby: Baby.TransactionBuilder,
tbaby: Baby.TransactionBuilder,
};

/**
Expand Down
1 change: 1 addition & 0 deletions modules/bitgo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@bitgo/sdk-coin-atom": "^13.1.15",
"@bitgo/sdk-coin-avaxc": "^5.2.9",
"@bitgo/sdk-coin-avaxp": "^5.0.58",
"@bitgo/sdk-coin-baby": "^1.0.0",
"@bitgo/sdk-coin-bch": "^2.1.9",
"@bitgo/sdk-coin-bcha": "^2.2.9",
"@bitgo/sdk-coin-bera": "^2.3.3",
Expand Down
4 changes: 4 additions & 0 deletions modules/bitgo/src/v2/coinFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
AvaxC,
AvaxCToken,
AvaxP,
Baby,
Bch,
Bera,
BeraToken,
Expand Down Expand Up @@ -83,6 +84,7 @@ import {
Tatom,
TavaxC,
TavaxP,
Tbaby,
Tbch,
Tbera,
Tbld,
Expand Down Expand Up @@ -153,6 +155,7 @@ function registerCoinConstructors(globalCoinFactory: CoinFactory): void {
globalCoinFactory.register('atom', Atom.createInstance);
globalCoinFactory.register('avaxc', AvaxC.createInstance);
globalCoinFactory.register('avaxp', AvaxP.createInstance);
globalCoinFactory.register('baby', Baby.createInstance);
globalCoinFactory.register('bch', Bch.createInstance);
globalCoinFactory.register('bcha', Bcha.createInstance);
globalCoinFactory.register('bera', Bera.createInstance);
Expand Down Expand Up @@ -209,6 +212,7 @@ function registerCoinConstructors(globalCoinFactory: CoinFactory): void {
globalCoinFactory.register('tatom', Tatom.createInstance);
globalCoinFactory.register('tavaxc', TavaxC.createInstance);
globalCoinFactory.register('tavaxp', TavaxP.createInstance);
globalCoinFactory.register('tbaby', Tbaby.createInstance);
globalCoinFactory.register('tbch', Tbch.createInstance);
globalCoinFactory.register('tbcha', Tbcha.createInstance);
globalCoinFactory.register('tbera', Tbera.createInstance);
Expand Down
2 changes: 2 additions & 0 deletions modules/bitgo/src/v2/coins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Arbeth, Tarbeth, ArbethToken } from '@bitgo/sdk-coin-arbeth';
import { Atom, Tatom } from '@bitgo/sdk-coin-atom';
import { AvaxC, AvaxCToken, TavaxC } from '@bitgo/sdk-coin-avaxc';
import { AvaxP, TavaxP } from '@bitgo/sdk-coin-avaxp';
import { Baby, Tbaby } from '@bitgo/sdk-coin-baby';
import { Bch, Tbch } from '@bitgo/sdk-coin-bch';
import { Bcha, Tbcha } from '@bitgo/sdk-coin-bcha';
import { Bera, Tbera, BeraToken } from '@bitgo/sdk-coin-bera';
Expand Down Expand Up @@ -64,6 +65,7 @@ export { Ada, Tada };
export { Atom, Tatom };
export { AvaxC, AvaxCToken, TavaxC };
export { AvaxP, TavaxP };
export { Baby, Tbaby };
export { Bch, Tbch };
export { Bera, Tbera, BeraToken };
export { Bsc, BscToken, Tbsc };
Expand Down
9 changes: 6 additions & 3 deletions modules/bitgo/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@
{
"path": "../sdk-api"
},
{
"path": "../sdk-hmac"
},
{
"path": "../sdk-coin-ada"
},
Expand All @@ -84,6 +81,9 @@
{
"path": "../sdk-coin-avaxp"
},
{
"path": "../sdk-coin-baby"
},
{
"path": "../sdk-coin-bch"
},
Expand Down Expand Up @@ -231,6 +231,9 @@
{
"path": "../sdk-core"
},
{
"path": "../sdk-hmac"
},
{
"path": "../sdk-test"
},
Expand Down
5 changes: 5 additions & 0 deletions modules/sdk-coin-baby/.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-baby/.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-baby/.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-baby/.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-baby/.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-baby/.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-baby/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# BitGo sdk-coin-baby

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-baby`.

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

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 { Baby } from '@bitgo/sdk-coin-baby';

const sdk = new BitGoAPI();

sdk.register('baby', Baby.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.
55 changes: 55 additions & 0 deletions modules/sdk-coin-baby/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"name": "@bitgo/sdk-coin-baby",
"version": "1.0.0",
"description": "BitGo SDK coin library for Babylon",
"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": ">=18 <21"
},
"repository": {
"type": "git",
"url": "https://github.com/BitGo/BitGoJS.git",
"directory": "modules/sdk-coin-baby"
},
"lint-staged": {
"*.{js,ts}": [
"yarn prettier --write",
"yarn eslint --fix"
]
},
"publishConfig": {
"access": "public"
},
"nyc": {
"extension": [
".ts"
]
},
"dependencies": {
"@bitgo/abstract-cosmos": "^11.2.15",
"@bitgo/sdk-core": "^28.19.1",
"@bitgo/statics": "^50.19.0",
"@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.58.1",
"@bitgo/sdk-test": "^8.0.64"
}
}
73 changes: 73 additions & 0 deletions modules/sdk-coin-baby/src/baby.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { CosmosCoin, CosmosKeyPair, GasAmountDetails } from '@bitgo/abstract-cosmos';
import { BaseCoin, BitGoBase, Environments } from '@bitgo/sdk-core';
import { BaseUnit, BaseCoin as StaticsBaseCoin, coins } from '@bitgo/statics';
import { KeyPair, TransactionBuilderFactory } from './lib';
import { GAS_AMOUNT, GAS_LIMIT } from './lib/constants';
import utils from './lib/utils';

/**
*
* Full Name: Babylon
* Website: https://babylonlabs.io/
* Docs: https://docs.babylonlabs.io/
* GitHub : https://github.com/babylonlabs-io/networks
*/
export class Baby extends CosmosCoin {
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 Baby(bitgo, staticsCoin);
}

/** @inheritDoc **/
getBaseFactor(): string | number {
return 1e6;
}

getBuilder(): TransactionBuilderFactory {
return new TransactionBuilderFactory(coins.get(this.getChain()));
}

/** @inheritDoc **/
isValidAddress(address: string): boolean {
return utils.isValidAddress(address) || utils.isValidValidatorAddress(address);
}

/** @inheritDoc **/
getDenomination(): string {
return BaseUnit.BABY;
}

/** @inheritDoc **/
getGasAmountDetails(): GasAmountDetails {
return {
gasAmount: GAS_AMOUNT,
gasLimit: GAS_LIMIT,
};
}

/** @inheritDoc **/
getKeyPair(publicKey: string): CosmosKeyPair {
return new KeyPair({ pub: publicKey });
}

/**
* Get the public node url from the Environments constant we have defined
*/
protected getPublicNodeUrl(): string {
return Environments[this.bitgo.getEnv()].babyNodeUrl;
}

getAddressFromPublicKey(pubKey: string): string {
return new KeyPair({ pub: pubKey }).getAddress();
}
}
4 changes: 4 additions & 0 deletions modules/sdk-coin-baby/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './lib';
export * from './baby';
export * from './tbaby';
export * from './register';
7 changes: 7 additions & 0 deletions modules/sdk-coin-baby/src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const validDenoms = ['baby', 'tbaby', 'ubbn'];
MohammedRyaan786 marked this conversation as resolved.
Show resolved Hide resolved
export const accountAddressRegex = /^(bbn)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38})$/;
export const validatorAddressRegex = /^(bbnvaloper)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38})$/;
export const contractAddressRegex = /^(bbn)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]+)$/;
Comment on lines +2 to +4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This regex doesn't seems to be correct. Ideally it should accept all alphanumeric characters in the address body of length 38.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rune and Atom too use the same regex.

The BECH32 algorithm is used to generate addresses. It doesn't use all the alphanumeric characters in order to avoids characters that are visually similar to reduce the chances of transcription errors.

export const ADDRESS_PREFIX = 'bbn';
export const GAS_AMOUNT = '7000';
export const GAS_LIMIT = 200000;
10 changes: 10 additions & 0 deletions modules/sdk-coin-baby/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 };
26 changes: 26 additions & 0 deletions modules/sdk-coin-baby/src/lib/keyPair.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { KeyPairOptions } from '@bitgo/sdk-core';
import { pubkeyToAddress } from '@cosmjs/amino';

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

/**
* Babylon 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,
},
ADDRESS_PREFIX
);
}
}
Loading
Loading