Skip to content

Commit

Permalink
fix(signer): remove use of JWK, simplify constructor (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Mar 18, 2024
2 parents 35ffab6 + f32f738 commit d2ef573
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 35 deletions.
8 changes: 5 additions & 3 deletions examples/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,14 @@ <h1 class="text-textPrimary w-full font-bold">View Distribution Data</h1>
</div>

<script type="module">
import Arweave from 'https://unpkg.com/arweave/bundles/web.bundle.js';
import { ArconnectSigner } from 'https://unpkg.com/arbundles@0.11.0/build/web/esm/webIndex.js';
import Arweave from 'https://unpkg.com/arweave@1.14.4/web/index.js';

import { ArIO } from './web.bundle.min.js';

const jwk = await Arweave.init({}).wallets.generate();
const arIO = new ArIO({ signer: jwk });
const arweave = Arweave.init({});
const signer = new ArconnectSigner(window['arweaveWallet'], arweave);
const arIO = new ArIO({ signer });

// fetch data on page load
async function init() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,4 @@
"warp-contracts": "^1.4.38",
"winston": "^3.11.0"
}
}
}
47 changes: 16 additions & 31 deletions src/common/ar-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
import { ArconnectSigner, ArweaveSigner } from 'arbundles';

import { ARNS_TESTNET_REGISTRY_TX } from '../constants.js';
import {
ArIOContract,
ArIOState,
Expand All @@ -34,13 +33,17 @@ import {
import { RemoteContract } from './contracts/remote-contract.js';

// TODO: append this with other configuration options (e.g. local vs. remote evaluation)
export type ContractConfiguration =
export type ArIOSigner = ArweaveSigner | ArconnectSigner;
export type ContractConfiguration = {
signer?: ArIOSigner; // TODO: optionally allow JWK in place of signer
} & (
| {
contract?: SmartWeaveContract<unknown>;
}
| {
contractTxId: string;
};
}
);

function isContractConfiguration<T>(
config: ContractConfiguration,
Expand All @@ -58,36 +61,18 @@ export class ArIO implements ArIOContract {
private contract: SmartWeaveContract<ArIOState>;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
private signer: ArweaveSigner | ArconnectSigner;
private signer: ArIOSigner | undefined;

constructor({
signer,
...config
}: ContractConfiguration & {
signer: ArweaveSigner | ArconnectSigner;
}) {
constructor({ signer, ...config }: ContractConfiguration) {
this.signer = signer;

const isContract = isContractConfiguration<ArIOState>(config);
const isContractTxId = isContractTxIdConfiguration(config);
const isBoth = isContract && isContractTxId;
switch (true) {
case isBoth:
throw new Error(
'ArIO contract configuration must include either `contract` or `contractTxId`, but not both',
);
case isContract:
this.contract = config.contract;
return;
case isContractTxId:
this.contract = new RemoteContract<ArIOState>({
contractTxId: config.contractTxId,
});
return;
default:
this.contract = new RemoteContract<ArIOState>({
contractTxId: ARNS_TESTNET_REGISTRY_TX,
});
if (isContractConfiguration<ArIOState>(config)) {
this.contract = config.contract;
} else if (isContractTxIdConfiguration(config)) {
this.contract = new RemoteContract<ArIOState>({
contractTxId: config.contractTxId,
});
} else {
throw new Error('Invalid configuration.');
}
}

Expand Down

0 comments on commit d2ef573

Please sign in to comment.