Skip to content

Commit

Permalink
remove ccip and ens
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis2626 committed Apr 15, 2024
1 parent f5d1905 commit 9161574
Show file tree
Hide file tree
Showing 18 changed files with 41 additions and 1,072 deletions.
3 changes: 1 addition & 2 deletions src.ts/_tests/test-address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
} from "./types.js";

import {
getAddress, getIcapAddress,
getAddress,
getCreateAddress, getCreate2Address
} from "../index.js";

Expand Down Expand Up @@ -76,7 +76,6 @@ describe("computes ICAP address", function() {
const tests = loadTests<TestCaseAccount>("accounts");
for (const test of tests) {
it(`computes the ICAP address: ${ test.name }`, function() {
assert.equal(getIcapAddress(test.address), test.icap);
assert.equal(getAddress(test.address.toLowerCase()), test.address);
assert.equal(getAddress("0x" + test.address.substring(2).toUpperCase()), test.address);
});
Expand Down
87 changes: 0 additions & 87 deletions src.ts/address/address.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { keccak256 } from "../crypto/index.js";
import { getBytes, assertArgument, BytesLike, concat, zeroPadValue, dataSlice, BigNumberish, toBigInt, toBeHex, stripZerosLeft } from "../utils/index.js";


const BN_0 = BigInt(0);
const BN_36 = BigInt(36);

function getChecksumAddress(address: string): string {
// if (!isHexString(address, 20)) {
// logger.throwArgumentError("invalid address", "address", address);
Expand Down Expand Up @@ -33,54 +29,6 @@ function getChecksumAddress(address: string): string {
return "0x" + chars.join("");
}

// See: https://en.wikipedia.org/wiki/International_Bank_Account_Number

// Create lookup table
const ibanLookup: { [character: string]: string } = { };
for (let i = 0; i < 10; i++) { ibanLookup[String(i)] = String(i); }
for (let i = 0; i < 26; i++) { ibanLookup[String.fromCharCode(65 + i)] = String(10 + i); }

// How many decimal digits can we process? (for 64-bit float, this is 15)
// i.e. Math.floor(Math.log10(Number.MAX_SAFE_INTEGER));
const safeDigits = 15;

function ibanChecksum(address: string): string {
address = address.toUpperCase();
address = address.substring(4) + address.substring(0, 2) + "00";

let expanded = address.split("").map((c) => { return ibanLookup[c]; }).join("");

// Javascript can handle integers safely up to 15 (decimal) digits
while (expanded.length >= safeDigits){
let block = expanded.substring(0, safeDigits);
expanded = parseInt(block, 10) % 97 + expanded.substring(block.length);
}

let checksum = String(98 - (parseInt(expanded, 10) % 97));
while (checksum.length < 2) { checksum = "0" + checksum; }

return checksum;
};

const Base36 = (function() {;
const result: Record<string, bigint> = { };
for (let i = 0; i < 36; i++) {
const key = "0123456789abcdefghijklmnopqrstuvwxyz"[i];
result[key] = BigInt(i);
}
return result;
})();

function fromBase36(value: string): bigint {
value = value.toLowerCase();

let result = BN_0;
for (let i = 0; i < value.length; i++) {
result = result * BN_36 + Base36[value[i]];
}
return result;
}

/**
* Returns a normalized and checksumed address for %%address%%.
* This accepts non-checksum addresses, checksum addresses and
Expand Down Expand Up @@ -134,44 +82,9 @@ export function getAddress(address: string): string {
return result;
}

// Maybe ICAP? (we only support direct mode)
if (address.match(/^XE[0-9]{2}[0-9A-Za-z]{30,31}$/)) {
// It is an ICAP address with a bad checksum
assertArgument(address.substring(2, 4) === ibanChecksum(address), "bad icap checksum", "address", address);

let result = fromBase36(address.substring(4)).toString(16);
while (result.length < 40) { result = "0" + result; }
return getChecksumAddress("0x" + result);
}

assertArgument(false, "invalid address", "address", address);
}

/**
* The [ICAP Address format](link-icap) format is an early checksum
* format which attempts to be compatible with the banking
* industry [IBAN format](link-wiki-iban) for bank accounts.
*
* It is no longer common or a recommended format.
*
* @example:
* getIcapAddress("0x8ba1f109551bd432803012645ac136ddd64dba72");
* //_result:
*
* getIcapAddress("XE65GB6LDNXYOFTX0NSV3FUWKOWIXAMJK36");
* //_result:
*
* // Throws an error if the ICAP checksum is wrong
* getIcapAddress("XE65GB6LDNXYOFTX0NSV3FUWKOWIXAMJK37");
* //_error:
*/
export function getIcapAddress(address: string): string {
//let base36 = _base16To36(getAddress(address).substring(2)).toUpperCase();
let base36 = BigInt(getAddress(address)).toString(36).toUpperCase();
while (base36.length < 30) { base36 = "0" + base36; }
return "XE" + ibanChecksum("XE00" + base36) + base36;
}

export function getContractAddress(from: string, nonce: BigNumberish, data: BytesLike): string {
const nonceBytes = zeroPadValue(toBeHex(toBigInt(nonce)), 8);
return getAddress(dataSlice(keccak256(concat([getAddress(from), nonceBytes, stripZerosLeft(data) ])), 12))
Expand Down
12 changes: 3 additions & 9 deletions src.ts/address/checks.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { assert, assertArgument } from "../utils/index.js";
import { assertArgument } from "../utils/index.js";

import { getAddress } from "./address.js";

import type { Addressable, AddressLike, NameResolver } from "./index.js";
import type { Addressable, AddressLike } from "./index.js";


/**
Expand Down Expand Up @@ -59,7 +59,6 @@ export function isAddress(value: any): value is string {
async function checkAddress(target: any, promise: Promise<null | string>): Promise<string> {
const result = await promise;
if (result == null || result === "0x0000000000000000000000000000000000000000") {
assert(typeof(target) !== "string", "unconfigured name", "UNCONFIGURED_NAME", { value: target });
assertArgument(false, "invalid AddressLike value; did not resolve to a value address", "target", target);
}
return getAddress(result);
Expand Down Expand Up @@ -102,16 +101,11 @@ async function checkAddress(target: any, promise: Promise<null | string>): Promi
* resolveAddress("nothing-here.ricmoo.eth")
* //_error:
*/
export function resolveAddress(target: AddressLike, resolver?: null | NameResolver): string | Promise<string> {
export function resolveAddress(target: AddressLike): string | Promise<string> {

if (typeof(target) === "string") {
if (target.match(/^0x[0-9a-f]{40}$/i)) { return getAddress(target); }

assert(resolver != null, "ENS resolution requires a provider",
"UNSUPPORTED_OPERATION", { operation: "resolveName" });

return checkAddress(target, resolver.resolveName(target));

} else if (isAddressable(target)) {
return checkAddress(target, target.getAddress());

Expand Down
16 changes: 1 addition & 15 deletions src.ts/address/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,7 @@ export interface Addressable {
*/
export type AddressLike = string | Promise<string> | Addressable;

/**
* An interface for any object which can resolve an ENS name.
*/
export interface NameResolver {
/**
* Resolve to the address for the ENS %%name%%.
*
* Resolves to ``null`` if the name is unconfigued. Use
* [[resolveAddress]] (passing this object as %%resolver%%) to
* throw for names that are unconfigured.
*/
resolveName(name: string): Promise<null | string>;
}

export { getAddress, getIcapAddress } from "./address.js";
export { getAddress } from "./address.js";

export { getCreateAddress, getCreate2Address } from "./contract-address.js";

Expand Down
53 changes: 6 additions & 47 deletions src.ts/contract/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { isAddressable, resolveAddress } from "../address/index.js";
import { copyRequest, Log, TransactionResponse } from "../providers/provider.js";
import {
defineProperties, getBigInt, isCallException, isHexString, resolveProperties,
isError, makeError, assert, assertArgument
isError, assert, assertArgument
} from "../utils/index.js";

import {
Expand All @@ -15,7 +15,7 @@ import {
} from "./wrappers.js";

import type { EventFragment, FunctionFragment, InterfaceAbi, ParamType, Result } from "../abi/index.js";
import type { Addressable, NameResolver } from "../address/index.js";
import type { Addressable } from "../address/index.js";
import type { EventEmitterable, Listener } from "../utils/index.js";
import type {
BlockTag, ContractRunner, Provider, TransactionRequest, TopicFilter
Expand Down Expand Up @@ -48,10 +48,6 @@ interface ContractRunnerSender extends ContractRunner {
sendTransaction: (tx: TransactionRequest) => Promise<TransactionResponse>;
}

interface ContractRunnerResolver extends ContractRunner {
resolveName: (name: string | Addressable) => Promise<null | string>;
}

function canCall(value: any): value is ContractRunnerCaller {
return (value && typeof(value.call) === "function");
}
Expand All @@ -60,22 +56,10 @@ function canEstimate(value: any): value is ContractRunnerEstimater {
return (value && typeof(value.estimateGas) === "function");
}

function canResolve(value: any): value is ContractRunnerResolver {
return (value && typeof(value.resolveName) === "function");
}

function canSend(value: any): value is ContractRunnerSender {
return (value && typeof(value.sendTransaction) === "function");
}

function getResolver(value: any): undefined | NameResolver {
if (value != null) {
if (canResolve(value)) { return value; }
if (value.provider) { return value.provider; }
}
return undefined;
}

class PreparedTopicFilter implements DeferredTopicFilter {
#filter: Promise<TopicFilter>;
readonly fragment!: EventFragment;
Expand All @@ -86,9 +70,6 @@ class PreparedTopicFilter implements DeferredTopicFilter {
throw new Error("too many arguments");
}

// Recursively descend into args and resolve any addresses
const runner = getRunner(contract.runner, "resolveName");
const resolver = canResolve(runner) ? runner: null;
this.#filter = (async function() {
const resolvedArgs = await Promise.all(fragment.inputs.map((param, index) => {
const arg = args[index];
Expand All @@ -97,9 +78,9 @@ class PreparedTopicFilter implements DeferredTopicFilter {
return param.walkAsync(args[index], (type, value) => {
if (type === "address") {
if (Array.isArray(value)) {
return Promise.all(value.map((v) => resolveAddress(v, resolver)));
return Promise.all(value.map((v) => resolveAddress(v)));
}
return resolveAddress(value, resolver);
return resolveAddress(value);
}
return value;
});
Expand Down Expand Up @@ -164,12 +145,10 @@ export async function copyOverrides<O extends string = "data" | "to">(arg: any,
*/
export async function resolveArgs(_runner: null | ContractRunner, inputs: ReadonlyArray<ParamType>, args: Array<any>): Promise<Array<any>> {
// Recursively descend into args and resolve any addresses
const runner = getRunner(_runner, "resolveName");
const resolver = canResolve(runner) ? runner: null;
return await Promise.all(inputs.map((param, index) => {
return param.walkAsync(args[index], (type, value) => {
value = Typed.dereference(value, type);
if (type === "address") { return resolveAddress(value, resolver); }
if (type === "address") { return resolveAddress(value); }
return value;
});
}));
Expand All @@ -184,7 +163,7 @@ function buildWrappedFallback(contract: BaseContract): WrappedFallback {
tx.to = await contract.getAddress();

if (tx.from) {
tx.from = await resolveAddress(tx.from, getResolver(contract.runner));
tx.from = await resolveAddress(tx.from);
}

const iface = contract.interface;
Expand Down Expand Up @@ -715,28 +694,8 @@ export class BaseContract implements Addressable, EventEmitterable<ContractEvent

// Resolve the target as the address
if (typeof(target) === "string") {
if (isHexString(target)) {
addr = target;
addrPromise = Promise.resolve(target);

} else {
const resolver = getRunner(runner, "resolveName");
if (!canResolve(resolver)) {
throw makeError("contract runner does not support name resolution", "UNSUPPORTED_OPERATION", {
operation: "resolveName"
});
}

addrPromise = resolver.resolveName(target).then((addr) => {
if (addr == null) {
throw makeError("an ENS name used for a contract target must be correctly configured", "UNCONFIGURED_NAME", {
value: target
});
}
getInternal(this).addr = addr;
return addr;
});
}
} else {
addrPromise = target.getAddress().then((addr) => {
if (addr == null) { throw new Error("TODO"); }
Expand Down
56 changes: 1 addition & 55 deletions src.ts/hash/typed-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getAddress } from "../address/index.js";
import { keccak256 } from "../crypto/index.js";
import { recoverAddress } from "../transaction/index.js";
import {
concat, defineProperties, getBigInt, getBytes, hexlify, isHexString, mask, toBeHex, toQuantity, toTwos, zeroPadValue,
concat, defineProperties, getBigInt, getBytes, hexlify, mask, toBeHex, toQuantity, toTwos, zeroPadValue,
assertArgument
} from "../utils/index.js";

Expand Down Expand Up @@ -492,60 +492,6 @@ export class TypedDataEncoder {
return keccak256(TypedDataEncoder.encode(domain, types, value));
}

// Replaces all address types with ENS names with their looked up address
/**
* Resolves to the value from resolving all addresses in %%value%% for
* %%types%% and the %%domain%%.
*/
static async resolveNames(domain: TypedDataDomain, types: Record<string, Array<TypedDataField>>, value: Record<string, any>, resolveName: (name: string) => Promise<string>): Promise<{ domain: TypedDataDomain, value: any }> {
// Make a copy to isolate it from the object passed in
domain = Object.assign({ }, domain);

// Allow passing null to ignore value
for (const key in domain) {
if ((<Record<string, any>>domain)[key] == null) {
delete (<Record<string, any>>domain)[key];
}
}

// Look up all ENS names
const ensCache: Record<string, string> = { };

// Do we need to look up the domain's verifyingContract?
if (domain.verifyingContract && !isHexString(domain.verifyingContract, 20)) {
ensCache[domain.verifyingContract] = "0x";
}

// We are going to use the encoder to visit all the base values
const encoder = TypedDataEncoder.from(types);

// Get a list of all the addresses
encoder.visit(value, (type: string, value: any) => {
if (type === "address" && !isHexString(value, 20)) {
ensCache[value] = "0x";
}
return value;
});

// Lookup each name
for (const name in ensCache) {
ensCache[name] = await resolveName(name);
}

// Replace the domain verifyingContract if needed
if (domain.verifyingContract && ensCache[domain.verifyingContract]) {
domain.verifyingContract = ensCache[domain.verifyingContract];
}

// Replace all ENS names with their address
value = encoder.visit(value, (type: string, value: any) => {
if (type === "address" && ensCache[value]) { return ensCache[value]; }
return value;
});

return { domain, value };
}

/**
* Returns the JSON-encoded payload expected by nodes which implement
* the JSON-RPC [[link-eip-712]] method.
Expand Down
Loading

0 comments on commit 9161574

Please sign in to comment.