Skip to content

Commit

Permalink
Merge pull request #29 from leapwallet/main
Browse files Browse the repository at this point in the history
Lint fixes and packages bump
  • Loading branch information
leapsamvel committed Aug 26, 2023
2 parents 2d68fd5 + 6b2ccad commit def7204
Show file tree
Hide file tree
Showing 19 changed files with 1,388 additions and 4,301 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/publish-packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ jobs:
node-version: 16
registry-url: 'https://registry.npmjs.org'
- run: yarn
env:
env:
GH_TOKEN: ${{secrets.NODE_AUTH_TOKEN}}
- run: yarn publish:provider
env:
GH_TOKEN: ${{secrets.NODE_AUTH_TOKEN}}
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
- run: yarn publish:snap
- run: yarn lint && yarn test && yarn publish:snap
env:
GH_TOKEN: ${{secrets.NODE_AUTH_TOKEN}}
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

9 changes: 0 additions & 9 deletions lerna.json

This file was deleted.

17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@
"packages/*"
],
"scripts": {
"bootstrap": "yarn --ignore-engines",
"build": "yarn workspaces foreach --parallel --topological --verbose run build",
"build:provider": "yarn workspace @leapwallet/cosmos-snap-provider build",
"build:snap": "yarn workspace @leapwallet/metamask-cosmos-snap build",
"lint": "yarn lint:eslint && yarn lint:misc --check",
"lint:eslint": "eslint . --cache --ext js,ts",
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write",
"lint:misc": "prettier '**/*.json' '**/*.md' '!**/CHANGELOG.md' '**/*.yml' --ignore-path .gitignore",
"publish:provider": "yarn workspace @leapwallet/cosmos-snap-provider publish:package",
"publish:snap": "yarn workspace @leapwallet/metamask-cosmos-snap publish:package",
"start": "yarn workspaces foreach --parallel --interlaced --verbose run start",
"start:snap": "yarn workspace snap start",
"build:provider": "yarn workspace @leapwallet/cosmos-snap-provider build",
"build:snap": "yarn workspace @leapwallet/metamask-cosmos-snap build",
"publish:snap": "yarn workspace @leapwallet/snap publish:package",
"publish:provider": "yarn workspace @leapwallet/cosmos-snap-provider publish:package",
"test": "echo \"TODO\"",
"bootstrap": "yarn --ignore-engines",
"publish:ci": "lerna publish from-package --yes"
"test": "yarn workspace @leapwallet/metamask-cosmos-snap test"
},
"dependencies": {
"long": "^5.2.3"
},
"devDependencies": {
"@metamask/eslint-config": "^10.0.0",
Expand All @@ -46,7 +48,6 @@
"eslint-plugin-jsdoc": "^39.2.9",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.2.1",
"lerna": "^7.1.5",
"patch-package": "^7.0.0",
"postinstall-postinstall": "^2.1.0",
"prettier": "^2.2.1",
Expand Down
52 changes: 14 additions & 38 deletions packages/cosmos-snap-provider/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ This method is used to detect if the Leap Cosmos Snap is installed within the us
**Usage:**

```js

import { getSnap } from '@leapwallet/cosmos-snap-provider';
const snapInstalled = await getSnap(); // Returns true if the snap is already installed
```
Expand All @@ -28,7 +27,6 @@ const snapInstalled = await getSnap();
if (!snapInstalled) {
connectSnap(); // Initiates installation if not already present
}

```

### 3. **getKey**
Expand All @@ -39,34 +37,14 @@ if (!snapInstalled) {
const key = await getKey(chainId);
```


### 4. **Sugest Chain**

Utilize the **`suggestChain`** method to suggest any chains of coinTypes in the list below

```javascript
[
118,
564,
494,
639,
483,
4444,
701,
990,
394,
852,
7777777,
459,
880,
931,
371,
505,
529,
234,
330,
5555,
370
118, 564, 494, 639, 483, 4444, 701, 990, 394, 852, 7777777, 459, 880, 931,
371, 505, 529, 234, 330, 5555, 370,
];
```

Expand All @@ -75,15 +53,15 @@ Utilize the **`suggestChain`** method to suggest any chains of coinTypes in the
```javascript
import { suggestChain } from '@leapwallet/cosmos-snap-provider';
await suggestChain({
"chainId": "coreum-mainnet-1",
"chainName": "coreum",
"bech32Config": {
"bech32PrefixAccAddr": "core"
},
"bip44": {
"coinType": 990
}
})
chainId: 'coreum-mainnet-1',
chainName: 'coreum',
bech32Config: {
bech32PrefixAccAddr: 'core',
},
bip44: {
coinType: 990,
},
});
```

### 4. **cosmjsOfflineSigner**
Expand All @@ -93,20 +71,18 @@ If you're already employing cosmjs libraries for transaction signing, **`cosmjsO
**Usage:**

```js

import { SigningStargateClient } from '@cosmjs/cosmwasm-stargate';
import { GasPrice } from '@cosmjs/stargate';
import { cosmjsOfflineSigner } from '@leapwallet/cosmos-snap-provider';

const offlineSigner = new cosmjsOfflineSigner(chainId);
const accounts = await offlineSigner.getAccounts();
const rpcUrl = ""; // Populate with an RPC URL corresponding to the given chainId
const rpcUrl = ''; // Populate with an RPC URL corresponding to the given chainId
const stargateClient = await SigningStargateClient.connectWithSigner(
rpcUrl,
offlineSigner,
{
gasPrice: GasPrice.fromString("0.0025ujuno"),
}
gasPrice: GasPrice.fromString('0.0025ujuno'),
},
);

```
26 changes: 11 additions & 15 deletions packages/cosmos-snap-provider/package.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
{
"version": "0.0.6",
"name": "@leapwallet/cosmos-snap-provider",
"packageManager": "[email protected]",
"files": [
"dist/**/*"
],
"main": "dist/index.js",
"version": "0.0.7",
"repository": {
"url": "[email protected]:leapwallet/cosmos-metamask-snap.git"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"license": "(MIT-0 OR Apache-2.0)",
"main": "dist/index.js",
"files": [
"dist/**/*"
],
"scripts": {
"bootstrap": "yarn --ignore-engines",
"build": "npx tsc",
"start": "npx tsc watch",
"prepublish": "yarn build",
"bootstrap": "yarn --ignore-engines",
"publish:package": "npm publish",
"publish:ci": "lerna publish from-package --yes"
"start": "npx tsc watch"
},
"dependencies": {
"@cosmjs/amino": "^0.31.0",
"@cosmjs/proto-signing": "^0.31.0"
"@cosmjs/proto-signing": "^0.31.0",
"long": "^5.2.3"
},
"devDependencies": {
"cosmjs-types": "^0.8.0",
"typescript": "^5.2.2"
}
},
"packageManager": "[email protected]"
}
40 changes: 32 additions & 8 deletions packages/cosmos-snap-provider/src/cosmjs-offline-signer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
/* eslint jsdoc/match-description: 0 */ // --> OFF
import { SignDoc } from 'cosmjs-types/cosmos/tx/v1beta1/tx';
import { AccountData, AminoSignResponse, StdSignDoc } from '@cosmjs/amino';
import { DirectSignResponse, OfflineDirectSigner } from '@cosmjs/proto-signing';
import { getKey, requestSignAmino, requestSignature } from './snap';

export class cosmjsOfflineSigner implements OfflineDirectSigner {
constructor(private chainId: string) {}
export class CosmjsOfflineSigner implements OfflineDirectSigner {
readonly chainId: string;

constructor(chainId: string) {
this.chainId = chainId;
}

async getAccounts(): Promise<AccountData[]> {
const key = await getKey(this.chainId);
Expand Down Expand Up @@ -60,35 +65,54 @@ export class cosmjsOfflineSigner implements OfflineDirectSigner {
}

/**
* Gives the cosmos Offline signer
*
* @param chainId
* @param chainId - chainId
* @returns CosmjsOfflineSigner
*/
export function getOfflineSigner(chainId: string) {
return new cosmjsOfflineSigner(chainId);
return new CosmjsOfflineSigner(chainId);
}

/**
* Helps to do signArbitrary of the data provided
*
* @param chainId - chainId
* @param signer - signer
* @param data - data
* @returns signature
*/
export async function signArbitrary(
chainId: string,
signer: string,
data: string,
) {
const { signDoc, isADR36WithString } = getADR36SignDoc(signer, data);
const { signDoc } = getADR36SignDoc(signer, data);
const result = await requestSignAmino(chainId, signer, signDoc, {
isADR36: true,
});
return result.signature;
}

/**
*
* Gets the getADR36SignDoc of the signer and data
*
* @param signer - signer
* @param data - data
* @returns SignDoc and isADR36WithString
*/
function getADR36SignDoc(
signer: string,
data: string | Uint8Array,
): { signDoc: StdSignDoc; isADR36WithString: boolean } {
let isADR36WithString = false;
let b64Data = '';
if (typeof data === 'string') {
data = Buffer.from(data).toString('base64');
b64Data = Buffer.from(data).toString('base64');
isADR36WithString = true;
} else {
data = Buffer.from(data).toString('base64');
b64Data = Buffer.from(data).toString('base64');
}
const signDoc = {
chain_id: '',
Expand All @@ -103,7 +127,7 @@ function getADR36SignDoc(
type: 'sign/MsgSignData',
value: {
signer,
data,
b64Data,
},
},
],
Expand Down
39 changes: 22 additions & 17 deletions packages/cosmos-snap-provider/src/snap.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AccountData } from '@cosmjs/amino';
/* eslint @typescript-eslint/prefer-optional-chain: 0 */ // --> OFF
import { AccountData, StdSignDoc, AminoSignResponse } from '@cosmjs/amino';
import Long from 'long';
import { defaultSnapOrigin } from './config';
import { ChainInfo, GetSnapsResponse, Snap } from './types';
import { StdSignDoc, AminoSignResponse } from '@cosmjs/amino';

/**
* Get the installed snaps in MetaMask.
Expand Down Expand Up @@ -78,39 +78,42 @@ export const requestSignature = async (
});

const { accountNumber } = signDoc;
// @ts-ignore
if (
!accountNumber ||
!accountNumber.low ||
!accountNumber.unsigned ||
!accountNumber.high
) {
throw new Error('Invalid account Number, It should be a long format');
}

const modifiedAccountNumber = new Long(
accountNumber!.low,
accountNumber!.high,
accountNumber!.unsigned,
accountNumber.low,
accountNumber.high,
accountNumber.unsigned,
);

const modifiedSignature = {
// @ts-ignore
signature: signature.signature,
signed: {
// @ts-ignore
...signature.signed,
accountNumber: `${modifiedAccountNumber.toString()}`,
authInfoBytes: new Uint8Array(
// @ts-ignore
Object.values(signature.signed.authInfoBytes),
),

bodyBytes: new Uint8Array(
// @ts-ignore
Object.values(signature.signed.bodyBytes),
),
bodyBytes: new Uint8Array(Object.values(signature.signed.bodyBytes)),
},
};

return modifiedSignature;
};

export const requestSignAmino = async (
chainId: string,
signerAddress: string,
signDoc: StdSignDoc,
{ isADR36: isADR36 = false } = {},
{ isADR36 = false } = {},
) => {
const signResponse = (await window.ethereum.request({
method: 'wallet_invokeSnap',
Expand Down Expand Up @@ -148,23 +151,25 @@ export const getKey = async (chainId: string): Promise<AccountData> => {
if (!accountData) {
throw new Error('No account data found');
}
// @ts-ignore

accountData.pubkey = Uint8Array.from(Object.values(accountData.pubkey));

return accountData as AccountData;
};

export const isLocalSnap = (snapId: string) => snapId.startsWith('local:');

export const suggestChain = async (chainInfo: ChainInfo): Promise<{ message: string, chainInfo: ChainInfo }> => {
export const suggestChain = async (
chainInfo: ChainInfo,
): Promise<{ message: string; chainInfo: ChainInfo }> => {
return await window.ethereum.request({
method: 'wallet_invokeSnap',
params: {
snapId: defaultSnapOrigin,
request: {
method: 'suggestChain',
params: {
chainInfo
chainInfo,
},
},
},
Expand Down
Loading

0 comments on commit def7204

Please sign in to comment.