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-islm): add Islamic Coin #3968

Merged
merged 2 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,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 @@ -131,6 +132,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 @@ -195,6 +197,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: 6 additions & 9 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 Down Expand Up @@ -227,7 +227,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 +382,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()),
Copy link
Contributor

Choose a reason for hiding this comment

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

Please verify if OVC needs a change corresponding to this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it wouldn't, since this uses keccak256. And it seems in OVC we default to keccak256 unless explicitly added here

MPC.sign(MESSAGE, signCombineTwo.oShare, signCombineOne.dShare, this.getHashFunction()),
];

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

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

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

/**
* Defines the protobuf typeUrl for the public key
*/
export enum PubKeyTypeUrl {
secp256k1 = '/cosmos.crypto.secp256k1.PubKey',
ethSecp256k1 = '/ethermint.crypto.v1.ethsecp256k1.PubKey',
}

/**
* Defines the amino encoding name for the public key
*/
export enum PubKeyType {
secp256k1 = 'tendermint/PubKeySecp256k1',
ethSecp256k1 = 'tendermint/PubKeyEthSecp256k1',
}

export interface TransactionExplanation extends BaseTransactionExplanation {
type: TransactionType;
}
Expand Down
4 changes: 2 additions & 2 deletions modules/abstract-cosmos/src/lib/transactionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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';
Expand Down Expand Up @@ -198,7 +198,7 @@ export abstract class CosmosTransactionBuilder extends BaseTransactionBuilder {
this._accountNumber,
this._chainId
);
const txnHash = sha256(makeSignBytes(signDoc));
const txnHash = Uint8Array.from(this._utils.getHashFunction().update(makeSignBytes(signDoc)).digest());
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
25 changes: 21 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,16 @@ export class CosmosUtils implements BaseUtils {
}
}

/**
* Takes a hex encoded pubkey, converts it to the Amino JSON representation (type/value wrapper)
* and returns it as protobuf `Any`
* @param {string} pubkey hex encoded compressed secp256k1 public key
* @returns {Any} pubkey encoded as protobuf `Any`
*/
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 +329,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 +607,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 +747,14 @@ export class CosmosUtils implements BaseUtils {
this.validateAmountData(message.funds, transactionType);
}
}

/**
* Get coin specific 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 @@ -43,6 +43,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",
mmcshinsky-bitgo marked this conversation as resolved.
Show resolved Hide resolved
"@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 @@ -163,6 +166,8 @@ const coinBuilderMap = {
tinjective: Injective.TransactionBuilderFactory,
zeta: Zeta.TransactionBuilderFactory,
tzeta: Zeta.TransactionBuilderFactory,
islm: Islm.TransactionBuilderFactory,
tislm: Islm.TransactionBuilderFactory,
core: Core.TransactionBuilderFactory,
tcore: Core.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 @@ -55,6 +55,9 @@
{
"path": "../sdk-coin-injective"
},
{
"path": "../sdk-coin-islm"
},
{
"path": "../sdk-coin-near"
},
Expand Down
3 changes: 2 additions & 1 deletion modules/bitgo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"@bitgo/sdk-coin-btc": "^1.7.11",
"@bitgo/sdk-coin-btg": "^1.5.11",
"@bitgo/sdk-coin-celo": "^1.5.11",
"@bitgo/sdk-coin-core": "^1.0.0",
"@bitgo/sdk-coin-cspr": "^1.5.0",
"@bitgo/sdk-coin-dash": "^1.5.11",
"@bitgo/sdk-coin-doge": "^1.12.11",
Expand All @@ -73,7 +74,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-core": "^1.0.0",
"@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
6 changes: 5 additions & 1 deletion modules/bitgo/src/v2/coinFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
Hash,
Hbar,
Injective,
Islm,
Ltc,
Ofc,
OfcToken,
Expand All @@ -70,6 +71,7 @@ import {
Tbsv,
Tbtc,
Tcelo,
Tcore,
Tcspr,
Tdash,
Tdoge,
Expand All @@ -85,7 +87,7 @@ import {
Thbar,
Tia,
Tinjective,
Tcore,
Tislm,
Tltc,
Tosmo,
Tpolygon,
Expand Down Expand Up @@ -143,6 +145,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 @@ -182,6 +185,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 @@ -25,6 +25,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 @@ -78,6 +79,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 @@ -113,6 +113,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