@particle-network/provider
Advanced tools
Comparing version 0.10.5 to 0.11.0
@@ -392,3 +392,2 @@ var __defProp = Object.defineProperty; | ||
} | ||
console.log("Particle Provider Request", request); | ||
const rpcRequest = { | ||
@@ -509,3 +508,2 @@ chainId: Number(this.auth.chainId()), | ||
if (signerMethods.includes(rpcRequest.method)) { | ||
console.log("Particle Signer Provider Request", rpcRequest); | ||
return yield this.signerProvider.request(rpcRequest); | ||
@@ -519,3 +517,2 @@ } else { | ||
const particlePpcRequest = __spreadProps(__spreadValues({}, rpcRequest), { chainId: this.chainId }); | ||
console.log("Particle Provider Delegate Request", particlePpcRequest); | ||
return yield this.requestStrict(particlePpcRequest); | ||
@@ -522,0 +519,0 @@ } |
579
lib/index.js
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
var __create = Object.create; | ||
var __defProp = Object.defineProperty; | ||
var __defProps = Object.defineProperties; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropDescs = Object.getOwnPropertyDescriptors; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __getProtoOf = Object.getPrototypeOf; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __spreadValues = (a, b) => { | ||
for (var prop in b || (b = {})) | ||
if (__hasOwnProp.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
if (__getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(b)) { | ||
if (__propIsEnum.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
return a; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./provider"), exports); | ||
__exportStar(require("./delegate-provider"), exports); | ||
//# sourceMappingURL=index.js.map | ||
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
var __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
} | ||
return to; | ||
}; | ||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( | ||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, | ||
mod | ||
)); | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
var __async = (__this, __arguments, generator) => { | ||
return new Promise((resolve, reject) => { | ||
var fulfilled = (value) => { | ||
try { | ||
step(generator.next(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var rejected = (value) => { | ||
try { | ||
step(generator.throw(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); | ||
step((generator = generator.apply(__this, __arguments)).next()); | ||
}); | ||
}; | ||
// src/index.ts | ||
var src_exports = {}; | ||
__export(src_exports, { | ||
ParticleDelegateProvider: () => ParticleDelegateProvider, | ||
ParticleProvider: () => ParticleProvider | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
// src/connection.ts | ||
var import_auth = require("@particle-network/auth"); | ||
var import_axios = __toESM(require("axios"), 1); | ||
// src/types.ts | ||
var import_events = require("events"); | ||
var ProviderError = class extends Error { | ||
constructor(code, message, data) { | ||
super(message); | ||
this.code = code; | ||
this.message = message; | ||
this.data = data; | ||
this.code = code; | ||
this.message = message; | ||
this.data = data; | ||
} | ||
static userRejectedRequest() { | ||
return new ProviderError(4001, "The user rejected the request"); | ||
} | ||
static userCancelOperation() { | ||
return new ProviderError(4011, "The user cancel the operation"); | ||
} | ||
static unauthorized() { | ||
return new ProviderError(4100, "The requested method and/or account has not been authorized by the user"); | ||
} | ||
static unsupportedMethod() { | ||
return new ProviderError(4200, "The Provider does not support the requested method"); | ||
} | ||
static unsupportedChain() { | ||
return new ProviderError(4201, "The Provider does not support the chain"); | ||
} | ||
static disconnected() { | ||
return new ProviderError(4900, "The Provider is disconnected from all chains"); | ||
} | ||
static chainDisconnected() { | ||
return new ProviderError(4901, "The Provider is not connected to the requested chain"); | ||
} | ||
static paramsError() { | ||
return new ProviderError(8002, "Param error, see doc for more info"); | ||
} | ||
}; | ||
var IEvents = class { | ||
}; | ||
var IJsonRpcConnection = class extends IEvents { | ||
constructor() { | ||
super(...arguments); | ||
this.events = new import_events.EventEmitter(); | ||
} | ||
on(event, listener) { | ||
this.events.on(event, listener); | ||
} | ||
once(event, listener) { | ||
this.events.once(event, listener); | ||
} | ||
off(event, listener) { | ||
this.events.off(event, listener); | ||
} | ||
removeListener(event, listener) { | ||
this.events.removeListener(event, listener); | ||
} | ||
}; | ||
var IJsonRpcProvider = class extends IEvents { | ||
constructor() { | ||
super(...arguments); | ||
this.events = new import_events.EventEmitter(); | ||
} | ||
on(event, listener) { | ||
this.events.on(event, listener); | ||
} | ||
once(event, listener) { | ||
this.events.once(event, listener); | ||
} | ||
off(event, listener) { | ||
this.events.off(event, listener); | ||
} | ||
removeListener(event, listener) { | ||
this.events.removeListener(event, listener); | ||
} | ||
}; | ||
var notSupportMethods = ["eth_signTransaction", "eth_sign", "eth_sendRawTransaction"]; | ||
var signerMethods = [ | ||
"eth_requestAccounts", | ||
"eth_accounts", | ||
"eth_chainId", | ||
"eth_sendTransaction", | ||
"eth_signTypedData", | ||
"eth_signTypedData_v1", | ||
"eth_signTypedData_v3", | ||
"eth_signTypedData_v4", | ||
"personal_sign", | ||
"wallet_switchEthereumChain", | ||
"wallet_addEthereumChain", | ||
"wallet_watchAsset" | ||
]; | ||
// src/connection.ts | ||
var HttpConnection = class extends IJsonRpcConnection { | ||
constructor(config) { | ||
super(); | ||
this.config = config; | ||
this.registering = false; | ||
if (!(0, import_auth.isHttpUrl)(config.url)) { | ||
throw new Error(`Provided URL is not compatible with HTTP connection: ${config.url}`); | ||
} | ||
this.config = config; | ||
} | ||
get connected() { | ||
return typeof this.api !== "undefined"; | ||
} | ||
get connecting() { | ||
return this.registering; | ||
} | ||
open() { | ||
return __async(this, null, function* () { | ||
this.api = yield this.register(); | ||
}); | ||
} | ||
close() { | ||
return __async(this, null, function* () { | ||
this.onClose(); | ||
}); | ||
} | ||
send(request) { | ||
return __async(this, null, function* () { | ||
if (typeof this.api === "undefined") { | ||
this.api = yield this.register(); | ||
} | ||
return this.api.post("/", request).then((res) => res.data); | ||
}); | ||
} | ||
register() { | ||
return __async(this, null, function* () { | ||
const connectConfig = this.config; | ||
if (!(0, import_auth.isHttpUrl)(connectConfig.url)) { | ||
throw new Error(`Provided URL is not compatible with HTTP connection: ${connectConfig.url}`); | ||
} | ||
if (this.registering) { | ||
return new Promise((resolve, reject) => { | ||
this.events.once("open", () => { | ||
if (typeof this.api === "undefined") { | ||
return reject(new Error("HTTP connection is missing or invalid")); | ||
} | ||
resolve(this.api); | ||
}); | ||
}); | ||
} | ||
this.registering = true; | ||
const api = import_axios.default.create({ | ||
baseURL: connectConfig.url, | ||
timeout: 3e4, | ||
headers: { | ||
Accept: "application/json", | ||
"Content-Type": "application/json" | ||
} | ||
}); | ||
api.interceptors.request.use( | ||
function(config) { | ||
var _a, _b; | ||
if (config.headers) { | ||
config.headers["Authorization"] = connectConfig.basicCredentials; | ||
} else { | ||
config.headers = { | ||
Authorization: connectConfig.basicCredentials | ||
}; | ||
} | ||
if (!config.params) { | ||
config.params = {}; | ||
} | ||
config.params.chainId = (_b = (_a = config.data) == null ? void 0 : _a.chainId) != null ? _b : connectConfig.chainId(); | ||
config.params.projectUuid = connectConfig.authentication.projectId; | ||
config.params.projectKey = connectConfig.authentication.clientKey; | ||
return config; | ||
}, | ||
(error) => Promise.reject(error) | ||
); | ||
this.onOpen(api); | ||
return api; | ||
}); | ||
} | ||
onOpen(api) { | ||
this.api = api; | ||
this.registering = false; | ||
this.events.emit("open"); | ||
} | ||
onClose() { | ||
this.api = void 0; | ||
this.events.emit("close"); | ||
} | ||
}; | ||
// src/provider.ts | ||
var import_uuid = require("uuid"); | ||
// src/auth-adapter.ts | ||
var import_auth2 = require("@particle-network/auth"); | ||
var import_common = require("@particle-network/common"); | ||
var AuthEVMAdapter = class { | ||
constructor(auth) { | ||
this.auth = auth; | ||
this.auth = auth; | ||
} | ||
request(request) { | ||
return __async(this, null, function* () { | ||
if (request.method === "eth_requestAccounts" || request.method === "eth_accounts") { | ||
let wallet = this.auth.wallet(); | ||
if (wallet) { | ||
return [wallet.public_address]; | ||
} | ||
yield this.auth.login(); | ||
wallet = this.auth.wallet(); | ||
if (wallet) { | ||
return [wallet.public_address]; | ||
} else { | ||
throw new Error("Create wallet failed"); | ||
} | ||
} else if (request.method === "eth_chainId") { | ||
return (0, import_auth2.intToHex)(this.auth.chainId()); | ||
} else if (request.method === "eth_sendTransaction") { | ||
if (request.params && request.params instanceof Array && request.params[0]) { | ||
const txData = request.params[0]; | ||
if ((0, import_auth2.isNullish)(txData.type)) { | ||
if (import_common.chains.isChainSupportEIP1559(this.auth.chain())) { | ||
txData.type = "0x2"; | ||
} else { | ||
txData.type = "0x0"; | ||
} | ||
} | ||
if ((0, import_auth2.isNullish)(txData.chainId)) { | ||
txData.chainId = (0, import_auth2.intToHex)(this.auth.chainId()); | ||
} | ||
if ((0, import_auth2.isNullish)(txData.nonce)) { | ||
txData.nonce = "0x0"; | ||
} | ||
if ((0, import_auth2.isNullish)(txData.data)) { | ||
txData.data = "0x"; | ||
} | ||
return this.auth.sendTransaction(this.legacyToString(request.params[0])); | ||
} else { | ||
return Promise.reject(ProviderError.paramsError()); | ||
} | ||
} else if (request.method === "eth_signTypedData_v3" || request.method === "eth_signTypedData_v4") { | ||
if (request.params && request.params instanceof Array && request.params.length >= 2) { | ||
let typedData = request.params[1]; | ||
if (typeof typedData === "string" && !typedData.startsWith("0x")) { | ||
typedData = (0, import_auth2.addHexPrefix)(Buffer.from(typedData).toString("hex")); | ||
} | ||
return this.auth.sign(request.method, this.legacyToString(typedData)); | ||
} else { | ||
return Promise.reject(ProviderError.paramsError()); | ||
} | ||
} else if (request.method === "eth_signTypedData" || request.method === "eth_signTypedData_v1") { | ||
if (request.params && request.params instanceof Array && request.params[0]) { | ||
let typedData = request.params[0]; | ||
if (typeof typedData === "string" && !typedData.startsWith("0x")) { | ||
typedData = (0, import_auth2.addHexPrefix)(Buffer.from(typedData).toString("hex")); | ||
} | ||
return this.auth.sign(request.method, this.legacyToString(typedData)); | ||
} else { | ||
return Promise.reject(ProviderError.paramsError()); | ||
} | ||
} else if (request.method === "personal_sign") { | ||
if (request.params && request.params instanceof Array && request.params[0]) { | ||
return this.auth.sign(request.method, this.legacyToString(request.params[0])); | ||
} else { | ||
return Promise.reject(ProviderError.paramsError()); | ||
} | ||
} else if (request.method === "wallet_switchEthereumChain") { | ||
if (request.params && request.params instanceof Array && request.params[0] && request.params[0].chainId) { | ||
const chainId = Number(request.params[0].chainId); | ||
const chain = import_common.chains.getEVMChainInfoById(chainId); | ||
if (chain) { | ||
yield this.auth.switchChain(chain); | ||
return Promise.resolve(null); | ||
} | ||
return Promise.reject(ProviderError.unsupportedChain()); | ||
} else { | ||
return Promise.reject(ProviderError.paramsError()); | ||
} | ||
} else { | ||
return Promise.reject(ProviderError.unsupportedMethod()); | ||
} | ||
}); | ||
} | ||
legacyToString(params) { | ||
let message; | ||
if (typeof params === "number") { | ||
message = (0, import_auth2.addHexPrefix)(params.toString(16)); | ||
} else if (typeof params === "string") { | ||
if (params.toString().startsWith("0x")) { | ||
message = params; | ||
} else { | ||
message = (0, import_auth2.addHexPrefix)(Buffer.from(params).toString("hex")); | ||
} | ||
} else { | ||
message = (0, import_auth2.addHexPrefix)(Buffer.from(JSON.stringify(params)).toString("hex")); | ||
} | ||
return message; | ||
} | ||
}; | ||
// src/provider.ts | ||
var import_auth3 = require("@particle-network/auth"); | ||
var ParticleProvider = class extends IJsonRpcProvider { | ||
constructor(auth) { | ||
super(); | ||
this.auth = auth; | ||
this.isParticleNetwork = true; | ||
this.auth = auth; | ||
this.connection = this.setConnection(); | ||
this.authAdapter = new AuthEVMAdapter(this.auth); | ||
this.auth.on("chainChanged", (chain) => { | ||
if (chain.name !== "Solana") { | ||
this.emit("chainChanged", (0, import_auth3.intToHex)(chain.id)); | ||
} | ||
}); | ||
if (typeof window !== "undefined" && window.particle) { | ||
window.particle.particleProvider = this; | ||
} | ||
} | ||
setConnection() { | ||
return new HttpConnection({ | ||
url: (0, import_auth3.isParticleDev)() ? "https://rpc-debug.particle.network/evm-chain" : "https://rpc.particle.network/evm-chain", | ||
basicCredentials: this.auth.basicCredentials(), | ||
chainId: () => this.auth.chainId(), | ||
authentication: this.auth.config | ||
}); | ||
} | ||
emit(eventName, ...args) { | ||
return this.events.emit(eventName, ...args); | ||
} | ||
disconnect() { | ||
return __async(this, null, function* () { | ||
return this.auth.logout(); | ||
}); | ||
} | ||
enable() { | ||
return __async(this, null, function* () { | ||
return this.request({ | ||
method: "eth_requestAccounts" | ||
}); | ||
}); | ||
} | ||
request(request) { | ||
return __async(this, null, function* () { | ||
var _a, _b; | ||
if (!request.method || notSupportMethods.includes(request.method)) { | ||
return Promise.reject(ProviderError.unsupportedMethod()); | ||
} | ||
if (!this.connection.connected) { | ||
yield this.open(); | ||
} | ||
const rpcRequest = { | ||
chainId: Number(this.auth.chainId()), | ||
id: (_a = request.id) != null ? _a : (0, import_uuid.v4)(), | ||
jsonrpc: (_b = request.jsonrpc) != null ? _b : "2.0", | ||
method: request.method, | ||
params: request.params | ||
}; | ||
if (signerMethods.includes(request.method)) { | ||
return this.authAdapter.request(request); | ||
} else { | ||
return this.requestStrict(rpcRequest); | ||
} | ||
}); | ||
} | ||
requestStrict(request) { | ||
return __async(this, null, function* () { | ||
return this.connection.send(request).then((output) => { | ||
if (output.error) { | ||
return Promise.reject(output.error); | ||
} else { | ||
return Promise.resolve(output.result); | ||
} | ||
}); | ||
}); | ||
} | ||
open() { | ||
return __async(this, null, function* () { | ||
yield this.connection.open(); | ||
this.connection.on("close", () => this.emit("disconnect")); | ||
this.emit("connect", { | ||
chainId: (0, import_auth3.intToHex)(this.auth.chainId()) | ||
}); | ||
}); | ||
} | ||
close() { | ||
return __async(this, null, function* () { | ||
yield this.connection.close(); | ||
}); | ||
} | ||
}; | ||
// src/delegate-provider.ts | ||
var import_uuid2 = require("uuid"); | ||
var import_auth4 = require("@particle-network/auth"); | ||
var ParticleDelegateProvider = class extends IJsonRpcProvider { | ||
constructor(auth, signerProvider) { | ||
super(); | ||
this.auth = auth; | ||
this.signerProvider = signerProvider; | ||
this.isParticleDelegateProvider = true; | ||
this.connection = this.setConnection(); | ||
this.listenEvent(); | ||
} | ||
listenEvent() { | ||
const event = this.events; | ||
this.signerProvider.on("connect", (result) => event.emit("connect", result)); | ||
this.signerProvider.on("disconnect", (result) => event.emit("disconnect", result)); | ||
this.signerProvider.on("message", (result) => event.emit("message", result)); | ||
this.signerProvider.on("chainChanged", (result) => event.emit("chainChanged", result)); | ||
this.signerProvider.on("accountsChanged", (result) => event.emit("accountsChanged", result)); | ||
} | ||
setConnection() { | ||
return new HttpConnection({ | ||
url: (0, import_auth4.isParticleDev)() ? "https://rpc-debug.particle.network/evm-chain" : "https://rpc.particle.network/evm-chain", | ||
basicCredentials: this.auth.basicCredentials(), | ||
chainId: () => { | ||
var _a; | ||
return (_a = this.chainId) != null ? _a : this.auth.chainId(); | ||
}, | ||
authentication: this.auth.config | ||
}); | ||
} | ||
disconnect() { | ||
return __async(this, null, function* () { | ||
const provider = this.signerProvider; | ||
if (provider.disconnect && typeof provider.disconnect === "function") { | ||
try { | ||
yield provider.disconnect(); | ||
} catch (e) { | ||
} | ||
} | ||
}); | ||
} | ||
enable() { | ||
return __async(this, null, function* () { | ||
const provider = this.signerProvider; | ||
let result; | ||
if (provider.enable && typeof provider.enable === "function") { | ||
try { | ||
result = yield provider.enable(); | ||
} catch (e) { | ||
result = yield this.request({ | ||
method: "eth_requestAccounts" | ||
}); | ||
} | ||
} | ||
return result; | ||
}); | ||
} | ||
request(request) { | ||
return __async(this, null, function* () { | ||
var _a, _b; | ||
if (!request.method || notSupportMethods.includes(request.method)) { | ||
return Promise.reject(ProviderError.unsupportedMethod()); | ||
} | ||
if (!this.connection.connected) { | ||
yield this.open(); | ||
} | ||
const rpcRequest = { | ||
id: (_a = request.id) != null ? _a : (0, import_uuid2.v4)(), | ||
jsonrpc: (_b = request.jsonrpc) != null ? _b : "2.0", | ||
method: request.method, | ||
params: request.params | ||
}; | ||
if (signerMethods.includes(rpcRequest.method)) { | ||
return yield this.signerProvider.request(rpcRequest); | ||
} else { | ||
try { | ||
this.chainId = Number(yield this.signerProvider.request({ method: "eth_chainId" })); | ||
} catch (error) { | ||
this.chainId = Number(this.auth.chainId()); | ||
} | ||
const particlePpcRequest = __spreadProps(__spreadValues({}, rpcRequest), { chainId: this.chainId }); | ||
return yield this.requestStrict(particlePpcRequest); | ||
} | ||
}); | ||
} | ||
requestStrict(request) { | ||
return __async(this, null, function* () { | ||
return this.connection.send(request).then((output) => { | ||
if (output.error) { | ||
return Promise.reject(output.error); | ||
} else { | ||
return Promise.resolve(output.result); | ||
} | ||
}); | ||
}); | ||
} | ||
open() { | ||
return __async(this, null, function* () { | ||
yield this.connection.open(); | ||
}); | ||
} | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@particle-network/provider", | ||
"version": "0.10.5", | ||
"version": "0.11.0", | ||
"files": [ | ||
@@ -9,2 +9,3 @@ "es", | ||
], | ||
"type": "module", | ||
"main": "lib/index.js", | ||
@@ -18,3 +19,3 @@ "module": "es/index.js", | ||
"peerDependencies": { | ||
"@particle-network/auth": "^0.10.4" | ||
"@particle-network/auth": "^0.11.0" | ||
}, | ||
@@ -26,3 +27,3 @@ "dependencies": { | ||
"devDependencies": { | ||
"@particle-network/auth": "^0.10.5", | ||
"@particle-network/auth": "^0.11.0", | ||
"@types/uuid": "^8.3.4", | ||
@@ -33,7 +34,7 @@ "ts-loader": "^9.3.1", | ||
"scripts": { | ||
"build": "yarn clean && tsc -p tsconfig.json && node ./esBuild.js", | ||
"build": "yarn clean && node ./esBuild.js && tsc --emitDeclarationOnly -p tsconfig.json", | ||
"clean": "shx rm -rf lib/* && shx rm -rf es/*", | ||
"build:min.js": "webpack" | ||
}, | ||
"gitHead": "93cf2df58a6ce2e4395a7b5ce3b9efe22e316efa" | ||
"gitHead": "15ad6ae874627274d836c065f8d10abadbb3bcfd" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
120374
Yes
13
1241