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: Support Panacea v2 based on Cosmos v0.42 Stargate #37

Merged
merged 12 commits into from
Jul 20, 2021
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
41 changes: 32 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,43 @@ on:
pull_request: # for all PRs regardless of its base branch

jobs:
build:
build-test:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
env:
CHAIN_ID: testing
MNEMONIC: ${{ secrets.TEST_MNEMONIC }}

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}

- name: Use Node.js 14.x
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build --if-present
- run: npm test
node-version: 14.x

- name: Install dependencies
run: yarn install

- name: Build
run: yarn build

- name: Run a panacea-core docker container
run: |
docker run --rm -d \
-e CHAIN_ID="${CHAIN_ID}" \
-e MNEMONIC="${MNEMONIC}" \
-p 26657:26657 \
-v $(pwd)/scripts:/root/scripts \
--name core \
ghcr.io/medibloc/panacea-core:master \
bash /root/scripts/panacea-core/init.sh

wget -qO- https://raw.githubusercontent.com/eficode/wait-for/v2.1.2/wait-for | sh -s -- localhost:26657 -t 30

- name: Run tests
env:
PANACEAD_ENABLED: true
TENDERMINT_URL: http://localhost:26657
run: yarn test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ docs/_build

#Logs
npm-debug.log*

third_party/proto/
11 changes: 0 additions & 11 deletions .npmignore

This file was deleted.

61 changes: 42 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,58 @@
# panacea-js
# Panacea Javascript SDK

