This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: ✨ web: add Arconnect support * fix: 🐛 misc: fix basicTest
- Loading branch information
1 parent
549560a
commit eda7b0e
Showing
5 changed files
with
132 additions
and
111 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
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
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 |
---|---|---|
@@ -1,117 +1,128 @@ | ||
// import { ArconnectSigner } from "arbundles"; | ||
// import BigNumber from "bignumber.js"; | ||
// import crypto from "crypto"; | ||
// import type { CurrencyConfig, Tx } from "../../common/types"; | ||
// import base64url from "base64url"; | ||
// import BaseNodeCurrency from "../currency"; | ||
// import { Arweave } from "../utils"; | ||
import { ArconnectSigner } from "arbundles"; | ||
import BigNumber from "bignumber.js"; | ||
import crypto from "crypto"; | ||
import type { TokenConfig, Tx } from "../../common/types"; | ||
import base64url from "base64url"; | ||
import { Arweave } from "../utils"; | ||
|
||
// // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
// import type * as _ from "arconnect"; | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
import type * as _ from "arconnect"; | ||
import BaseWebToken from "../token"; | ||
|
||
// export default class ArweaveConfig extends BaseNodeCurrency { | ||
// protected declare providerInstance: Arweave; | ||
// opts?: { provider?: "arconnect" | "arweave.app"; network?: string }; | ||
// protected declare wallet: Window["arweaveWallet"]; | ||
// protected signerInstance: ArconnectSigner; | ||
// constructor(config: CurrencyConfig) { | ||
// super(config); | ||
// this.base = ["winston", 1e12]; | ||
// this.needsFee = true; | ||
// } | ||
export default class ArweaveConfig extends BaseWebToken { | ||
protected declare providerInstance: Arweave; | ||
public isSlow = true; | ||
opts?: { provider?: "arconnect" | "arweave.app"; network?: string }; | ||
protected declare wallet: Window["arweaveWallet"]; | ||
protected signerInstance: ArconnectSigner; | ||
constructor(config: TokenConfig) { | ||
super(config); | ||
this.base = ["winston", 1e12]; | ||
this.needsFee = true; | ||
} | ||
|
||
// private async getProvider(): Promise<Arweave> { | ||
// if (!this.providerInstance) { | ||
// const purl = new URL(this.providerUrl ?? "https://arweave.net"); | ||
// let config; | ||
// try { | ||
// config = this.wallet.getArweaveConfig(); | ||
// } catch (e) {} | ||
// this.providerInstance = Arweave.init( | ||
// config ?? { | ||
// host: purl.hostname, | ||
// protocol: purl.protocol.replaceAll(":", "").replaceAll("/", ""), | ||
// port: purl.port, | ||
// network: this?.opts?.network, | ||
// }, | ||
// ); | ||
// } | ||
// return this.providerInstance; | ||
// } | ||
private getProvider(): Arweave { | ||
if (!this.providerInstance) { | ||
const purl = new URL(this.providerUrl ?? "https://arweave.net"); | ||
// let config; | ||
// try { | ||
// config = this.wallet.getArweaveConfig(); | ||
// } catch (e) {} | ||
this.providerInstance = Arweave.init( | ||
/* config ?? */ { | ||
url: purl, | ||
network: this?.opts?.network, | ||
}, | ||
); | ||
} | ||
return this.providerInstance; | ||
} | ||
|
||
// async getTx(txId: string): Promise<Tx> { | ||
// const arweave = await this.getProvider(); | ||
// const txs = await arweave.transactions.getStatus(txId); | ||
// let tx; | ||
// if (txs.status === 200) { | ||
// tx = await arweave.transactions.get(txId); | ||
// } | ||
// const confirmed = txs.status !== 202 && (txs.confirmed?.number_of_confirmations ?? 0) >= this.minConfirm; | ||
// let owner; | ||
// if (tx?.owner) { | ||
// owner = this.ownerToAddress(tx.owner); | ||
// } | ||
// return { | ||
// from: owner ?? undefined, | ||
// to: tx?.target ?? undefined, | ||
// amount: new BigNumber(tx?.quantity ?? 0), | ||
// pending: txs.status === 202, | ||
// confirmed, | ||
// }; | ||
// } | ||
async getTx(txId: string): Promise<Tx> { | ||
const arweave = await this.getProvider(); | ||
const txs = await arweave.transactions.getStatus(txId); | ||
let tx; | ||
if (txs.status === 200) { | ||
tx = await arweave.transactions.get(txId); | ||
} | ||
const confirmed = txs.status !== 202 && (txs.confirmed?.number_of_confirmations ?? 0) >= this.minConfirm; | ||
let owner; | ||
if (tx?.owner) { | ||
owner = this.ownerToAddress(tx.owner); | ||
} | ||
return { | ||
from: owner ?? undefined, | ||
to: tx?.target ?? undefined, | ||
amount: new BigNumber(tx?.quantity ?? 0), | ||
pending: txs.status === 202, | ||
confirmed, | ||
}; | ||
} | ||
|
||
// ownerToAddress(owner: any): string { | ||
// return Arweave.utils.bufferTob64Url( | ||
// crypto | ||
// .createHash("sha256") | ||
// .update(Arweave.utils.b64UrlToBuffer(Buffer.isBuffer(owner) ? base64url(owner) : owner)) | ||
// .digest(), | ||
// ); | ||
// } | ||
ownerToAddress(owner: any): string { | ||
return Arweave.utils.bufferTob64Url( | ||
crypto | ||
.createHash("sha256") | ||
.update(Arweave.utils.b64UrlToBuffer(Buffer.isBuffer(owner) ? base64url(owner) : owner)) | ||
.digest(), | ||
); | ||
} | ||
|
||
// async sign(data: Uint8Array): Promise<Uint8Array> { | ||
// return this.getSigner().sign(data); | ||
// } | ||
async sign(data: Uint8Array): Promise<Uint8Array> { | ||
return this.getSigner().sign(data); | ||
} | ||
|
||
// getSigner(): ArconnectSigner { | ||
// if (this.signerInstance) return this.signerInstance; | ||
// switch (this?.opts?.provider ?? "arconnect") { | ||
// case "arconnect": | ||
// this.signerInstance = new ArconnectSigner(this.wallet); | ||
// } | ||
// return this.signerInstance; | ||
// } | ||
getSigner(): ArconnectSigner { | ||
if (this.signerInstance) return this.signerInstance; | ||
switch (this?.opts?.provider ?? "arconnect") { | ||
case "arconnect": | ||
this.signerInstance = new ArconnectSigner(this.wallet, this.getProvider()); | ||
} | ||
return this.signerInstance; | ||
} | ||
|
||
// async verify(pub: any, data: Uint8Array, signature: Uint8Array): Promise<boolean> { | ||
// if (Buffer.isBuffer(pub)) { | ||
// pub = pub.toString(); | ||
// } | ||
// return Arweave.crypto.verify(pub, data, signature); | ||
// } | ||
async verify(pub: any, data: Uint8Array, signature: Uint8Array): Promise<boolean> { | ||
if (Buffer.isBuffer(pub)) { | ||
pub = pub.toString(); | ||
} | ||
return this.getProvider().crypto.verify(pub, data, signature); | ||
} | ||
|
||
// async getCurrentHeight(): Promise<BigNumber> { | ||
// return (await this.getProvider()).network.getInfo().then((r) => new BigNumber(r.height)); | ||
// } | ||
async getCurrentHeight(): Promise<BigNumber> { | ||
return (await this.getProvider()).network.getInfo().then((r) => new BigNumber(r.height)); | ||
} | ||
|
||
// async getFee(amount: BigNumber.Value, to?: string): Promise<BigNumber> { | ||
// return new BigNumber(await (await this.getProvider()).transactions.getPrice(new BigNumber(amount).toNumber(), to)).integerValue( | ||
// BigNumber.ROUND_CEIL, | ||
// ); | ||
// } | ||
async getFee(amount: BigNumber.Value, to?: string): Promise<BigNumber> { | ||
return new BigNumber(await (await this.getProvider()).transactions.getPrice(new BigNumber(amount).toNumber(), to)).integerValue( | ||
BigNumber.ROUND_CEIL, | ||
); | ||
} | ||
|
||
// async sendTx(data: any): Promise<any> { | ||
// return await (await this.getProvider()).transactions.post(data); | ||
// } | ||
async sendTx(data: any): Promise<any> { | ||
return await (await this.getProvider()).transactions.post(data); | ||
} | ||
|
||
// async createTx(amount: BigNumber.Value, to: string, fee?: string): Promise<{ txId: string | undefined; tx: any }> { | ||
// const arweave = await this.getProvider(); | ||
// const tx = await this.wallet.sign(await arweave.createTransaction({ quantity: new BigNumber(amount).toString(), reward: fee, target: to })); | ||
// return { txId: tx.id, tx }; | ||
// } | ||
async createTx(amount: BigNumber.Value, to: string, fee?: string): Promise<{ txId: string | undefined; tx: any }> { | ||
const arweave = await this.getProvider(); | ||
const atx = await arweave.createTransaction({ quantity: new BigNumber(amount).toString(), reward: fee?.toString(), target: to }); | ||
// @ts-expect-error override | ||
atx.merkle = undefined; | ||
// @ts-expect-error override | ||
atx.deepHash = undefined; | ||
// @ts-expect-error types | ||
const tx = await this.wallet.sign(atx); | ||
return { txId: tx.id, tx }; | ||
} | ||
|
||
// async getPublicKey(): Promise<string> { | ||
// const signer = this.getSigner(); | ||
// await signer.setPublicKey(); | ||
// return Arweave.utils.bufferTob64Url(signer.publicKey); | ||
// } | ||
// } | ||
async getPublicKey(): Promise<string> { | ||
const signer = this.getSigner(); | ||
await signer.setPublicKey(); | ||
return Arweave.utils.bufferTob64Url(signer.publicKey); | ||
} | ||
|
||
public async ready(): Promise<void> { | ||
const pubKey = await this.getPublicKey(); | ||
const address = this.ownerToAddress(pubKey); | ||
this._address = address; | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
import { createData, DataItem, deepHash, stringToBuffer, getCryptoDriver, bundleAndSignData /* Arweave */ } from "arbundles/web"; | ||
export { createData, DataItem, deepHash, stringToBuffer, getCryptoDriver, bundleAndSignData /* Arweave */ }; | ||
import { createData, DataItem, deepHash, stringToBuffer, getCryptoDriver, bundleAndSignData, Arweave } from "arbundles/web"; | ||
export { createData, DataItem, deepHash, stringToBuffer, getCryptoDriver, bundleAndSignData, Arweave }; |