Skip to content

Commit

Permalink
feat(sdk-coin-islm): add Islamic Coin
Browse files Browse the repository at this point in the history
Ticket: WIN-490
  • Loading branch information
hitansh-madan committed Oct 9, 2023
1 parent cfacfc9 commit 86ab885
Show file tree
Hide file tree
Showing 48 changed files with 2,882 additions and 14 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ COPY --from=builder /tmp/bitgo/modules/sdk-coin-eth2 /var/modules/sdk-coin-eth2/
COPY --from=builder /tmp/bitgo/modules/sdk-coin-hash /var/modules/sdk-coin-hash/
COPY --from=builder /tmp/bitgo/modules/sdk-coin-hbar /var/modules/sdk-coin-hbar/
COPY --from=builder /tmp/bitgo/modules/sdk-coin-injective /var/modules/sdk-coin-injective/
COPY --from=builder /tmp/bitgo/modules/sdk-coin-islm /var/modules/sdk-coin-islm/
COPY --from=builder /tmp/bitgo/modules/sdk-coin-near /var/modules/sdk-coin-near/
COPY --from=builder /tmp/bitgo/modules/sdk-coin-osmo /var/modules/sdk-coin-osmo/
COPY --from=builder /tmp/bitgo/modules/sdk-coin-polygon /var/modules/sdk-coin-polygon/
Expand Down Expand Up @@ -129,6 +130,7 @@ cd /var/modules/sdk-coin-eth2 && yarn link && \
cd /var/modules/sdk-coin-hash && yarn link && \
cd /var/modules/sdk-coin-hbar && yarn link && \
cd /var/modules/sdk-coin-injective && yarn link && \
cd /var/modules/sdk-coin-islm && yarn link && \
cd /var/modules/sdk-coin-near && yarn link && \
cd /var/modules/sdk-coin-osmo && yarn link && \
cd /var/modules/sdk-coin-polygon && yarn link && \
Expand Down Expand Up @@ -192,6 +194,7 @@ RUN cd /var/bitgo-express && \
yarn link @bitgo/sdk-coin-hash && \
yarn link @bitgo/sdk-coin-hbar && \
yarn link @bitgo/sdk-coin-injective && \
yarn link @bitgo/sdk-coin-islm && \
yarn link @bitgo/sdk-coin-near && \
yarn link @bitgo/sdk-coin-osmo && \
yarn link @bitgo/sdk-coin-polygon && \
Expand Down
15 changes: 7 additions & 8 deletions modules/abstract-cosmos/src/cosmosCoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { bip32 } from '@bitgo/utxo-lib';
import { Coin } from '@cosmjs/stargate';
import { BigNumber } from 'bignumber.js';
import { Buffer } from 'buffer';
import { createHash, Hash, randomBytes } from 'crypto';
import { Hash, randomBytes } from 'crypto';
import * as _ from 'lodash';
import * as querystring from 'querystring';
import * as request from 'superagent';
Expand All @@ -43,7 +43,6 @@ import {
} from './lib';
import { ROOT_PATH } from './lib/constants';
import utils from './lib/utils';

