Skip to content

Commit

Permalink
feat: proto namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
kimurayu45z committed Jul 19, 2021
1 parent 583dfac commit 59f62a3
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 110 deletions.
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ npm install --save cosmos-client
## Examples

```typescript
import { cosmosclient, rest, cosmos } from 'cosmos-client';
import { cosmosclient, rest, proto } from '../../..';

describe('bank', () => {
it('send', async () => {
expect.hasAssertions();

const sdk = new cosmosclient.CosmosSDK('http://localhost:1317', 'testchain');

const privKey = new cosmosclient.secp256k1.PrivKey({
const privKey = new proto.cosmos.crypto.secp256k1.PrivKey({
key: await cosmosclient.generatePrivKeyFromMnemonic('joke door law post fragile cruel torch silver siren mechanic flush surround'),
});
const pubKey = privKey.pubKey();
Expand All @@ -36,28 +36,28 @@ describe('bank', () => {
.then((res) => res.data.account && cosmosclient.codec.unpackCosmosAny(res.data.account))
.catch((_) => undefined);

if (!(account instanceof cosmos.auth.v1beta1.BaseAccount)) {
if (!(account instanceof proto.cosmos.auth.v1beta1.BaseAccount)) {
console.log(account);
return;
}

// build tx
const msgSend = new cosmos.bank.v1beta1.MsgSend({
const msgSend = new proto.cosmos.bank.v1beta1.MsgSend({
from_address: fromAddress.toString(),
to_address: toAddress.toString(),
amount: [{ denom: 'token', amount: '10' }],
amount: [{ denom: 'token', amount: '1' }],
});

const txBody = new cosmos.tx.v1beta1.TxBody({
const txBody = new proto.cosmos.tx.v1beta1.TxBody({
messages: [cosmosclient.codec.packAny(msgSend)],
});
const authInfo = new cosmos.tx.v1beta1.AuthInfo({
const authInfo = new proto.cosmos.tx.v1beta1.AuthInfo({
signer_infos: [
{
public_key: cosmosclient.codec.packAny(pubKey),
mode_info: {
single: {
mode: cosmos.tx.signing.v1beta1.SignMode.SIGN_MODE_DIRECT,
mode: proto.cosmos.tx.signing.v1beta1.SignMode.SIGN_MODE_DIRECT,
},
},
sequence: account.sequence,
Expand All @@ -79,8 +79,11 @@ describe('bank', () => {
mode: rest.cosmos.tx.BroadcastTxMode.Block,
});
console.log(res);

expect(res.data.tx_response?.raw_log?.match('failed')).toBeFalsy();
});
});

```

