-
Notifications
You must be signed in to change notification settings - Fork 282
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-baby): add SDK support for babylon
TICKET: COIN-2761
- Loading branch information
1 parent
ad33065
commit db6ec7e
Showing
36 changed files
with
1,434 additions
and
6 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
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
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 |
---|---|---|
@@ -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-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. |
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,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" | ||
} | ||
} |
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,74 @@ | ||
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'; | ||
|
||
/** | ||
* Class representing the Babylon coin. | ||
* | ||
* 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(); | ||
} | ||
} |
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 './lib'; | ||
export * from './baby'; | ||
export * from './tbaby'; | ||
export * from './register'; |
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,7 @@ | ||
export const validDenoms = ['baby', 'tbaby', 'ubbn']; | ||
export const accountAddressRegex = /^(bbn)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38})$/; | ||
export const validatorAddressRegex = /^(bbnvaloper)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38})$/; | ||
export const contractAddressRegex = /^(bbn)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]+)$/; | ||
export const ADDRESS_PREFIX = 'bbn'; | ||
export const GAS_AMOUNT = '7000'; | ||
export const GAS_LIMIT = 200000; |
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,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 | ||
); | ||
} | ||
} |
Oops, something went wrong.