Skip to content

Commit

Permalink
Merge pull request #166 from StarfilesFileSharing/alpha
Browse files Browse the repository at this point in the history
Node incentive
  • Loading branch information
QuixThe2nd authored Nov 9, 2024
2 parents 0e49f30 + 92712d5 commit 8e0ba75
Showing 1 changed file with 52 additions and 53 deletions.
105 changes: 52 additions & 53 deletions src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,62 @@ import { createPublicClient, createWalletClient, http, parseEther, publicActions
import { privateKeyToAccount } from "npm:viem/accounts";
import { sepolia } from "npm:viem/chains";
import type Hydrafiles from "./hydrafiles.ts";
import { ERR_AMBIGUOUS_ARGUMENT } from "https://deno.land/[email protected]/node/internal/errors.ts";

export type EthAddress = `0x${string}`;

class Wallet {
private _client: Hydrafiles;
account: ReturnType<typeof privateKeyToAccount>;
client: ReturnType<typeof createWalletClient> & ReturnType<typeof createPublicClient>;

private constructor(client: Hydrafiles, privateKey: EthAddress) {
this._client = client;
this.account = privateKeyToAccount(privateKey);
this.client = createWalletClient({
account: this.account,
chain: sepolia,
transport: http(),
}).extend(publicActions);
}

public static async init(client: Hydrafiles): Promise<Wallet> {
const keyFilePath = "eth.key";

if (!await client.fs.exists(keyFilePath)) await client.fs.writeFile(keyFilePath, new TextEncoder().encode(Wallet.generateEthPrivateKey()));

const fileContent = await client.fs.readFile(keyFilePath);
const key = fileContent !== false ? new TextDecoder().decode(fileContent) as EthAddress : Wallet.generateEthPrivateKey();

return new Wallet(client, key);
}

public static generateEthPrivateKey(): EthAddress {
const privateKey = new Uint8Array(32);
crypto.getRandomValues(privateKey);

return ("0x" + Array.from(privateKey)
.map((byte) => byte.toString(16).padStart(2, "0"))
.join("")) as EthAddress;
}

public async balance(): Promise<number> {
const balanceWei = await this.client.getBalance({ address: this.account.address });
return parseFloat(balanceWei.toString()) / 1e18;
}

public async transfer(to: EthAddress, amount: number): Promise<void> {
const hash = await this.client.sendTransaction({
account: this.account,
chain: sepolia,
to,
value: parseEther(String(amount)),
});
console.log("Transaction Hash:", hash);
}

public address(): EthAddress {
return this.account.address;
}
private _client: Hydrafiles;
account: ReturnType<typeof privateKeyToAccount>;
client: ReturnType<typeof createWalletClient> & ReturnType<typeof createPublicClient>;

private constructor(client: Hydrafiles, privateKey: EthAddress) {
this._client = client;
this.account = privateKeyToAccount(privateKey);
this.client = createWalletClient({
account: this.account,
chain: sepolia,
transport: http(),
}).extend(publicActions);
}

public static async init(client: Hydrafiles): Promise<Wallet> {
const keyFilePath = "eth.key";

if (!await client.fs.exists(keyFilePath)) await client.fs.writeFile(keyFilePath, new TextEncoder().encode(Wallet.generateEthPrivateKey()));

const fileContent = await client.fs.readFile(keyFilePath);
const key = fileContent !== false ? new TextDecoder().decode(fileContent) as EthAddress : Wallet.generateEthPrivateKey();

return new Wallet(client, key);
}

public static generateEthPrivateKey(): EthAddress {
const privateKey = new Uint8Array(32);
crypto.getRandomValues(privateKey);

return ("0x" + Array.from(privateKey)
.map((byte) => byte.toString(16).padStart(2, "0"))
.join("")) as EthAddress;
}

public async balance(): Promise<number> {
const balanceWei = await this.client.getBalance({ address: this.account.address });
return parseFloat(balanceWei.toString()) / 1e18;
}

public async transfer(to: EthAddress, amount: number): Promise<void> {
const hash = await this.client.sendTransaction({
account: this.account,
chain: sepolia,
to,
value: parseEther(String(amount)),
});
console.log("Transaction Hash:", hash);
}

public address(): EthAddress {
return this.account.address;
}
}

export default Wallet;

0 comments on commit 8e0ba75

Please sign in to comment.