## For library developlers
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "cosmos-client",
"description": "REST API Client for Cosmos SDK Blockchain",
"version": "0.42.11",
"version": "0.42.12",
"author": "LCNEM, Inc.",
"bugs": {
"url": "https://github.com/cosmos-client/cosmos-client-ts/issues"
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './rest';
export * from './websocket';
export * from './proto';
export * as proto from './proto';
export * as cosmosclient from './module';
14 changes: 7 additions & 7 deletions src/rest/cosmos/bank/bank.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { cosmosclient, rest, cosmos } from '../../..';
import { cosmosclient, rest, proto } from '../../..';

describe('bank', () => {
it('send', async () => {
expect.hasAssertions();

const sdk = new cosmosclient.CosmosSDK('http://localhost:1317', 'testchain');

const privKey = new cosmosclient.secp256k1.PrivKey({
const privKey = new proto.cosmos.crypto.secp256k1.PrivKey({
key: await cosmosclient.generatePrivKeyFromMnemonic('joke door law post fragile cruel torch silver siren mechanic flush surround'),
});
const pubKey = privKey.pubKey();
Expand All @@ -23,28 +23,28 @@ describe('bank', () => {
.then((res) => res.data.account && cosmosclient.codec.unpackCosmosAny(res.data.account))
.catch((_) => undefined);

if (!(account instanceof cosmos.auth.v1beta1.BaseAccount)) {
if (!(account instanceof proto.cosmos.auth.v1beta1.BaseAccount)) {
console.log(account);
return;
}

// build tx
const msgSend = new cosmos.bank.v1beta1.MsgSend({
const msgSend = new proto.cosmos.bank.v1beta1.MsgSend({
from_address: fromAddress.toString(),
to_address: toAddress.toString(),
amount: [{ denom: 'token', amount: '1' }],
});

const txBody = new cosmos.tx.v1beta1.TxBody({
const txBody = new proto.cosmos.tx.v1beta1.TxBody({
messages: [cosmosclient.codec.packAny(msgSend)],
});
const authInfo = new cosmos.tx.v1beta1.AuthInfo({
const authInfo = new proto.cosmos.tx.v1beta1.AuthInfo({
signer_infos: [
{
public_key: cosmosclient.codec.packAny(pubKey),
mode_info: {
single: {
mode: cosmos.tx.signing.v1beta1.SignMode.SIGN_MODE_DIRECT,
mode: proto.cosmos.tx.signing.v1beta1.SignMode.SIGN_MODE_DIRECT,
},
},
sequence: account.sequence,
Expand Down
12 changes: 6 additions & 6 deletions src/types/address/address.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as crypto from 'crypto';
import { cosmosclient } from '../..';
import { proto, cosmosclient } from '../..';

describe('address', () => {
it('ed25519', () => {
expect.hasAssertions();

const bytes = new Uint8Array(crypto.randomBytes(32));
const key = new cosmosclient.ed25519.PrivKey({ key: bytes });
const key = new proto.cosmos.crypto.ed25519.PrivKey({ key: bytes });
const address = cosmosclient.AccAddress.fromPublicKey(key.pubKey());
const str = address.toString();
const revived = cosmosclient.AccAddress.fromString(str);
Expand All @@ -22,7 +22,7 @@ describe('address', () => {
expect.hasAssertions();

const bytes = new Uint8Array(crypto.randomBytes(32));
const key = new cosmosclient.secp256k1.PrivKey({ key: bytes });
const key = new proto.cosmos.crypto.secp256k1.PrivKey({ key: bytes });
const address = cosmosclient.AccAddress.fromPublicKey(key.pubKey());
const str = address.toString();
const revived = cosmosclient.AccAddress.fromString(str);
Expand All @@ -38,10 +38,10 @@ describe('address', () => {
expect.hasAssertions();
const privKeyStr = 'ef40ea14839c3ee5690336bb1f032870941dbb329fc0553132a4a109a022a391';

const privKey1 = new cosmosclient.ed25519.PrivKey({
const privKey1 = new proto.cosmos.crypto.ed25519.PrivKey({
key: Buffer.from(privKeyStr, 'hex'),
});
const privKey2 = new cosmosclient.secp256k1.PrivKey({
const privKey2 = new proto.cosmos.crypto.secp256k1.PrivKey({
key: Buffer.from(privKeyStr, 'hex'),
});

Expand All @@ -58,7 +58,7 @@ describe('address', () => {
expect.hasAssertions();
const privKeyStr = 'ef40ea14839c3ee5690336bb1f032870941dbb329fc0553132a4a109a022a391';

const privKey = new cosmosclient.secp256k1.PrivKey({
const privKey = new proto.cosmos.crypto.secp256k1.PrivKey({
key: Buffer.from(privKeyStr, 'hex'),
});

Expand Down
19 changes: 9 additions & 10 deletions src/types/address/prefix.spec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import * as crypto from 'crypto';
import { cosmosclient } from '../..';
import { cosmosclient, proto } from '../..';
import { setBech32Prefix } from './config';

describe('address', () => {
it('prefix', async () => {
expect.hasAssertions();

setBech32Prefix({
accAddr: "jpyx",
accPub: "jpyxpub",
valAddr: "jpyxvaloper",
valPub: "jpyxvaloperpub",
consAddr: "jpyxvalcons",
consPub: "jpyxvalconspub",
accAddr: 'jpyx',
accPub: 'jpyxpub',
valAddr: 'jpyxvaloper',
valPub: 'jpyxvaloperpub',
consAddr: 'jpyxvalcons',
consPub: 'jpyxvalconspub',
});
const privKey = new cosmosclient.secp256k1.PrivKey({
const privKey = new proto.cosmos.crypto.secp256k1.PrivKey({
key: await cosmosclient.generatePrivKeyFromMnemonic('joke door law post fragile cruel torch silver siren mechanic flush surround'),
});
const pubKey = privKey.pubKey();
const address = cosmosclient.AccAddress.fromPublicKey(pubKey);

expect(address.toString().startsWith("jpyx")).toBeTruthy();
expect(address.toString().startsWith('jpyx')).toBeTruthy();
});
});
12 changes: 5 additions & 7 deletions src/types/codec/codec.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { codec } from '.';
import { cosmos } from '../../proto';
import '../..';
import { proto, cosmosclient } from '../..';

describe('codec', () => {
it('unpack', () => {
Expand Down Expand Up @@ -57,15 +55,15 @@ describe('codec', () => {
},
};

const unpacked = codec.unpackCosmosAny(res.data.account);
if (!(unpacked instanceof cosmos.auth.v1beta1.BaseAccount)) {
const unpacked = cosmosclient.codec.unpackCosmosAny(res.data.account);
if (!(unpacked instanceof proto.cosmos.auth.v1beta1.BaseAccount)) {
throw Error('');
}

console.log(unpacked);

const key = codec.unpackAny(unpacked.pub_key);
console.log(key)
const key = cosmosclient.codec.unpackAny(unpacked.pub_key);
console.log(key);

expect(true).toBeTruthy();
});
Expand Down
4 changes: 2 additions & 2 deletions src/types/crypto/ed25519.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as crypto from 'crypto';
import { cosmosclient } from '../..';
import { proto, cosmosclient } from '../..';

describe('ed25519', () => {
const bytes = crypto.randomBytes(32);
const key = new cosmosclient.ed25519.PrivKey({ key: bytes });
const key = new proto.cosmos.crypto.ed25519.PrivKey({ key: bytes });
const address = cosmosclient.AccAddress.fromPublicKey(key.pubKey());
const str = address.toString();

Expand Down
59 changes: 35 additions & 24 deletions src/types/crypto/ed25519.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,45 @@ import * as nacl from 'tweetnacl';
import { PrivKey as BasePrivKey, PubKey as BasePubKey } from './key';
import { cosmos } from '../../proto';

export class PrivKey extends cosmos.crypto.ed25519.PrivKey implements BasePrivKey {
bytes() {
return new Uint8Array(this.key);
declare module '../../proto' {
namespace cosmos {
namespace crypto {
namespace ed25519 {
interface PrivKey extends BasePrivKey { }
interface PubKey extends BasePubKey { }
}
}
}
}

sign(message: Uint8Array) {
const keypair = nacl.sign.keyPair.fromSeed(this.key);
return nacl.sign(new Uint8Array(message), new Uint8Array(keypair.secretKey));
}
// PrivKey

pubKey() {
const keypair = nacl.sign.keyPair.fromSeed(new Uint8Array(this.key));
cosmos.crypto.ed25519.PrivKey.prototype.bytes = function () {
return new Uint8Array(this.key);
};

return new PubKey({ key: new Uint8Array(keypair.publicKey) });
}
}
cosmos.crypto.ed25519.PrivKey.prototype.sign = function (message: Uint8Array) {
const keypair = nacl.sign.keyPair.fromSeed(this.key);
return nacl.sign(new Uint8Array(message), new Uint8Array(keypair.secretKey));
};

export class PubKey extends cosmos.crypto.ed25519.PubKey implements BasePubKey {
bytes() {
return new Uint8Array(this.key);
}
cosmos.crypto.ed25519.PrivKey.prototype.pubKey = function () {
const keypair = nacl.sign.keyPair.fromSeed(new Uint8Array(this.key));

verify(_: Uint8Array, sig: Uint8Array) {
return nacl.sign.open(new Uint8Array(sig), new Uint8Array(this.key)) !== null;
}
return new cosmos.crypto.ed25519.PubKey({ key: new Uint8Array(keypair.publicKey) });
};

address() {
const hash = crypto.createHash('sha256').update(this.key).digest();
return new Uint8Array(hash.subarray(0, 20));
}
}
// PubKey

cosmos.crypto.ed25519.PubKey.prototype.bytes = function () {
return new Uint8Array(this.key);
};

cosmos.crypto.ed25519.PubKey.prototype.verify = function (_: Uint8Array, sig: Uint8Array) {
return nacl.sign.open(new Uint8Array(sig), new Uint8Array(this.key)) !== null;
};

cosmos.crypto.ed25519.PubKey.prototype.address = function () {
const hash = crypto.createHash('sha256').update(this.key).digest();
return new Uint8Array(hash.subarray(0, 20));
};
15 changes: 7 additions & 8 deletions src/types/crypto/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { codec } from '../codec';
import * as ed25519 from './ed25519';
import * as secp256k1 from './secp256k1';
import { cosmos } from '../../proto';
import './ed25519';
import './secp256k1';

export * from './key';
export * as ed25519 from './ed25519';
export * as secp256k1 from './secp256k1';

codec.register('/cosmos.crypto.ed25519.PrivKey', ed25519.PrivKey);
codec.register('/cosmos.crypto.ed25519.PubKey', ed25519.PubKey);
codec.register('/cosmos.crypto.secp256k1.PrivKey', secp256k1.PrivKey);
codec.register('/cosmos.crypto.secp256k1.PubKey', secp256k1.PubKey);
codec.register('/cosmos.crypto.ed25519.PrivKey', cosmos.crypto.ed25519.PrivKey);
codec.register('/cosmos.crypto.ed25519.PubKey', cosmos.crypto.ed25519.PubKey);
codec.register('/cosmos.crypto.secp256k1.PrivKey', cosmos.crypto.secp256k1.PrivKey);
codec.register('/cosmos.crypto.secp256k1.PubKey', cosmos.crypto.secp256k1.PubKey);
4 changes: 2 additions & 2 deletions src/types/crypto/secp256k1.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as crypto from 'crypto';
import { cosmosclient } from '../..';
import { proto, cosmosclient } from '../..';
import * as secp256k1 from 'tiny-secp256k1';

describe('secp256k1', () => {
it('verify', () => {
expect.hasAssertions();
const bytes = crypto.randomBytes(32);
const key = new cosmosclient.secp256k1.PrivKey({ key: bytes });
const key = new proto.cosmos.crypto.secp256k1.PrivKey({ key: bytes });
const pubkey = key.pubKey();
const address = cosmosclient.AccAddress.fromPublicKey(pubkey);
const str = address.toString();
Expand Down
Loading

0 comments on commit 59f62a3

Please sign in to comment.