/**
* Cosmos accounts support memo Id based addresses
*/
Expand Down Expand Up @@ -227,7 +226,7 @@ export class CosmosCoin extends BaseCoin {
// Step 7: Sign the tx
const signature = await this.signRecoveryTSS(userKeyCombined, backupKeyCombined, signableHex);
const signableBuffer = Buffer.from(signableHex, 'hex');
MPC.verify(signableBuffer, signature, createHash('sha256'));
MPC.verify(signableBuffer, signature, this.getHashFunction());
const cosmosKeyPair = this.getKeyPair(publicKey);
txnBuilder.addSignature({ pub: cosmosKeyPair.getKeys().pub }, Buffer.from(signature.r + signature.s, 'hex'));
const signedTransaction = await txnBuilder.build();
Expand Down Expand Up @@ -382,8 +381,8 @@ export class CosmosCoin extends BaseCoin {
const MESSAGE = Buffer.from(txHex, 'hex');

const [signA, signB] = [
MPC.sign(MESSAGE, signCombineOne.oShare, signCombineTwo.dShare, createHash('sha256')),
MPC.sign(MESSAGE, signCombineTwo.oShare, signCombineOne.dShare, createHash('sha256')),
MPC.sign(MESSAGE, signCombineOne.oShare, signCombineTwo.dShare, this.getHashFunction()),
MPC.sign(MESSAGE, signCombineTwo.oShare, signCombineOne.dShare, this.getHashFunction()),
];

return MPC.constructSignature([signA, signB]);
Expand Down Expand Up @@ -620,11 +619,11 @@ export class CosmosCoin extends BaseCoin {
}

/**
* Retrieves the SHA256 hash function.
* @returns {Hash} The SHA256 hash function.
* Retrieves the hash function.
* @returns {Hash} The hash function.
*/
getHashFunction(): Hash {
return createHash('sha256');
return utils.getHashFunction();
}

/**
Expand Down
10 changes: 10 additions & 0 deletions modules/abstract-cosmos/src/lib/iface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { TransactionExplanation as BaseTransactionExplanation, TransactionType } from '@bitgo/sdk-core';
import { Coin } from '@cosmjs/stargate';

export enum PubKeyTypeUrl {
secp256k1 = '/cosmos.crypto.secp256k1.PubKey',
ethSecp256k1 = '/ethermint.crypto.v1.ethsecp256k1.PubKey',
}

export enum PubKeyType {
secp256k1 = 'tendermint/PubKeySecp256k1',
ethSecp256k1 = 'tendermint/PubKeyEthSecp256k1',
}

export interface TransactionExplanation extends BaseTransactionExplanation {
type: TransactionType;
}
Expand Down
5 changes: 3 additions & 2 deletions modules/abstract-cosmos/src/lib/transactionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import {
TransactionType,
} from '@bitgo/sdk-core';
import { BaseCoin as CoinConfig } from '@bitgo/statics';
import { Secp256k1, sha256 } from '@cosmjs/crypto';
import { Secp256k1 } from '@cosmjs/crypto';
import { makeSignBytes } from '@cosmjs/proto-signing';
import BigNumber from 'bignumber.js';
import { CosmosTransactionMessage, FeeData, MessageData } from './iface';
import { CosmosKeyPair as KeyPair } from './keyPair';
import { CosmosTransaction } from './transaction';
import { CosmosUtils } from './utils';
import { fromHex } from '@cosmjs/encoding';

export abstract class CosmosTransactionBuilder extends BaseTransactionBuilder {
protected _transaction: CosmosTransaction;
Expand Down Expand Up @@ -198,7 +199,7 @@ export abstract class CosmosTransactionBuilder extends BaseTransactionBuilder {
this._accountNumber,
this._chainId
);
const txnHash = sha256(makeSignBytes(signDoc));
const txnHash = fromHex(this._utils.getHashFunction().update(makeSignBytes(signDoc)).digest().toString('hex'));
const signature = await Secp256k1.createSignature(txnHash, privateKey);
const compressedSig = Buffer.concat([signature.r(), signature.s()]);
this.addSignature({ pub: this.transaction.cosmosLikeTransaction.publicKey }, compressedSig);
Expand Down
19 changes: 15 additions & 4 deletions modules/abstract-cosmos/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { SignDoc, TxRaw } from 'cosmjs-types/cosmos/tx/v1beta1/tx';
import { Any } from 'cosmjs-types/google/protobuf/any';
import { MsgExecuteContract } from 'cosmjs-types/cosmwasm/wasm/v1/tx';

import * as crypto from 'crypto';
import { Hash, createHash } from 'crypto';
import * as constants from './constants';
import {
CosmosLikeTransaction,
Expand Down Expand Up @@ -309,6 +309,10 @@ export class CosmosUtils implements BaseUtils {
}
}

getEncodedPubkey(pubkey: string): Any {
return encodePubkey(encodeSecp256k1Pubkey(fromHex(pubkey)));
}

/**
* Creates a txRaw from an cosmos like transaction @see CosmosLikeTransaction
* @Precondition cosmosLikeTransaction.publicKey must be defined
Expand All @@ -319,7 +323,7 @@ export class CosmosUtils implements BaseUtils {
if (!cosmosLikeTransaction.publicKey) {
throw new Error('publicKey is required to create a txRaw');
}
const encodedPublicKey: Any = encodePubkey(encodeSecp256k1Pubkey(fromHex(cosmosLikeTransaction.publicKey)));
const encodedPublicKey: Any = this.getEncodedPubkey(cosmosLikeTransaction.publicKey);
const messages = cosmosLikeTransaction.sendMessages as unknown as Any[];
let txBodyValue;
if (cosmosLikeTransaction.memo) {
Expand Down Expand Up @@ -597,8 +601,7 @@ export class CosmosUtils implements BaseUtils {
authInfoBytes: unsignedTx.authInfoBytes,
signatures: [signature],
});
hash = crypto
.createHash('sha256')
hash = createHash('sha256')
.update(TxRaw.encode(signedTx).finish())
.digest()
.toString('hex')
Expand Down Expand Up @@ -738,6 +741,14 @@ export class CosmosUtils implements BaseUtils {
this.validateAmountData(message.funds, transactionType);
}
}

/**
* Retrieves the hash function.
* @returns {Hash} The hash function.
*/
getHashFunction(): Hash {
return createHash('sha256');
}
}

const utils = new CosmosUtils();
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 @@ -42,6 +42,7 @@
"@bitgo/sdk-coin-hash": "^1.4.11",
"@bitgo/sdk-coin-hbar": "^1.5.11",
"@bitgo/sdk-coin-injective": "^1.4.11",
"@bitgo/sdk-coin-islm": "^1.0.0",
"@bitgo/sdk-coin-near": "^1.6.11",
"@bitgo/sdk-coin-osmo": "^1.6.11",
"@bitgo/sdk-coin-polygon": "^1.8.0",
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 @@ -86,6 +86,9 @@ export { Sei };
import * as Injective from '@bitgo/sdk-coin-injective';
export { Injective };

import * as Islm from '@bitgo/sdk-coin-islm';
export { Islm };

import * as Zeta from '@bitgo/sdk-coin-zeta';
export { Zeta };

Expand Down Expand Up @@ -160,6 +163,8 @@ const coinBuilderMap = {
tinjective: Injective.TransactionBuilderFactory,
zeta: Zeta.TransactionBuilderFactory,
tzeta: Zeta.TransactionBuilderFactory,
islm: Islm.TransactionBuilderFactory,
tislm: Islm.TransactionBuilderFactory,
};

/**
Expand Down
3 changes: 3 additions & 0 deletions modules/account-lib/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
{
"path": "../sdk-coin-injective"
},
{
"path": "../sdk-coin-islm"
},
{
"path": "../sdk-coin-near"
},
Expand Down
1 change: 1 addition & 0 deletions modules/bitgo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"@bitgo/sdk-coin-hash": "^1.4.11",
"@bitgo/sdk-coin-hbar": "^1.5.11",
"@bitgo/sdk-coin-injective": "^1.4.11",
"@bitgo/sdk-coin-islm": "^1.0.0",
"@bitgo/sdk-coin-ltc": "^2.2.11",
"@bitgo/sdk-coin-near": "^1.6.11",
"@bitgo/sdk-coin-osmo": "^1.6.11",
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 @@ -44,6 +44,7 @@ import {
Hash,
Hbar,
Injective,
Islm,
Ltc,
Ofc,
OfcToken,
Expand Down Expand Up @@ -84,6 +85,7 @@ import {
Thbar,
Tia,
Tinjective,
Tislm,
Tltc,
Tosmo,
Tpolygon,
Expand Down Expand Up @@ -140,6 +142,7 @@ function registerCoinConstructors(globalCoinFactory: CoinFactory): void {
globalCoinFactory.register('hbar', Hbar.createInstance);
globalCoinFactory.register('ltc', Ltc.createInstance);
globalCoinFactory.register('injective', Injective.createInstance);
globalCoinFactory.register('islm', Islm.createInstance);
globalCoinFactory.register('near', Near.createInstance);
globalCoinFactory.register('ofc', Ofc.createInstance);
globalCoinFactory.register('osmo', Osmo.createInstance);
Expand Down Expand Up @@ -178,6 +181,7 @@ function registerCoinConstructors(globalCoinFactory: CoinFactory): void {
globalCoinFactory.register('thash', Thash.createInstance);
globalCoinFactory.register('thbar', Thbar.createInstance);
globalCoinFactory.register('tinjective', Tinjective.createInstance);
globalCoinFactory.register('tislm', Tislm.createInstance);
globalCoinFactory.register('tltc', Tltc.createInstance);
globalCoinFactory.register('tnear', TNear.createInstance);
globalCoinFactory.register('tosmo', Tosmo.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 @@ -24,6 +24,7 @@ import { Ethw } from '@bitgo/sdk-coin-ethw';
import { Hash, Thash } from '@bitgo/sdk-coin-hash';
import { Hbar, Thbar } from '@bitgo/sdk-coin-hbar';
import { Injective, Tinjective } from '@bitgo/sdk-coin-injective';
import { Islm, Tislm } from '@bitgo/sdk-coin-islm';
import { Ltc, Tltc } from '@bitgo/sdk-coin-ltc';
import { Osmo, Tosmo } from '@bitgo/sdk-coin-osmo';
import { Polygon, PolygonToken, Tpolygon } from '@bitgo/sdk-coin-polygon';
Expand Down Expand Up @@ -76,6 +77,7 @@ export { Ton, Tton };
export { Bld, Tbld };
export { Sei, Tsei };
export { Injective, Tinjective };
export { Islm, Tislm };
export { Trx, Ttrx };
export { StellarToken, Txlm, Xlm };
export { Txrp, Xrp };
Expand Down
3 changes: 3 additions & 0 deletions modules/bitgo/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@
{
"path": "../sdk-coin-injective"
},
{
"path": "../sdk-coin-islm"
},
{
"path": "../sdk-coin-ltc"
},
Expand Down
5 changes: 5 additions & 0 deletions modules/sdk-coin-islm/.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-islm/.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-islm/.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-islm/.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-islm/.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-islm/.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-islm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# BitGo sdk-coin-islm

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

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

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

const sdk = new BitGoAPI();

sdk.register('islm', Islm.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.
Loading

0 comments on commit 86ab885

Please sign in to comment.