Skip to content

Commit

Permalink
fix(safety): update type safety checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Atticus committed Mar 25, 2024
1 parent 9c0540b commit 32eebbc
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 53 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"@types/sinon": "^10.0.15",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^6.4.0",
"crypto-browserify": "^3.12.0",
"esbuild": "^0.19.2",
"esbuild-plugin-polyfill-node": "^0.3.0",
"eslint": "^8.47.0",
Expand Down
8 changes: 2 additions & 6 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import {
ArconnectSigner,
ArweaveSigner,
DataItem,
Transaction,
} from 'arbundles';
import { ArconnectSigner, ArweaveSigner, DataItem } from 'arbundles';
import { Transaction } from 'warp-contracts';

import {
ANTRecord,
Expand Down
18 changes: 7 additions & 11 deletions src/common/contracts/warp-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { DataItem, Signer, Transaction } from 'arbundles';
import { DataItem, Signer } from 'arbundles';
import Arweave from 'arweave';
import {
Contract,
EvaluationOptions,
LoggerFactory,
Transaction,
Warp,
WarpFactory,
defaultCacheOptions,
} from 'warp-contracts';

import { defaultArweave } from '../../constants.js';
import {
BaseContract,
ContractSigner,
Expand All @@ -33,7 +35,7 @@ import {
WriteContract,
WriteParameters,
} from '../../types.js';
import { isDataItem, isTransaction } from '../../utils/arweave.js';
import { isTransaction } from '../../utils/arweave.js';
import { getContractManifest } from '../../utils/smartweave.js';
import { FailedRequestError, WriteInteractionError } from '../error.js';
import { DefaultLogger } from '../logger.js';
Expand All @@ -48,11 +50,7 @@ export class WarpContract<T>
private contract: Contract<T>;
private contractTxId: string;
private cacheUrl: string | undefined;
private arweave = Arweave.init({
host: 'arweave.net',
port: 443,
protocol: 'https',
});
private arweave = defaultArweave;
private warpEvaluationOptions: Partial<EvaluationOptions> | undefined;
private log = new DefaultLogger({
level: 'debug',
Expand All @@ -68,18 +66,16 @@ export class WarpContract<T>
},
true,
),
warpEvaluationOptions,
}: {
contractTxId: string;
cacheUrl?: string;
warp?: Warp;
signer?: ContractSigner;
warpEvaluationOptions?: Partial<EvaluationOptions>;
}) {
this.contractTxId = contractTxId;
this.contract = warp.contract<T>(contractTxId);
this.cacheUrl = cacheUrl;
this.warpEvaluationOptions = warpEvaluationOptions;
this.arweave = warp.arweave as unknown as Arweave;
}

configuration(): { contractTxId: string; cacheUrl: string | undefined } {
Expand Down Expand Up @@ -202,7 +198,7 @@ export class WarpContract<T>
// Flexible way to return information on the transaction, aids in caching and re-deployment if desired by simply refetching tx anchor and resigning.
if (
(interactionTx && isTransaction(interactionTx)) ||
(interactionTx && isDataItem(interactionTx))
(interactionTx && DataItem.isDataItem(interactionTx))
) {
this.log.debug(`Write interaction succesful`, {
contractTxId: this.contractTxId,
Expand Down
7 changes: 7 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import Arweave from 'arweave';

export const ARWEAVE_TX_REGEX = new RegExp('^[a-zA-Z0-9_-]{43}$');
// sortkey: padded blockheight to 12, JS timestamp, hash of transactionID + block hash. Timestamp only applicable to L2 and normally is all zeros.
Expand All @@ -25,3 +26,9 @@ export const ARNS_TESTNET_REGISTRY_TX =

export const ARNS_DEVNET_REGISTRY_TX =
'_NctcA2sRy1-J4OmIQZbYFPM17piNcbdBPH2ncX2RL8';

export const defaultArweave = Arweave.init({
host: 'ar-io.dev',
port: 443,
protocol: 'https',
});
42 changes: 13 additions & 29 deletions src/utils/arweave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { DataItem, Transaction } from 'arbundles';
import { Tag } from 'warp-contracts/web';
import { Tag, Transaction } from 'warp-contracts';

import { BlockHeight } from '../common.js';
import { ARWEAVE_TX_REGEX } from '../constants.js';
Expand All @@ -29,25 +28,20 @@ export function isBlockHeight(height: string | number): height is BlockHeight {
}

export const dummyTransaction: Transaction = new Transaction({
attributes: {
format: 2,
id: 'dummy',
last_tx: 'dummy',
owner: 'dummy',
tags: [],
target: 'dummy',
quantity: 'dummy',
data: Buffer.from('dummy'),
reward: 'dummy',
signature: 'dummy',
data_size: 'dummy',
data_root: 'dummy',
}, // deps unnecesaary for testing and type checking
deps: {} as any, // eslint-disable-line @typescript-eslint/no-explicit-any
format: 2,
id: 'dummy',
last_tx: 'dummy',
owner: 'dummy',
tags: [],
target: 'dummy',
quantity: 'dummy',
data: new Uint8Array(),
reward: 'dummy',
signature: 'dummy',
data_size: 'dummy',
data_root: 'dummy',
});

export const dummyDataItem: DataItem = new DataItem(Buffer.from('dummy'));

export const isTransaction = (tx: object): tx is Transaction => {
try {
const testTxKeys = Object.keys(dummyTransaction);
Expand All @@ -58,16 +52,6 @@ export const isTransaction = (tx: object): tx is Transaction => {
}
};

export const isDataItem = (item: object): item is DataItem => {
try {
const testItemKeys = Object.keys(dummyDataItem);
const itemKeys = Object.keys(item);
return itemKeys.every((key) => testItemKeys.includes(key));
} catch (error) {
return false;
}
};

export function tagsToObject(tags: Tag[]): {
[x: string]: string;
} {
Expand Down

0 comments on commit 32eebbc

Please sign in to comment.