Official client-side JavaScript library for the [medibloc blockchain](https://github.com/medibloc/panacea-core).
The `panacea-js` is the official [Panacea](https://github.com/medibloc/panacea-core) Javascript SDK written in Typescript, powered by [CosmJS](https://github.com/cosmos/cosmjs).

## Install
The `panacea-js` extends the CosmJS in order to provide [Panacea-specific features](https://github.com/medibloc/panacea-core#key-features) (AOL, DID, and so on).
So, it exposes CosmJS basic functions as they are, such as `connectWithSigner` and `sendTokens`.

## Usage

### Installation

```bash
npm install @medibloc/panacea-js
yarn add @medibloc/panacea-js @cosmjs/proto-signing @cosmjs/stargate
```

Please refers to the [documentation](https://panacea-js.readthedocs.io/en/latest/)
### Examples

A list of examples can be found at the [example.md](docs/examples.md).

## Test
## Contribution

Install dependencies and build the project.
```bash
npm test
yarn install
yarn build
```

## License
To run simple unit tests,
```bash
yarn test
````

To run integration tests with [panacea-core](https://github.com/medibloc/panacea-core), start a `panacea-core` daemon first.
```bash
docker run --rm -d \
-e CHAIN_ID="chain-1" \
-e MNEMONIC="..." \
-p 26657:26657 \
-v $(pwd)/scripts:/root/scripts \
--name core \
ghcr.io/medibloc/panacea-core:master \
bash /root/scripts/panacea-core/init.sh
```
Copyright 2018 MediBloc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Then, the integration tests can be run with the following environment variables.
```bash
PANACEAD_ENABLED=true \
TENDERMINT_URL="http://localhost:26657" \
CHAIN_ID="chain-1" \
MNEMONIC="..." \
yarn test
```

http://www.apache.org/licenses/LICENSE-2.0
For more details, please see the [CI script](.github/workflows/ci.yml).

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```
## License

[Apache-2.0 License](LICENSE)
137 changes: 137 additions & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# Examples

## Sending tokens

```ts
import { panaceaWalletOpts, SigningPanaceaClient } from "@medibloc/panacea-js";
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { assertIsBroadcastTxSuccess } from "@cosmjs/stargate";

const mnemonic = "bulb rail ...";
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, panaceaWalletOpts);
const [firstAccount] = await wallet.getAccounts();

const tendermintRpcEndpoint = "http://localhost:26657";
const client = await SigningPanaceaClient.connectWithSigner(tendermintRpcEndpoint, wallet);

const recipient = "panacea1e7hl0w63tankr6pefugk96wvr6s990z350t6xs";
const amount = {
denom: "umed",
amount: "123456789",
};
const result = await client.sendTokens(firstAccount.address, recipient, [amount], "memo");
assertIsBroadcastTxSuccess(result);
```

## Creating AOL topics and adding records

```ts
import { panaceaWalletOpts, SigningPanaceaClient } from "@medibloc/panacea-js";
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { assertIsBroadcastTxSuccess } from "@cosmjs/stargate";
import { TextEncoder } from "util";
import Long from "long";

const mnemonic = "bulb rail ...";
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, panaceaWalletOpts);
const [firstAccount] = await wallet.getAccounts();
console.log(firstAccount);

const tendermintRpcEndpoint = "http://localhost:26657";
const client = await SigningPanaceaClient.connectWithSigner(tendermintRpcEndpoint, wallet);

const ownerAddress = firstAccount.address;
const topicName = "topic-1"
let result = await client.createTopic(ownerAddress, topicName, "description", "memo");
assertIsBroadcastTxSuccess(result);

const topic = await client.getPanaceaClient().getTopic(ownerAddress, topicName);
console.log(topic);

const writerAddress = ownerAddress;
result = await client.addWriter(ownerAddress, topicName, writerAddress, "moniker", "description", "memo");
assertIsBroadcastTxSuccess(result);

const writer = await client.getPanaceaClient().getWriter(ownerAddress, topicName, writerAddress);
console.log(writer);

// Encode key and value as you want
const key = new TextEncoder().encode("key1");
const value = new TextEncoder().encode("value1");
result = await client.addRecord(ownerAddress, topicName, key, value, writerAddress, "memo");
assertIsBroadcastTxSuccess(result);

const record = await client.getPanaceaClient().getRecord(ownerAddress, topicName, Long.fromInt(0));
console.log(record);
```

If you want to use a `feePayerAddress` along with the `writerAddress`, please use the `GroupSigningPanaceaClient`.
```ts
import { GroupSigningPanaceaClient } from "@medibloc/panacea-js";

// Some lines were skipped ...

const feePayerWallet = await DirectSecp256k1HdWallet.fromMnemonic("...", panaceaWalletOpts);
const writerWallet = await DirectSecp256k1HdWallet.fromMnemonic("...", panaceaWalletOpts);

const tendermintRpcEndpoint = "http://localhost:26657";
const client = await GroupSigningPanaceaClient.connectWithSigner(tendermintRpcEndpoint, [feePayerWallet, writerWallet]);

const result = await client.addRecordWithFeePayer(ownerAddress, topicName, key, value, writerAddress, feePayerAddress, "");
```

## Creating DIDs

```ts
import { panaceaWalletOpts, SigningPanaceaClient, Secp256k1, DidUtil } from "@medibloc/panacea-js";
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { assertIsBroadcastTxSuccess } from "@cosmjs/stargate";

const mnemonic = "bulb rail ...";
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, panaceaWalletOpts);
const [ firstAccount ] = await wallet.getAccounts();
console.log(firstAccount);

const tendermintRpcEndpoint = "http://localhost:26657";
const client = await SigningPanaceaClient.connectWithSigner(tendermintRpcEndpoint, wallet);

const privKey = Secp256k1.generatePrivateKey();
const pubKeyCompressed = Secp256k1.getPublicKeyCompressed(privKey);

const did = DidUtil.getDid(pubKeyCompressed);
const verificationMethodId = `${ did }#key1`;
const didDocument = {
contexts: {
values: [ 'https://www.w3.org/ns/did/v1' ],
},
id: did,
controller: undefined,
verificationMethods: [
{
id: verificationMethodId,
type: 'EcdsaSecp256k1VerificationKey2019',
controller: did,
publicKeyBase58: DidUtil.getPublicKeyBase58(pubKeyCompressed),
},
],
authentications: [
{
verificationMethodId: verificationMethodId,
verificationMethod: undefined,
}
],
assertionMethods: [],
keyAgreements: [],
capabilityInvocations: [],
capabilityDelegations: [],
services: [],
};

const signature = DidUtil.signDidDocument(privKey, didDocument);

const result = await client.createDid(didDocument, verificationMethodId, signature, firstAccount.address);
assertIsBroadcastTxSuccess(result);

const didDocumentWithSeq = await client.getPanaceaClient().getDid(didDocument.id);
console.log(didDocumentWithSeq);
```
65 changes: 0 additions & 65 deletions example/aol.js

This file was deleted.

Loading