Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
!**/packages/bundler/dist
**.idea/
**.npmrc
sdk/*.tgz
**.env
sdk/*.tgz
15 changes: 15 additions & 0 deletions sdk/src/common/ario.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { DependencyType } from "helpers/types.ts";

export function getPrimaryNameWith(deps: DependencyType) {
return async(address:string):Promise<string> => {
try {
const {name} = await deps.ario.getPrimaryName({
address,
});
return name
} catch (error:any) {
console.error(error.message ?? `Error fetching ArNS primary name for ${address}`)
return ""
}
}
}
4 changes: 3 additions & 1 deletion sdk/src/helpers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ export type DependencyType = {
ao: any;
signer?: any;
arweave?: any;
node?: { url: string; scheduler: string; authority: string };
node?: { url: string, scheduler: string, authority:string };
ario?:any
};

export type ProcessReadType = {
Expand All @@ -15,6 +16,7 @@ export type ProcessReadType = {

export type ReadOptsType = {
hydrate?: boolean;
primaryName?: string;
}

export type ProcessSpawnType = {
Expand Down
24 changes: 20 additions & 4 deletions sdk/src/services/profiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DependencyType, GQLNodeResponseType, ProfileArgsType, ProfileType, Read
import { getBootTag, isValidMediaData } from '../helpers/utils.ts';

import { createZoneWith, getZoneWith, updateZoneVersionWith, updateZoneWith } from './zones.ts';
import { getPrimaryNameWith } from 'common/ario.ts';

export function createProfileWith(deps: DependencyType) {
const createZone = createZoneWith(deps);
Expand Down Expand Up @@ -101,12 +102,14 @@ export function getProfileByIdWith(deps: DependencyType) {
const getZone = getZoneWith(deps);

return async (profileId: string, opts?: ReadOptsType): Promise<ProfileType | null> => {
const { primaryName } = opts || {};
try {
const zone = await getZone(profileId, opts);
if (!zone) {
throw new Error('Error fetching profile - Not found');
}
return {

const profile = {
id: profileId,
owner: zone.owner,
assets: zone.assets,
Expand All @@ -115,7 +118,13 @@ export function getProfileByIdWith(deps: DependencyType) {
version: zone.version,
authorities: zone.authorities,
...zone.store,
};
}

if(primaryName && primaryName.trim().length > 0) {
profile.arnsName = primaryName
}

return profile
} catch (e: any) {
throw new Error(e.message ?? 'Error fetching profile');
}
Expand All @@ -125,7 +134,7 @@ export function getProfileByIdWith(deps: DependencyType) {
export function getProfileByWalletAddressWith(deps: DependencyType) {
const getProfileById = getProfileByIdWith(deps);

return async (walletAddress: string): Promise<(ProfileType & any) | null> => {
return async (walletAddress: string, arns:boolean=false): Promise<(ProfileType & any) | null> => {
try {
const gqlResponse = await getGQLData({
gateway: GATEWAYS.ao,
Expand All @@ -143,7 +152,14 @@ export function getProfileByWalletAddressWith(deps: DependencyType) {
return timestampB - timestampA;
});

return await getProfileById(gqlResponse.data[0].node.id);
let primaryName

if(arns) {
const getPrimaryName = getPrimaryNameWith(deps)
primaryName = await getPrimaryName(walletAddress)
}

return await getProfileById(gqlResponse.data[0].node.id, { primaryName });
}

return { id: null };
Expand Down
2 changes: 2 additions & 0 deletions sdk/tests/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PATH_TO_WALLET = "path_to_your_jwk" #optional
ArNS_NAME = "arns_name_linked_to_your_wallet_above" #optional
33 changes: 29 additions & 4 deletions sdk/tests/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import Arweave from 'arweave';
import { connect, createSigner } from '@permaweb/aoconnect';
import Permaweb from '@permaweb/libs';
import { ARIO } from '@ar.io/sdk';
import fs from 'fs';
import 'dotenv/config'

const CREATOR = 'L0p6ecK4PdgWome2LI0STBNl5_ZK3XwnXCcoUq0tLLY';

Expand Down Expand Up @@ -111,16 +113,21 @@ function logError(message) {
scheduler: 'mYJTM8VpIibDLuyGLQTcbcPy-LeOY48qzECADTUYfWc',
};

const ario = ARIO.init({
signer
});

const dependencies = {
ao: connect({
MODE: 'mainnet',
MODE: "legacy",
URL: AO_NODE.url,
SCHEDULER: AO_NODE.scheduler,
signer: signer,
}),
arweave: arweave,
signer: signer,
node: AO_NODE,
ario
};

const permaweb = Permaweb.init(dependencies);
Expand Down Expand Up @@ -159,6 +166,7 @@ function logError(message) {
async function testProfiles() {
try {
logTest('Testing profile creation...');
const address = await arweave.wallets.jwkToAddress(wallet)
const profileId = await permaweb.createProfile(
{
username: 'My username',
Expand All @@ -178,12 +186,29 @@ function logError(message) {
expect(profileById.username).toEqual('My username');

logTest('Testing profile fetch by address...');
const profileByWalletAddress = await permaweb.getProfileByWalletAddress(
await arweave.wallets.jwkToAddress(wallet),
let profileByWalletAddress = await permaweb.getProfileByWalletAddress(
address
);

expect(profileByWalletAddress).toBeDefined();
expect(profileByWalletAddress.username).toEqual('My username');

logTest('Testing ArNS primary name support...');

expect(profileByWalletAddress.arnsName).toEqual(undefined)

const isFetchArns = true

profileByWalletAddress = await permaweb.getProfileByWalletAddress(
address, isFetchArns
);
//check if wallet was generated automatically
//It will show normal display name because generated wallets do not have primary names attached
if(fs.existsSync(process.env.PATH_TO_WALLET)) {
expect(profileByWalletAddress.arnsName).toEqual(process.env.ArNS_NAME);
} else {
console.log(address, "has no primary name")
expect(profileByWalletAddress.arnsName).toEqual(undefined)
}

logTest('Testing profile update...');
const profileUpdateId = await permaweb.updateProfile(
Expand Down
Loading