Skip to content

Commit 5327151

Browse files
JustinFengmicwallace
authored andcommitted
chore: 🎸 create base class to get rid of duplicated code
1 parent c6a3984 commit 5327151

File tree

6 files changed

+550
-980
lines changed

6 files changed

+550
-980
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { IEngineConfig, ITokenScriptEngine } from './IEngine';
2+
import { ITokenScript } from './ITokenScript';
3+
import { Repo } from './repo/Repo';
4+
import { IWalletAdapter } from './wallet/IWalletAdapter';
5+
6+
const DEFAULT_CONFIG: IEngineConfig = {
7+
ipfsGateway: 'https://smart-token-labs-demo-server.mypinata.cloud/ipfs/',
8+
noLocalStorage: false,
9+
trustedKeys: [],
10+
};
11+
12+
/**
13+
* Engine.ts is the top level component for the TokenScript engine, it can be used to create a new TokenScript instance
14+
* via the repo, URL or directly from XML source
15+
*/
16+
export abstract class AbstractTokenScriptEngine implements ITokenScriptEngine {
17+
protected repo: Repo
18+
// TODO: Should we pass in a function or a constructor, dunno
19+
constructor(public getWalletAdapter: () => Promise<IWalletAdapter>, public readonly config?: IEngineConfig) {
20+
this.repo = new Repo(this)
21+
22+
if (this.config) {
23+
this.config = {
24+
...DEFAULT_CONFIG,
25+
...this.config,
26+
};
27+
} else {
28+
this.config = DEFAULT_CONFIG;
29+
}
30+
}
31+
32+
public resolveAllScripts(tsPath: string, forceReload = false) {
33+
return this.repo.resolveAllScripts(tsPath, forceReload);
34+
}
35+
36+
/**
37+
* Sign a personal message using the Ethereum WalletAdapter implementation provided by the user-agent
38+
* @param data
39+
*/
40+
public async signPersonalMessage(data) {
41+
try {
42+
return await (await this.getWalletAdapter()).signPersonalMessage(data);
43+
} catch (e) {
44+
throw new Error('Signing failed: ' + e.message);
45+
}
46+
}
47+
48+
// TODO: This should probably be moved somewhere else
49+
/**
50+
* Public IPFS gateways are sometimes very slow, so when a custom IPFS gateway is supplied in the config,
51+
* we update the following URLs to our own gateways.
52+
* @private
53+
*/
54+
private IPFS_REPLACE_GATEWAYS = ['ipfs://', 'https://ipfs.io/ipfs/', 'https://gateway.pinata.cloud/ipfs/'];
55+
56+
public processIpfsUrl(uri: string) {
57+
for (let gateway of this.IPFS_REPLACE_GATEWAYS) {
58+
if (this.config.ipfsGateway.indexOf(gateway) === 0) {
59+
continue;
60+
}
61+
62+
if (uri.indexOf(gateway) === 0) {
63+
uri = uri.replace(gateway, this.config.ipfsGateway);
64+
break;
65+
}
66+
}
67+
68+
return uri;
69+
}
70+
71+
public async getScriptUris(chain: string | number, contractAddr: string) {
72+
// Direct RPC gets too hammered by opensea view (that doesn't allow localStorage to cache XML)
73+
/*const provider = await this.getWalletAdapter();
74+
let uri: string|string[]|null;
75+
76+
try {
77+
uri = Array.from(await provider.call(parseInt(chain), contractAddr, "scriptURI", [], ["string[]"])) as string[];
78+
} catch (e) {
79+
uri = await provider.call(parseInt(chain), contractAddr, "scriptURI", [], ["string"]);
80+
}
81+
82+
if (uri && Array.isArray(uri))
83+
uri = uri.length ? uri[0] : null
84+
85+
return <string>uri;*/
86+
87+
// TODO: Add support for selecting a specific index or URL?
88+
// const res = await fetch(`https://api.token-discovery.tokenscript.org/script-uri?chain=${chain}&contract=${contractAddr}`);
89+
// const scriptUris = await res.json();
90+
//return <string>scriptUris[0];
91+
92+
// i.e. https://store-backend.smartlayer.network/tokenscript/0xD5cA946AC1c1F24Eb26dae9e1A53ba6a02bd97Fe/chain/137/script-uri
93+
const res = await fetch(`https://store-backend.smartlayer.network/tokenscript/${contractAddr.toLowerCase()}/chain/${chain}/script-uri`);
94+
const data = await res.json();
95+
96+
if (!data.scriptURI) return null;
97+
98+
let uris: string[] = [];
99+
100+
if (data.scriptURI.erc5169?.length) uris.push(...data.scriptURI.erc5169);
101+
102+
if (data.scriptURI.offchain?.length) uris.push(...data.scriptURI.offchain);
103+
104+
return uris.length ? uris : null;
105+
}
106+
107+
public abstract getTokenScriptFromUrl(url: string): Promise<ITokenScript>;
108+
public abstract loadTokenScript(xml: string): Promise<ITokenScript>;
109+
public abstract getTokenDiscoveryAdapter?: () => Promise<any>;
110+
public abstract getAttestationStorageAdapter?: () => any;
111+
public abstract getLocalStorageAdapter?: () => any;
112+
public abstract getAttestationManager(): any;
113+
}

0 commit comments

Comments
 (0)