Skip to content

Commit

Permalink
feat(ant registry): add ant registry class
Browse files Browse the repository at this point in the history
  • Loading branch information
atticusofsparta committed Jul 30, 2024
1 parent 4e84bd2 commit 2056674
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 2 deletions.
106 changes: 106 additions & 0 deletions src/common/ant-registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* 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 { ANT_REGISTRY_ID } from '../constants.js';
import {
AoANTRegistryRead,
AoANTRegistryWrite,
AoMessageResult,
AoSigner,
OptionalSigner,
ProcessConfiguration,
WithSigner,
isProcessConfiguration,
isProcessIdConfiguration,
} from '../types.js';
import { createAoSigner } from '../utils/ao.js';
import { AOProcess } from './index.js';

export class ANTRegistry {
static init(): AoANTRegistryRead;
static init(
config: Required<ProcessConfiguration> & { signer?: undefined },
): AoANTRegistryRead;
static init({
signer,
...config
}: WithSigner<Required<ProcessConfiguration>>): AoANTRegistryRead;
static init(
config?: OptionalSigner<ProcessConfiguration>,
): AoANTRegistryRead | AoANTRegistryWrite {
if (config && config.signer) {
const { signer, ...rest } = config;
return new AoANTRegistryWriteable({
...rest,
signer,
});
}
return new AoANTRegistryReadable(config);
}
}

export class AoANTRegistryReadable implements AoANTRegistryRead {
protected process: AOProcess;

constructor(config?: ProcessConfiguration) {
if (
config &&
(isProcessIdConfiguration(config) || isProcessConfiguration(config))
) {
this.process = AOProcess.fromConfiguration(config);
} else {
this.process = AOProcess.fromConfiguration({
processId: ANT_REGISTRY_ID,
});
}
}

// Should we rename this to "getANTsByAddress"? seems more clear, though not same as handler name
async accessControlList({ address }: { address: string }): Promise<string[]> {
return this.process.read({
tags: [
{ name: 'Action', value: 'Access-Control-List' },
{ name: 'Address', value: address },
],
});
}
}

export class AoANTRegistryWriteable
extends AoANTRegistryReadable
implements AoANTRegistryWrite
{
private signer: AoSigner;

constructor({ signer, ...config }: WithSigner<ProcessConfiguration>) {
super(config);
this.signer = createAoSigner(signer);
}

async register({
processId,
}: {
processId: string;
}): Promise<AoMessageResult> {
return this.process.send({
tags: [
{ name: 'Action', value: 'Register' },
{ name: 'Process-Id', value: processId },
],
signer: this.signer,
});
}
}
26 changes: 24 additions & 2 deletions src/common/contracts/ao-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@
*/
import { connect } from '@permaweb/aoconnect';

import { AOContract, AoClient, AoSigner } from '../../types.js';
import {
AOContract,
AoClient,
AoSigner,
ProcessConfiguration,
isProcessConfiguration,
isProcessIdConfiguration,
} from '../../types.js';
import { safeDecode } from '../../utils/json.js';
import { version } from '../../version.js';
import { WriteInteractionError } from '../error.js';
import {
InvalidContractConfigurationError,
WriteInteractionError,
} from '../error.js';
import { ILogger, Logger } from '../logger.js';

export class AOProcess implements AOContract {
Expand All @@ -41,6 +51,18 @@ export class AOProcess implements AOContract {
this.ao = ao;
}

static fromConfiguration(config: Required<ProcessConfiguration>) {
if (isProcessConfiguration(config)) {
return config.process;
} else if (isProcessIdConfiguration(config)) {
return new AOProcess({
processId: config.processId,
});
} else {
throw new InvalidContractConfigurationError();
}
}

async read<K>({
tags,
retries = 3,
Expand Down
1 change: 1 addition & 0 deletions src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
export * from './error.js';
export * from './logger.js';
export * from './ant.js';
export * from './ant-registry.js';

// ao
export * from './io.js';
Expand Down
4 changes: 4 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export const IO_DEVNET_PROCESS_ID =
export const ioDevnetProcessId = IO_DEVNET_PROCESS_ID;
export const IO_TESTNET_PROCESS_ID =
'agYcCFJtrMG6cqMuZfskIkFTGvUPddICmtQSBIoPdiA';

// TODO: set ant registry ID with deployed contract
export const ANT_REGISTRY_ID =
process.env.ANT_REGISTRY_ID ?? 'todo-set-ant-registry-id';
export const MIO_PER_IO = 1_000_000;
export const AOS_MODULE_ID = 'cbn0KKrBZH7hdNkNokuXLtGryrWM--PjSTBqIzw9Kkk';
export const ANT_LUA_ID = 'Flwio4Lr08g6s6uim6lEJNnVGD9ylvz0_aafvpiL8FI';
Expand Down
8 changes: 8 additions & 0 deletions src/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,14 @@ export interface AoANTWrite extends AoANTRead {
setName({ name }): Promise<AoMessageResult>;
}

export interface AoANTRegistryRead {
accessControlList(params: { address: string }): Promise<string[]>;
}

export interface AoANTRegistryWrite extends AoANTRegistryRead {
register(params: { processId: string }): Promise<AoMessageResult>;
}

// AO Contract types
export interface AoIOState {
GatewayRegistry: Record<WalletAddress, AoGateway>;
Expand Down

0 comments on commit 2056674

Please sign in to comment.