Skip to content

Commit

Permalink
add <GenerateKeypair/> component
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeesun Kim authored and Jeesun Kim committed Mar 13, 2024
1 parent d915bc8 commit 023d01d
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 19 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"next": "14.1.0",
"react": "^18",
"react-dom": "^18",
"stellar-sdk": "^11.2.2",
"zustand": "^4.5.1",
"zustand-querystring": "^0.0.19"
},
Expand Down
6 changes: 3 additions & 3 deletions src/app/(sidebar)/account/create/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import { Card, Link, Text, Button, Icon } from "@stellar/design-system";

import { GenerateKeypair } from "@/components/GenerateKeypair";

import "../styles.scss";

export default function CreateAccount() {
Expand All @@ -21,9 +23,7 @@ export default function CreateAccount() {
</Text>
</div>
<div className="Account__CTA">
<Button size="md" variant="tertiary">
Generate keypair
</Button>
<GenerateKeypair />
</div>
</div>
</Card>
Expand Down
11 changes: 11 additions & 0 deletions src/app/(sidebar)/account/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,15 @@
.CardText__button {
align-self: flex-start;
}

&__keypair {
display: flex;
align-items: flex-start;
flex-direction: column;
gap: pxToRem(16px);

.Input__side-element--right {
cursor: pointer;
}
}
}
57 changes: 57 additions & 0 deletions src/components/GenerateKeypair.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Button, Icon, Input } from "@stellar/design-system";
import { Keypair } from "stellar-sdk";
import { useStore } from "@/store/useStore";

export const GenerateKeypair = () => {
const { account } = useStore();

const generateKeypair = () => {
let keypair = Keypair.random();

account.updateKeypair({
publicKey: keypair.publicKey(),
secretKey: keypair.secret(),
});
};

return (
<div className="Account__keypair">
<Button size="md" variant="tertiary" onClick={generateKeypair}>
Generate keypair
</Button>

{account.keypair.publicKey && (
<Input
readOnly
id="generate-keypair-publickey"
fieldSize="md"
label="Public Key"
value={account.keypair.publicKey}
rightElement={
<Icon.Copy01
onClick={() => {
navigator.clipboard.writeText(account.keypair.publicKey);
}}
/>
}
/>
)}
{account.keypair.secretKey && (
<Input
readOnly
id="generate-keypair-secretkey"
fieldSize="md"
label="Secret Key"
value={account.keypair.secretKey}
rightElement={
<Icon.Copy01
onClick={() => {
navigator.clipboard.writeText(account.keypair.secretKey);
}}
/>
}
/>
)}
</div>
);
};
26 changes: 13 additions & 13 deletions src/store/createStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ export interface Store {
// Account
account: {
value: string;
nestedObject: {
nestedValue1: string;
nestedValue2: number;
keypair: {
publicKey: string;
secretKey: string;
};
update: (value: string) => void;
updateNested: (nestedVal: {
nestedValue1: string;
nestedValue2: number;
updateKeypair: (nestedVal: {
publicKey: string;
secretKey: string;
}) => void;
reset: () => void;
};
Expand All @@ -40,20 +40,20 @@ export const createStore = (options: CreateStoreOptions) =>
}),
account: {
value: "",
nestedObject: {
nestedValue1: "",
nestedValue2: 0,
keypair: {
publicKey: "",
secretKey: "",
},
update: (value: string) =>
set((state) => {
state.account.value = value;
}),
updateNested: (nestedVal: {
nestedValue1: string;
nestedValue2: number;
updateKeypair: (nestedVal: {
publicKey: string;
secretKey: string;
}) =>
set((state) => {
state.account.nestedObject = nestedVal;
state.account.keypair = nestedVal;
}),
reset: () =>
set((state) => {
Expand Down
Loading

0 comments on commit 023d01d

Please sign in to comment.