-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #166 from StarfilesFileSharing/alpha
Node incentive
- Loading branch information
Showing
1 changed file
with
52 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; |