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

Update Qi UTXO related types #204

Merged
merged 20 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
23 changes: 11 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"aes-js": "4.0.0-beta.5",
"dotenv": "^16.4.1",
"google-protobuf": "^3.21.2",
"tslib": "^2.6.2",
"ws": "8.5.0"
},
"description": "A complete and compact Quai library, for dapps, wallets and any other tools.",
Expand All @@ -39,7 +40,6 @@
"rollup": "3.21.5",
"semver": "7.5.4",
"ts-mocha": "^10.0.0",
"tslib": "^2.6.2",
"typedoc": "^0.25.13",
"typedoc-plugin-frontmatter": "^1.0.0",
"typedoc-plugin-markdown": "^4.0.1",
Expand Down Expand Up @@ -143,7 +143,7 @@
"format": "prettier --write src/**/*.ts"
},
"sideEffects": false,
"version": "1.0.0-alpha",
"version": "1.0.0-alpha.1",
"husky": {
"hooks": {
"pre-commit": "lint-staged"
Expand Down
23 changes: 14 additions & 9 deletions src/providers/abstract-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// migrate the listener to the static event. We also need to maintain a map
// of Signer to address so we can sync respond to listenerCount.

import { getAddress, resolveAddress } from '../address/index.js';
import { computeAddress, resolveAddress } from '../address/index.js';
import { Shard, toShard, toZone, Zone } from '../constants/index.js';
import { TxInput, TxOutput } from '../transaction/index.js';
import { Outpoint } from '../transaction/utxo.js';
Expand Down Expand Up @@ -58,6 +58,7 @@ import type { Networkish } from './network.js';
import type {
BlockParams,
LogParams,
OutpointResponseParams,
QiTransactionResponseParams,
TransactionReceiptParams,
TransactionResponseParams,
Expand All @@ -76,7 +77,6 @@ import type {
import { WorkObjectLike } from '../transaction/work-object.js';
import { QiTransaction, QuaiTransaction } from '../transaction/index.js';
import { QuaiTransactionResponseParams } from './formatting.js';
import { keccak256, SigningKey } from '../crypto/index.js';

type Timer = ReturnType<typeof setTimeout>;

Expand Down Expand Up @@ -1098,12 +1098,8 @@ export class AbstractProvider<C = FetchRequest> implements Provider {

const addr = Array.isArray((<any>request)[key])
? 'address' in <any>request[key][0]
? (<TxOutput[]>(<any>request)[key]).map((it) => resolveAddress(hexlify(it.address)))
: (<TxInput[]>(<any>request)[key]).map((it) =>
getAddress(
keccak256('0x' + SigningKey.computePublicKey(it.pub_key).substring(4)).substring(26),
),
)
? (<TxOutput[]>(<any>request)[key]).map((it) => it.address)
: (<TxInput[]>(<any>request)[key]).map((it) => computeAddress(it.pubkey))
: resolveAddress((<any>request)[key]);
if (isPromise(addr)) {
if (Array.isArray(addr)) {
Expand Down Expand Up @@ -1320,7 +1316,16 @@ export class AbstractProvider<C = FetchRequest> implements Provider {
}

async getOutpointsByAddress(address: AddressLike): Promise<Outpoint[]> {
return await this.#getAccountValue({ method: 'getOutpointsByAddress' }, address, 'latest');
const outpoints: OutpointResponseParams[] = await this.#getAccountValue(
{ method: 'getOutpointsByAddress' },
address,
'latest',
);
return outpoints.map((outpoint: OutpointResponseParams) => ({
txhash: outpoint.Txhash,
index: outpoint.Index,
denomination: outpoint.Denomination,
}));
}

async getTransactionCount(address: AddressLike, blockTag?: BlockTag): Promise<number> {
Expand Down
23 changes: 21 additions & 2 deletions src/providers/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { getAddress } from '../address/index.js';
import { Signature } from '../crypto/index.js';
import { accessListify } from '../transaction/index.js';
import { hexlify } from '../utils/data';
import {
getBigInt,
getNumber,
Expand Down Expand Up @@ -380,8 +381,8 @@ export function formatTransactionResponse(value: any): TransactionResponseParams
index: allowNull((value: any) => (value ? BigInt(value) : null), null),
chainId: allowNull((value: any) => (value ? BigInt(value) : null), null),
signature: (value: any) => value,
txInputs: allowNull((value: any) => value, null),
txOutputs: allowNull((value: any) => value, null),
txInputs: allowNull((value: any) => value.map(_formatTxInput), null),
txOutputs: allowNull((value: any) => value.map(_formatTxOutput), null),
},
{
index: ['transactionIndex'],
Expand All @@ -396,3 +397,21 @@ export function formatTransactionResponse(value: any): TransactionResponseParams

return result;
}

const _formatTxInput = object(
{
txhash: formatHash,
index: getNumber,
pubkey: hexlify,
},
{
txhash: ['previous_out_point', 'hash', 'value'],
index: ['previous_out_point', 'index'],
pubkey: ['pub_key'],
},
);

const _formatTxOutput = object({
address: (addr: string) => hexlify(getAddress(addr)),
denomination: getNumber,
});
6 changes: 6 additions & 0 deletions src/providers/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,9 @@ export interface QiTransactionResponseParams {

txInputs?: TxInput[];
}

export interface OutpointResponseParams {
Txhash: string;
Index: number;
Denomination: number;
}
11 changes: 2 additions & 9 deletions src/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,14 @@ export { AbstractProvider, UnmanagedSubscriber } from './abstract-provider.js';

export { Network } from './network.js';

export { JsonRpcApiProvider, JsonRpcProvider } from './provider-jsonrpc.js';
export { JsonRpcApiProvider, JsonRpcProvider, JsonRpcSigner } from './provider-jsonrpc.js';

export { BrowserProvider } from './provider-browser.js';

export { SocketProvider } from './provider-socket.js';
export { WebSocketProvider } from './provider-websocket.js';

export {
Block,
FeeData,
Log,
TransactionReceipt,
TransactionResponse,
copyRequest
} from './provider.js';
export { Block, FeeData, Log, TransactionReceipt, TransactionResponse, copyRequest } from './provider.js';

export {
SocketSubscriber,
Expand Down
41 changes: 36 additions & 5 deletions src/providers/provider-browser.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { assertArgument } from '../utils/index.js';

import { JsonRpcApiProvider } from './provider-jsonrpc.js';
import { JsonRpcApiProvider, JsonRpcSigner } from './provider-jsonrpc.js';

import type {
JsonRpcError, JsonRpcPayload, JsonRpcResult
} from "./provider-jsonrpc.js";
import type { Networkish } from "./network.js";
import type { JsonRpcError, JsonRpcPayload, JsonRpcResult } from './provider-jsonrpc.js';
import type { Networkish } from './network.js';

/**
* The interface to an [EIP-1193](https://eips.ethereum.org/EIPS/eip-1193) provider, which is a standard used by most
Expand Down Expand Up @@ -73,6 +71,20 @@ export class BrowserProvider extends JsonRpcApiProvider {
};
}

async hasSigner(address: number | string): Promise<boolean> {
if (address == null) {
address = 0;
}

const accounts = await this.send('quai_accounts', []);
if (typeof address === 'number') {
return accounts.length > address;
}

address = address.toLowerCase();
return accounts.filter((a: string) => a.toLowerCase() === address).length !== 0;
}

async send(method: string, params: Array<any> | Record<string, any>): Promise<any> {
await this._start();

Expand Down Expand Up @@ -112,6 +124,25 @@ export class BrowserProvider extends JsonRpcApiProvider {
return super.getRpcError(payload, error);
}

async getSigner(address?: number | string): Promise<JsonRpcSigner> {
if (address == null) {
address = 0;
}

if (!(await this.hasSigner(address))) {
try {
//const resp =
await this.#request('quai_requestAccounts', []);
//console.log("RESP", resp);
} catch (error: any) {
const payload = error.payload;
throw this.getRpcError(payload, { id: payload.id, error });
}
}

return await super.getSigner(address);
}

/**
* Resolves to `true` if the provider manages the `address`.
*
Expand Down
Loading