Merge pull request #63 from confio/blub
Add some address derivation fun to CLI
This commit is contained in:
commit
0a4ee557a9
@ -75,6 +75,17 @@ const signedTx: types.StdTx = {
|
||||
const postResult = await client.postTx(marshalTx(signedTx));
|
||||
```
|
||||
|
||||
## Other example codes
|
||||
|
||||
### Create random mnemonic and Cosmos address
|
||||
|
||||
```ts
|
||||
const mnemonic = Bip39.encode(Random.getBytes(16)).toString();
|
||||
const pen = await Secp256k1Pen.fromMnemonic(mnemonic);
|
||||
const pubkey = encodeSecp256k1Pubkey(pen.pubkey);
|
||||
const address = encodeAddress(pubkey, "cosmos");
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
This package is part of the cosmwasm-js repository, licensed under the Apache
|
||||
|
||||
8
packages/cli/examples/generate_address.ts
Normal file
8
packages/cli/examples/generate_address.ts
Normal file
@ -0,0 +1,8 @@
|
||||
const mnemonic = Bip39.encode(Random.getBytes(16)).toString();
|
||||
const pen = await Secp256k1Pen.fromMnemonic(mnemonic);
|
||||
const pubkey = encodeSecp256k1Pubkey(pen.pubkey);
|
||||
const address = encodeAddress(pubkey, "cosmos");
|
||||
|
||||
console.info("mnemonic:", mnemonic);
|
||||
console.info("pubkey:", pubkey);
|
||||
console.info("address:", address);
|
||||
@ -41,6 +41,8 @@ export function main(originalArgs: readonly string[]): void {
|
||||
[
|
||||
"@cosmwasm/sdk",
|
||||
[
|
||||
"encodeAddress",
|
||||
"encodeSecp256k1Pubkey",
|
||||
"encodeSecp256k1Signature",
|
||||
"makeSignBytes",
|
||||
"marshalTx",
|
||||
@ -95,9 +97,14 @@ export function main(originalArgs: readonly string[]): void {
|
||||
}
|
||||
}
|
||||
console.info(colors.yellow(" * helper functions"));
|
||||
console.info(colors.yellow(" - toAscii"));
|
||||
console.info(colors.yellow(" - fromAscii"));
|
||||
console.info(colors.yellow(" - fromBase64"));
|
||||
console.info(colors.yellow(" - fromHex"));
|
||||
console.info(colors.yellow(" - fromUtf8"));
|
||||
console.info(colors.yellow(" - toAscii"));
|
||||
console.info(colors.yellow(" - toBase64"));
|
||||
console.info(colors.yellow(" - toHex"));
|
||||
console.info(colors.yellow(" - toUtf8"));
|
||||
|
||||
let init = `
|
||||
import * as http from 'http';
|
||||
@ -110,7 +117,7 @@ export function main(originalArgs: readonly string[]): void {
|
||||
}
|
||||
// helper functions
|
||||
init += `
|
||||
const { toAscii, fromHex, toHex } = Encoding;
|
||||
const { fromAscii, fromBase64, fromHex, fromUtf8, toAscii, toBase64, toHex, toUtf8 } = Encoding;
|
||||
`;
|
||||
|
||||
if (args.selftest) {
|
||||
@ -121,9 +128,13 @@ export function main(originalArgs: readonly string[]): void {
|
||||
const hexHash = toHex(hash);
|
||||
export class NewDummyClass {};
|
||||
|
||||
const pen = await Secp256k1Pen.fromMnemonic(
|
||||
"zebra slush diet army arrest purpose hawk source west glimpse custom record",
|
||||
);
|
||||
const encoded = toHex(toUtf8(toBase64(toAscii("hello world"))));
|
||||
const decoded = fromAscii(fromBase64(fromUtf8(fromHex(encoded))));
|
||||
|
||||
const mnemonic = Bip39.encode(Random.getBytes(16)).toString();
|
||||
const pen = await Secp256k1Pen.fromMnemonic(mnemonic);
|
||||
const pubkey = encodeSecp256k1Pubkey(pen.pubkey);
|
||||
const address = encodeAddress(pubkey, "cosmos");
|
||||
const data = Encoding.toAscii("foo bar");
|
||||
const signature = await pen.createSignature(data);
|
||||
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
import { Secp256k1 } from "@iov/crypto";
|
||||
import { Encoding } from "@iov/encoding";
|
||||
|
||||
import { Msg, NonceInfo, pubkeyType, StdFee, StdSignature, StdTx } from "./types";
|
||||
import { encodeSecp256k1Pubkey } from "./pubkey";
|
||||
import { Msg, NonceInfo, StdFee, StdSignature, StdTx } from "./types";
|
||||
|
||||
const { toBase64, toUtf8 } = Encoding;
|
||||
|
||||
@ -61,10 +62,7 @@ export function makeSignBytes(
|
||||
export function encodeSecp256k1Signature(pubkey: Uint8Array, signature: Uint8Array): StdSignature {
|
||||
return {
|
||||
// eslint-disable-next-line @typescript-eslint/camelcase
|
||||
pub_key: {
|
||||
type: pubkeyType.secp256k1,
|
||||
value: toBase64(Secp256k1.compressPubkey(pubkey)),
|
||||
},
|
||||
pub_key: encodeSecp256k1Pubkey(pubkey),
|
||||
// Recovery seems to be unused
|
||||
signature: toBase64(Secp256k1.trimRecoveryByte(signature)),
|
||||
};
|
||||
|
||||
@ -7,3 +7,4 @@ export { unmarshalTx } from "./decoding";
|
||||
export { encodeSecp256k1Signature, makeSignBytes, marshalTx } from "./encoding";
|
||||
export { RestClient, TxsResponse } from "./restclient";
|
||||
export { makeCosmoshubPath, Pen, PrehashType, Secp256k1Pen } from "./pen";
|
||||
export { encodeSecp256k1Pubkey } from "./pubkey";
|
||||
|
||||
27
packages/sdk/src/pubkey.spec.ts
Normal file
27
packages/sdk/src/pubkey.spec.ts
Normal file
@ -0,0 +1,27 @@
|
||||
import { Encoding } from "@iov/encoding";
|
||||
|
||||
import { encodeSecp256k1Pubkey } from "./pubkey";
|
||||
|
||||
const { fromBase64 } = Encoding;
|
||||
|
||||
describe("pubkey", () => {
|
||||
describe("encodeSecp256k1Pubkey", () => {
|
||||
it("encodes a full signature", () => {
|
||||
const pubkey = fromBase64("AtQaCqFnshaZQp6rIkvAPyzThvCvXSDO+9AzbxVErqJP");
|
||||
expect(encodeSecp256k1Pubkey(pubkey)).toEqual({
|
||||
type: "tendermint/PubKeySecp256k1",
|
||||
value: "AtQaCqFnshaZQp6rIkvAPyzThvCvXSDO+9AzbxVErqJP",
|
||||
});
|
||||
});
|
||||
|
||||
it("compresses uncompressed public keys", () => {
|
||||
const pubkey = fromBase64(
|
||||
"BE8EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQE7WHpoHoNswYeoFkuYpYSKK4mzFzMV/dB0DVAy4lnNU=",
|
||||
);
|
||||
expect(encodeSecp256k1Pubkey(pubkey)).toEqual({
|
||||
type: "tendermint/PubKeySecp256k1",
|
||||
value: "A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ",
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
11
packages/sdk/src/pubkey.ts
Normal file
11
packages/sdk/src/pubkey.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { Secp256k1 } from "@iov/crypto";
|
||||
import { Encoding } from "@iov/encoding";
|
||||
|
||||
import { PubKey, pubkeyType } from "./types";
|
||||
|
||||
export function encodeSecp256k1Pubkey(pubkey: Uint8Array): PubKey {
|
||||
return {
|
||||
type: pubkeyType.secp256k1,
|
||||
value: Encoding.toBase64(Secp256k1.compressPubkey(pubkey)),
|
||||
};
|
||||
}
|
||||
1
packages/sdk/types/index.d.ts
vendored
1
packages/sdk/types/index.d.ts
vendored
@ -6,3 +6,4 @@ export { unmarshalTx } from "./decoding";
|
||||
export { encodeSecp256k1Signature, makeSignBytes, marshalTx } from "./encoding";
|
||||
export { RestClient, TxsResponse } from "./restclient";
|
||||
export { makeCosmoshubPath, Pen, PrehashType, Secp256k1Pen } from "./pen";
|
||||
export { encodeSecp256k1Pubkey } from "./pubkey";
|
||||
|
||||
2
packages/sdk/types/pubkey.d.ts
vendored
Normal file
2
packages/sdk/types/pubkey.d.ts
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
import { PubKey } from "./types";
|
||||
export declare function encodeSecp256k1Pubkey(pubkey: Uint8Array): PubKey;
|
||||
Loading…
Reference in New Issue
Block a user