Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new twitter name resolver #46

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
126 changes: 66 additions & 60 deletions src/baseIdrissCrypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ import {SendToAnyoneParams} from "./types/sendToAnyoneParams";
import {VotingParams} from "./types/votingParams";

export abstract class BaseIdrissCrypto {
protected web3Promise:Promise<Web3>;
protected web3Promise: Promise<Web3>;
protected registryWeb3Promise: Promise<Web3>;
private idrissRegistryContractPromise;
private idrissReverseMappingContractPromise;
private idrissSendToAnyoneContractPromise;
private priceOracleContractPromise;
private tippingContractPromise;
private twitterNameResolver: TwitterNameResolver;
private TWITTER_BEARER_TOKEN = "";
protected ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
protected IDRISS_REGISTRY_CONTRACT_ADDRESS = '0x2EcCb53ca2d4ef91A79213FDDF3f8c2332c2a814';
protected IDRISS_REVERSE_MAPPING_CONTRACT_ADDRESS = '0x561f1b5145897A52A6E94E4dDD4a29Ea5dFF6f64';
Expand All @@ -41,7 +42,7 @@ export abstract class BaseIdrissCrypto {

// we split web3 from web3 for registry, as registry is only accessible on Polygon,
// and library is about to support multiple chains
constructor(web3: Web3|Promise<Web3>, registryWeb3: Web3|Promise<Web3>, connectionOptions: ConnectionOptions) {
constructor(web3: Web3|Promise<Web3>, registryWeb3: Web3|Promise<Web3>, connectionOptions: ConnectionOptions, twitterApiKey: string) {
this.IDRISS_REGISTRY_CONTRACT_ADDRESS = (typeof connectionOptions.idrissRegistryContractAddress !== 'undefined') ?
connectionOptions.idrissRegistryContractAddress : this.IDRISS_REGISTRY_CONTRACT_ADDRESS
this.IDRISS_REVERSE_MAPPING_CONTRACT_ADDRESS = (typeof connectionOptions.reverseIDrissMappingContractAddress !== 'undefined') ?
Expand All @@ -52,11 +53,12 @@ export abstract class BaseIdrissCrypto {
connectionOptions.sendToAnyoneContractAddress : this.IDRISS_SEND_TO_ANYONE_CONTRACT_ADDRESS
this.IDRISS_TIPPING_CONTRACT_ADDRESS = (typeof connectionOptions.tippingContractAddress !== 'undefined') ?
connectionOptions.tippingContractAddress : this.IDRISS_TIPPING_CONTRACT_ADDRESS

this.TWITTER_BEARER_TOKEN = (typeof twitterApiKey !== 'undefined') ?
twitterApiKey : this.TWITTER_BEARER_TOKEN
adiusz marked this conversation as resolved.
Show resolved Hide resolved

this.web3Promise = Promise.resolve(web3)
this.registryWeb3Promise = Promise.resolve(registryWeb3)
this.twitterNameResolver = new TwitterNameResolver()
this.twitterNameResolver = new TwitterNameResolver(this.TWITTER_BEARER_TOKEN);
this.idrissRegistryContractPromise = this.generateIDrissRegistryContract();
this.idrissReverseMappingContractPromise = this.generateIDrissReverseMappingContract();
this.idrissSendToAnyoneContractPromise = this.generateIDrissSendToAnyoneContract();
Expand Down Expand Up @@ -108,8 +110,8 @@ export abstract class BaseIdrissCrypto {
}

public async multitransferToIDriss(
sendParams: SendToAnyoneParams[],
transactionOptions: TransactionOptions = {}
sendParams: SendToAnyoneParams[],
transactionOptions: TransactionOptions = {}
):Promise<MultiSendToHashTransactionReceipt | TransactionReceipt> {
let result: MultiSendToHashTransactionReceipt | TransactionReceipt

Expand Down Expand Up @@ -174,9 +176,9 @@ export abstract class BaseIdrissCrypto {
private async encodeSendToAnyoneToHex(hash: string, param: SendToAnyoneParams): Promise<string> {
const sendToHashContract = await this.idrissSendToAnyoneContractPromise
return sendToHashContract.methods
.sendToAnyone(hash, param.asset.amount, param.asset.type.valueOf(),
param.asset.assetContractAddress ?? this.ZERO_ADDRESS, param.asset.assetId ?? 0, param.message ?? '')
.encodeABI()
.sendToAnyone(hash, param.asset.amount, param.asset.type.valueOf(),
param.asset.assetContractAddress ?? this.ZERO_ADDRESS, param.asset.assetId ?? 0, param.message ?? '')
.encodeABI()
}

private addAssetForAllowanceToMap(assetsMap: Map<string, AssetLiability>, asset: AssetLiability) {
Expand All @@ -187,12 +189,12 @@ export abstract class BaseIdrissCrypto {

// because for ERC721 we have to approve each id separately
const assetMapKey = asset.type === AssetType.ERC721
? `${asset.assetContractAddress}-${asset.assetId}`
: `${asset.assetContractAddress}`
? `${asset.assetContractAddress}-${asset.assetId}`
: `${asset.assetContractAddress}`

const savedAsset: AssetLiability = assetsMap
.get(assetMapKey)
?? {...asset, amount: 0}
.get(assetMapKey)
?? {...asset, amount: 0}

savedAsset.amount = BigNumber.from(savedAsset.amount).add(asset.amount)
assetsMap.set(assetMapKey, savedAsset)
Expand Down Expand Up @@ -363,7 +365,7 @@ export abstract class BaseIdrissCrypto {
let transactionReceipt: TransactionReceipt

await this.approveAssets([asset], signer,
roundContractAddress, transactionOptions);
roundContractAddress, transactionOptions);

if (!transactionOptions.gas) {
try {
Expand All @@ -379,10 +381,10 @@ export abstract class BaseIdrissCrypto {
}

const sendOptions = {
from: signer,
...transactionOptions,
value: nativeToSend.toString()
}
from: signer,
...transactionOptions,
value: nativeToSend.toString()
}

transactionReceipt = (await this.getVotingMethod({
encodedVote: encodedVote,
Expand All @@ -401,7 +403,7 @@ export abstract class BaseIdrissCrypto {
let transactionReceipt: TransactionReceipt

await this.approveAssets([asset], signer,
this.IDRISS_TIPPING_CONTRACT_ADDRESS, transactionOptions);
this.IDRISS_TIPPING_CONTRACT_ADDRESS, transactionOptions);

message = message ?? ''

Expand All @@ -420,10 +422,10 @@ export abstract class BaseIdrissCrypto {
}

const sendOptions = {
from: signer,
...transactionOptions,
value: maticToSend.toString()
}
from: signer,
...transactionOptions,
value: maticToSend.toString()
}

transactionReceipt = (await this.getTippingMethod({
asset: asset,
Expand Down Expand Up @@ -451,9 +453,9 @@ export abstract class BaseIdrissCrypto {
}

const sendOptions = {
from: signer,
...transactionOptions
}
from: signer,
...transactionOptions
}

transactionReceipt = await sendToHashContract.methods
.revertPayment(beneficiary, assetType, assetContractAddress)
Expand Down Expand Up @@ -529,8 +531,8 @@ export abstract class BaseIdrissCrypto {
transactionOptions.gas = await tippingContract.methods.batch(encodedCalldata).estimateGas({from: signer, value: maticToSend.toString()});
}
catch (e) {
console.log("Could not estimate gas: ", e);
}
console.log("Could not estimate gas: ", e);
}
}

transactionReceipt = await tippingContract.methods
Expand Down Expand Up @@ -589,12 +591,12 @@ export abstract class BaseIdrissCrypto {
}

transactionReceipt = await sendToHashContract.methods
.batch(encodedCalldata)
.send({
from: signer,
...transactionOptions,
value: maticToSend.toString()
})
.batch(encodedCalldata)
.send({
from: signer,
...transactionOptions,
value: maticToSend.toString()
})

delete transactionOptions.gas

Expand All @@ -619,15 +621,15 @@ export abstract class BaseIdrissCrypto {

private async callWeb3SendToAnyone(hash: string, beneficiary: string, asset: AssetLiability,
message: string, transactionOptions:TransactionOptions)
:Promise<{ transactionReceipt: TransactionReceipt; claimUrl: string; claimPassword: string }> {
:Promise<{ transactionReceipt: TransactionReceipt; claimUrl: string; claimPassword: string }> {
const paymentFee = await this.calculateSendToAnyonePaymentFee(asset.amount, asset.type)
const maticToSend = asset.type === AssetType.Native ? BigNumber.from(asset.amount).add(paymentFee) : paymentFee
const signer = await this.getConnectedAccount()
let transactionReceipt: TransactionReceipt
const sendToHashContract = await this.idrissSendToAnyoneContractPromise

await this.approveAssets([asset], signer,
this.IDRISS_SEND_TO_ANYONE_CONTRACT_ADDRESS, transactionOptions);
this.IDRISS_SEND_TO_ANYONE_CONTRACT_ADDRESS, transactionOptions);

const claimPassword = await this.generateClaimPassword()
const hashWithPassword = await this.generateHashWithPassword(hash, claimPassword)
Expand Down Expand Up @@ -657,33 +659,33 @@ export abstract class BaseIdrissCrypto {
}

private async approveAssets(
assets: AssetLiability[],
signer: string,
toContract: string,
transactionOptions: TransactionOptions
assets: AssetLiability[],
signer: string,
toContract: string,
transactionOptions: TransactionOptions
) {
let approvalTransactionReceipt: TransactionReceipt | boolean = false

for (let asset of assets) {
if (asset.type === AssetType.ERC20) {
approvalTransactionReceipt = await this.authorizeERC20ForContract(
signer,
toContract,
asset,
transactionOptions)
signer,
toContract,
asset,
transactionOptions)
} else if (asset.type === AssetType.ERC721) {
approvalTransactionReceipt = await this.authorizeERC721ForContract(
signer,
toContract,
asset,
transactionOptions)
signer,
toContract,
asset,
transactionOptions)
} else if (asset.type === AssetType.ERC1155) {
approvalTransactionReceipt = await this.setAuthorizationForERC1155Contract(
signer,
toContract,
asset,
true,
transactionOptions)
signer,
toContract,
asset,
true,
transactionOptions)
}

// @ts-ignore
Expand Down Expand Up @@ -756,8 +758,8 @@ export abstract class BaseIdrissCrypto {
transactionOptions.gas = await contract.methods.approve(contractToAuthorize, BigNumber.from(asset.amount).toString()).estimateGas({from: signer});
}
catch (e) {
console.log("Could not estimate gas: ", e);
}
console.log("Could not estimate gas: ", e);
}
}
let approval = await contract.methods
.approve(contractToAuthorize, BigNumber.from(asset.amount).toString())
Expand Down Expand Up @@ -787,8 +789,8 @@ export abstract class BaseIdrissCrypto {
transactionOptions.gas = await contract.methods.approve(contractToAuthorize, asset.assetId).estimateGas({from: signer});
}
catch (e) {
console.log("Could not estimate gas: ", e);
}
console.log("Could not estimate gas: ", e);
}
}

let approval = await contract.methods
Expand All @@ -814,13 +816,13 @@ export abstract class BaseIdrissCrypto {

if (isApproved !== authToSet) {

if (!transactionOptions.gas) {
if (!transactionOptions.gas) {
try {
transactionOptions.gas = await contract.methods.setApprovalForAll(contractToAuthorize, true).estimateGas({from: signer});
}
catch (e) {
console.log("Could not estimate gas: ", e);
}
console.log("Could not estimate gas: ", e);
}
}
let approval = await contract.methods
// unfortunately ERC1155 standard does not allow granular permissions, and only option is to approve all user tokens
Expand Down Expand Up @@ -973,4 +975,8 @@ export abstract class BaseIdrissCrypto {
return result;
}
}

public async reverseResolveNew(address: string) {
return await this.callWeb3ReverseIDriss(address)
}
adiusz marked this conversation as resolved.
Show resolved Hide resolved
}
26 changes: 19 additions & 7 deletions src/browser.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import {BaseIdrissCrypto} from "./baseIdrissCrypto";

export class IdrissCrypto extends BaseIdrissCrypto {
constructor(polygonEndpoint: string = "https://polygon-rpc.com/", connectionOptions: ConnectionOptions = {}) {
constructor(twitterApiKey: string, polygonEndpoint: string = "https://polygon-rpc.com/", connectionOptions: ConnectionOptions = {}) {
adiusz marked this conversation as resolved.
Show resolved Hide resolved
// @ts-ignore
const Web3Promise = import("web3/dist/web3.min.js")
super(BaseIdrissCrypto.generateWeb3(Web3Promise, polygonEndpoint, connectionOptions.web3Provider),
BaseIdrissCrypto.generateWeb3(Web3Promise, polygonEndpoint), connectionOptions);
super(
BaseIdrissCrypto.generateWeb3(Web3Promise, polygonEndpoint, connectionOptions.web3Provider),
BaseIdrissCrypto.generateWeb3(Web3Promise, polygonEndpoint),
connectionOptions,
twitterApiKey
);
}

protected async digestMessage(message:string) {
protected async digestMessage(message: string) {
const msgUint8 = new TextEncoder().encode(message); // encode as (utf-8) Uint8Array
const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8); // hash the message
const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array
Expand All @@ -23,7 +27,7 @@ export class IdrissCrypto extends BaseIdrissCrypto {
throw new Error('No wallet detected.');
}

const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
const accounts = await ethereum.request({method: 'eth_requestAccounts'});
return accounts[0];
}

Expand All @@ -36,5 +40,13 @@ export class IdrissCrypto extends BaseIdrissCrypto {

import {Authorization, CreateOTPResponse, WrongOTPException} from "./authorization"
import {AuthorizationTestnet, CreateOTPResponseTestnet, WrongOTPExceptionTestnet} from "./authorizationTestnet"
import { ConnectionOptions } from "./types/connectionOptions";
export {Authorization, CreateOTPResponse, WrongOTPException, AuthorizationTestnet, CreateOTPResponseTestnet, WrongOTPExceptionTestnet};
import {ConnectionOptions} from "./types/connectionOptions";

export {
Authorization,
CreateOTPResponse,
WrongOTPException,
AuthorizationTestnet,
CreateOTPResponseTestnet,
WrongOTPExceptionTestnet
};
25 changes: 17 additions & 8 deletions src/idrissCrypto.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
import {BaseIdrissCrypto} from "./baseIdrissCrypto";
import { ConnectionOptions } from "./types/connectionOptions";
import {ConnectionOptions} from "./types/connectionOptions";

let crypto = require('crypto');

/**
* This class is used for NodeJS
*/
export class IdrissCrypto extends BaseIdrissCrypto {
constructor(polygonEndpoint: string = "https://polygon-rpc.com/", connectionOptions: ConnectionOptions = {}) {
constructor(twitterApiKey: string, polygonEndpoint: string = "https://polygon-rpc.com/", connectionOptions: ConnectionOptions = {}) {
const Web3Promise = import("web3");
super(BaseIdrissCrypto.generateWeb3(Web3Promise, polygonEndpoint, connectionOptions.web3Provider),
BaseIdrissCrypto.generateWeb3(Web3Promise, polygonEndpoint), connectionOptions);
}
super(
BaseIdrissCrypto.generateWeb3(Web3Promise, polygonEndpoint, connectionOptions.web3Provider),
BaseIdrissCrypto.generateWeb3(Web3Promise, polygonEndpoint),
connectionOptions,
twitterApiKey
);
}

protected async digestMessage(message: string):Promise<string> {
protected async digestMessage(message: string): Promise<string> {
return crypto.createHash('sha256').update(message).digest('hex');
}

protected async getConnectedAccount(): Promise<string> {
return this.web3Promise
.then(web3 => {return web3.eth.getAccounts()})
.then(acc => {return acc[0]})
.then(web3 => {
return web3.eth.getAccounts()
})
.then(acc => {
return acc[0]
})
}
}
Loading