Merge pull request #15 from webmaster128/chain-aware-addresses

Allow configuring address prefix for multisig address and examples
This commit is contained in:
samepant
2022-01-11 09:47:39 -05:00
committed by GitHub
8 changed files with 47 additions and 7 deletions
+2 -1
View File
@@ -1,4 +1,5 @@
FAUNADB_SECRET=
NEXT_PUBLIC_NODE_ADDRESS=https://cosmoshub.validator.network:443
NEXT_PUBLIC_DENOM=uatom
NEXT_PUBLIC_CHAIN_ID=cosmoshub-4
NEXT_PUBLIC_CHAIN_ID=cosmoshub-4
NEXT_PUBLIC_ADDRESS_PREFIX=cosmos
+2 -1
View File
@@ -5,6 +5,7 @@ import { withRouter } from "next/router";
import Button from "../inputs/Button";
import StackableContainer from "../layout/StackableContainer";
import Input from "../inputs/Input";
import { exampleAddress } from "../../lib/displayHelpers";
class FindMultisigForm extends React.Component {
constructor(props) {
@@ -45,7 +46,7 @@ class FindMultisigForm extends React.Component {
value={this.state.address}
label="Multisig Address"
name="address"
placeholder="cosmos1vqpjljwsynsn58dugz0w8ut7kun7t8ls2qkmsq"
placeholder={exampleAddress()}
/>
<Button
label="Use this Multisig"
+3 -2
View File
@@ -8,6 +8,7 @@ import { createMultisigFromCompressedSecp256k1Pubkeys } from "../../lib/multisig
import Input from "../inputs/Input";
import StackableContainer from "../layout/StackableContainer";
import ThresholdInput from "../inputs/ThresholdInput";
import { exampleAddress, examplePubkey } from "../../lib/displayHelpers";
let emptyPubKeyGroup = () => {
return { address: "", compressedPubkey: "", keyError: "", isPubkey: false };
@@ -161,8 +162,8 @@ class MultiSigForm extends React.Component {
width="100%"
placeholder={
pubkeyGroup.isPubkey
? "Akd/qKMWdZXyiMnSu6aFLpQEGDO0ijyal9mXUIcVaPNX"
: "cosmos1vqpjljwsynsn58dugz0w8ut7kun7t8ls2qkmsq"
? examplePubkey(index)
: exampleAddress(index)
}
error={pubkeyGroup.keyError}
onBlur={(e) => {
+2 -1
View File
@@ -7,6 +7,7 @@ import { withRouter } from "next/router";
import Button from "../../components/inputs/Button";
import Input from "../../components/inputs/Input";
import StackableContainer from "../layout/StackableContainer";
import { exampleAddress } from "../../lib/displayHelpers";
class TransactionForm extends React.Component {
constructor(props) {
@@ -84,7 +85,7 @@ class TransactionForm extends React.Component {
value={this.state.toAddress}
onChange={this.handleChange}
error={this.state.addressError}
placeholder="cosmos1fjrzd7ycxzse05zme3r2zqwpsvcrskv80wj82h"
placeholder={exampleAddress()}
/>
</div>
<div className="form-item">
+35 -1
View File
@@ -1,3 +1,5 @@
import { Bech32, fromBase64, toBase64 } from "@cosmjs/encoding";
import { sha512 } from "@cosmjs/crypto";
import { Decimal } from "@cosmjs/math";
/**
@@ -42,4 +44,36 @@ const printableCoins = (coins) => {
return printableCoin(coins[0]);
}
export { abbreviateLongString, printableCoin, printableCoins };
/**
* Generates an example address for the configured blockchain.
*
* `index` can be set to a small integer in order to get different addresses. Defaults to 0.
*/
const exampleAddress = (index) => {
const usedIndex = index || 0;
let data = Bech32.decode("cosmos1vqpjljwsynsn58dugz0w8ut7kun7t8ls2qkmsq").data;
for (let i = 0; i < usedIndex; ++i) {
data = sha512(data).slice(0, data.length); // hash one time and trim to original length
}
return Bech32.encode(process.env.NEXT_PUBLIC_ADDRESS_PREFIX, data);
}
/**
* Generates an example pubkey (secp256k1, compressed).
*
* `index` can be set to a small integer in order to get different addresses. Defaults to 0.
*
* Note: the keys are not necessarily valid (as in points on the chain) and should only be used
* as dummy data.
*/
const examplePubkey = (index) => {
const usedIndex = index || 0;
let data = fromBase64("Akd/qKMWdZXyiMnSu6aFLpQEGDO0ijyal9mXUIcVaPNX");
for (let i = 0; i < usedIndex; ++i) {
data = sha512(data).slice(0, data.length); // hash one time and trim to original length
data[0] = index % 2 ? 0x02 : 0x03; // pubkeys have to start with 0x02 or 0x03
}
return toBase64(data);
}
export { abbreviateLongString, printableCoin, printableCoins, exampleAddress, examplePubkey };
+1 -1
View File
@@ -24,7 +24,7 @@ const createMultisigFromCompressedSecp256k1Pubkeys = async (
};
});
const multisigPubkey = createMultisigThresholdPubkey(pubkeys, threshold);
const multisigAddress = pubkeyToAddress(multisigPubkey, "cosmos");
const multisigAddress = pubkeyToAddress(multisigPubkey, process.env.NEXT_PUBLIC_ADDRESS_PREFIX);
// save multisig to fauna
const multisig = {
+1
View File
@@ -7,6 +7,7 @@
"name": "create-next-example-app",
"dependencies": {
"@cosmjs/amino": "^0.27.0",
"@cosmjs/crypto": "^0.27.0",
"@cosmjs/encoding": "^0.27.0",
"@cosmjs/math": "^0.27.0",
"@cosmjs/proto-signing": "^0.27.0",
+1
View File
@@ -7,6 +7,7 @@
},
"dependencies": {
"@cosmjs/amino": "^0.27.0",
"@cosmjs/crypto": "^0.27.0",
"@cosmjs/encoding": "^0.27.0",
"@cosmjs/math": "^0.27.0",
"@cosmjs/proto-signing": "^0.27.0",