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

add send tx in base provider #93

Merged
merged 2 commits into from
Sep 20, 2023
Merged
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
7 changes: 7 additions & 0 deletions .changeset/twelve-otters-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@zcloak/login-rpc-defines": minor
"@zcloak/login-providers": minor
"@zcloak/login-did": minor
---

add send transaction in base provider
5 changes: 4 additions & 1 deletion login/did/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
"version": "2.0.1",
"main": "index.js",
"dependencies": {
"@ethersproject/bignumber": "^5.7.0",
"@ethersproject/transactions": "^5.7.0",
"@polkadot/util": "^12.1.2",
"@zcloak/did": "workspace:^",
"@zcloak/did-resolver": "workspace:^",
"@zcloak/login-providers": "workspace:^"
"@zcloak/login-providers": "workspace:^",
"ethers": "^6.7.1"
}
}
22 changes: 22 additions & 0 deletions login/did/src/LoginDid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import type { DidKeys, EncryptedData, IDidDetails, IDidKeyring, SignedData } fro
import type { DidUrl } from '@zcloak/did-resolver/types';
import type { BaseProvider } from '@zcloak/login-providers/base/Provider';

import { UnsignedTransaction } from '@ethersproject/transactions';
import { hexToU8a } from '@polkadot/util';

import { Did } from '@zcloak/did';
import { parseDidDocument } from '@zcloak/did/did/helpers';

import { bigNumberishToHex } from './util';

type HexString = `0x${string}`;

export class LoginDid extends Did implements IDidKeyring {
Expand Down Expand Up @@ -78,4 +81,23 @@ export class LoginDid extends Did implements IDidKeyring {
signature: hexToU8a(signature)
};
}

public override async sendTransaction(
tx: UnsignedTransaction,
providerUrl: string,
keyOrDidUrl: DidUrl | Exclude<DidKeys, 'keyAgreement'> = 'controller'
): Promise<any> {
const _tx: UnsignedTransaction = {
...tx,
gasLimit: tx.gasLimit ? bigNumberishToHex(tx.gasLimit) : undefined,
gasPrice: tx.gasPrice ? bigNumberishToHex(tx.gasPrice) : undefined,
value: tx.value ? bigNumberishToHex(tx.value) : undefined,
maxPriorityFeePerGas: tx.maxPriorityFeePerGas ? bigNumberishToHex(tx.maxPriorityFeePerGas) : undefined,
maxFeePerGas: tx.maxFeePerGas ? bigNumberishToHex(tx.maxFeePerGas) : undefined
};

const result = await this.provider.sendTransaction(_tx, providerUrl, keyOrDidUrl);

return result;
}
}
26 changes: 26 additions & 0 deletions login/did/src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2021-2023 zcloak authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { BigNumber, BigNumberish } from '@ethersproject/bignumber';
import { isHex } from '@polkadot/util';
import { ethers, toBeHex } from 'ethers';

export const bigNumberishToHex = (value: BigNumberish): string => {
if (isHex(value)) {
return value;
}

if (typeof value === 'bigint' || typeof value === 'number' || typeof value === 'string') {
return toBeHex(value);
}

if (ethers.isBytesLike(value)) {
return ethers.hexlify(value);
}

if (value instanceof BigNumber) {
return value.toHexString();
}

throw new Error(`Convert ${value} to hex failed.`);
};
1 change: 1 addition & 0 deletions login/providers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"version": "2.0.0",
"main": "index.js",
"dependencies": {
"@ethersproject/transactions": "^5.7.0",
"@polkadot/util": "^12.1.2",
"@zcloak/login-rpc": "workspace:^",
"@zcloak/login-rpc-defines": "workspace:^",
Expand Down
9 changes: 9 additions & 0 deletions login/providers/src/base/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { Request, RpcRequest, RpcResponse } from '@zcloak/login-rpc';
import type { VerifiablePresentation } from '@zcloak/vc/types';
import type { ProviderEvents } from '../types';

import { UnsignedTransaction } from '@ethersproject/transactions';
import { isHex, isString, isU8a, numberToHex, stringToHex, u8aToHex } from '@polkadot/util';
import Events from 'eventemitter3';

Expand Down Expand Up @@ -152,4 +153,12 @@ export class BaseProvider extends Events<ProviderEvents> {
public generateZkp(params: RpcRequest<'proof_generate'>): Promise<RpcResponse<'proof_generate'>> {
return this.request('proof_generate', params);
}

public async sendTransaction(
tx: UnsignedTransaction,
providerUrl: string,
keyOrDidUrl: DidUrl | Exclude<DidKeys, 'keyAgreement'> = 'controller'
): Promise<any> {
return this.request('send_tx', { tx, providerUrl, keyOrDidUrl });
}
}
1 change: 1 addition & 0 deletions login/rpc-defines/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"version": "2.0.0",
"main": "index.js",
"dependencies": {
"@ethersproject/transactions": "^5.7.0",
"@zcloak/login-rpc": "workspace:^"
},
"devDependencies": {
Expand Down
9 changes: 9 additions & 0 deletions login/rpc-defines/src/defineZk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type { DidKeys } from '@zcloak/did/types';
import type { DidDocument, DidUrl, SignatureType, VerificationMethodType } from '@zcloak/did-resolver/types';
import type { VerifiablePresentation } from '@zcloak/vc/types';

import { UnsignedTransaction } from '@ethersproject/transactions';

export type HexString = `0x${string}`;

export type DidSignature = {
Expand Down Expand Up @@ -76,6 +78,12 @@ export type ZkpGenRequest = {
publicInput?: string;
};

export type SendTx = {
tx: UnsignedTransaction;
providerUrl: string;
keyOrDidUrl: DidUrl | Exclude<DidKeys, 'keyAgreement'>;
};

declare module '@zcloak/login-rpc/rpcs' {
interface Rpcs {
wallet_requestAuth: [undefined, boolean];
Expand All @@ -90,6 +98,7 @@ declare module '@zcloak/login-rpc/rpcs' {
did_encrypt: [DidEncryptParams, DidEncrypted];
did_decrypt: [DidDecryptParams, HexString];
proof_generate: [ZkpGenRequest, ZkpGenResponse];
send_tx: [SendTx, any];
}

interface RpcEvents {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4424,17 +4424,21 @@ __metadata:
version: 0.0.0-use.local
resolution: "@zcloak/login-did@workspace:login/did"
dependencies:
"@ethersproject/bignumber": "npm:^5.7.0"
"@ethersproject/transactions": "npm:^5.7.0"
"@polkadot/util": "npm:^12.1.2"
"@zcloak/did": "workspace:^"
"@zcloak/did-resolver": "workspace:^"
"@zcloak/login-providers": "workspace:^"
ethers: "npm:^6.7.1"
languageName: unknown
linkType: soft

"@zcloak/login-providers@workspace:^, @zcloak/login-providers@workspace:login/providers":
version: 0.0.0-use.local
resolution: "@zcloak/login-providers@workspace:login/providers"
dependencies:
"@ethersproject/transactions": "npm:^5.7.0"
"@polkadot/util": "npm:^12.1.2"
"@zcloak/login-rpc": "workspace:^"
"@zcloak/login-rpc-defines": "workspace:^"
Expand All @@ -4446,6 +4450,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@zcloak/login-rpc-defines@workspace:login/rpc-defines"
dependencies:
"@ethersproject/transactions": "npm:^5.7.0"
"@kiltprotocol/types": "npm:^0.28.2"
"@zcloak/login-rpc": "workspace:^"
languageName: unknown
Expand Down