@astrox/icsnap-adapter
Advanced tools
Comparing version
import { ICPSnapApi, SignRawMessageResponse } from '@astrox/icsnap-types'; | ||
import { PublicKey, Signature, SignIdentity } from '@dfinity/agent'; | ||
import { Secp256k1KeyIdentity } from '@dfinity/identity'; | ||
import { Principal } from '@dfinity/principal'; | ||
@@ -7,5 +8,5 @@ export declare class SnapIdentity extends SignIdentity { | ||
private _api; | ||
private _rawPublickeyString; | ||
private _principalString; | ||
constructor(_api: ICPSnapApi, _rawPublickeyString: string, _principalString: string); | ||
private _delegationChain; | ||
private _sessionKey; | ||
constructor(_api: ICPSnapApi, _delegationChain: string, _sessionKey: Secp256k1KeyIdentity); | ||
getPublicKey(): PublicKey; | ||
@@ -12,0 +13,0 @@ getPrincipal(): Principal; |
@@ -7,32 +7,20 @@ "use strict"; | ||
const agent_1 = require("@dfinity/agent"); | ||
const util_1 = require("./util"); | ||
const identity_1 = require("@dfinity/identity"); | ||
const principal_1 = require("@dfinity/principal"); | ||
class SnapIdentity extends agent_1.SignIdentity { | ||
constructor(_api, _rawPublickeyString, _principalString) { | ||
constructor(_api, _delegationChain, _sessionKey) { | ||
super(); | ||
this._api = _api; | ||
this._rawPublickeyString = _rawPublickeyString; | ||
this._principalString = _principalString; | ||
this._delegationChain = _delegationChain; | ||
this._sessionKey = _sessionKey; | ||
_SnapIdentity_innerIdentity.set(this, void 0); | ||
tslib_1.__classPrivateFieldSet(this, _SnapIdentity_innerIdentity, identity_1.DelegationIdentity.fromDelegation(this._sessionKey, identity_1.DelegationChain.fromJSON(JSON.parse(this._delegationChain))), "f"); | ||
} | ||
getPublicKey() { | ||
return identity_1.Secp256k1PublicKey.fromRaw((0, util_1.fromHexString)(this._rawPublickeyString)); | ||
return tslib_1.__classPrivateFieldGet(this, _SnapIdentity_innerIdentity, "f").getPublicKey(); | ||
} | ||
getPrincipal() { | ||
return principal_1.Principal.fromText(this._principalString); | ||
return tslib_1.__classPrivateFieldGet(this, _SnapIdentity_innerIdentity, "f").getPrincipal(); | ||
} | ||
async sign(blob) { | ||
var _a; | ||
try { | ||
// console.log(toHexString(blob)); | ||
// const signedResponse = await this._api.sign(toHexString(blob)); | ||
// if (signedResponse.error) { | ||
// throw signedResponse.error; | ||
// } | ||
// if (signedResponse.confirmed === false) { | ||
// throw new Error(`signing message error: signing process terminated by user`); | ||
// } | ||
// return fromHexString(signedResponse.signature) as Signature; | ||
tslib_1.__classPrivateFieldSet(this, _SnapIdentity_innerIdentity, (_a = tslib_1.__classPrivateFieldGet(this, _SnapIdentity_innerIdentity, "f")) !== null && _a !== void 0 ? _a : identity_1.Secp256k1KeyIdentity.fromJSON(await this._api.getIdentity()), "f"); | ||
const sig = await tslib_1.__classPrivateFieldGet(this, _SnapIdentity_innerIdentity, "f").sign(blob); | ||
@@ -39,0 +27,0 @@ return sig; |
@@ -1,2 +0,2 @@ | ||
export { getIdentity, configure, sign, signRawMessage, getPrincipal } from './methods'; | ||
export { configure, sign, signRawMessage, getPrincipal } from './methods'; | ||
export { MetamaskICPSnap, enableICPSnap } from './snap'; | ||
@@ -3,0 +3,0 @@ import './types'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SnapIdentity = exports.isSnapInstalled = exports.isMetamaskSnapsSupported = exports.getWalletSnaps = exports.hasMetaMask = exports.enableICPSnap = exports.MetamaskICPSnap = exports.getPrincipal = exports.signRawMessage = exports.sign = exports.configure = exports.getIdentity = void 0; | ||
exports.SnapIdentity = exports.isSnapInstalled = exports.isMetamaskSnapsSupported = exports.getWalletSnaps = exports.hasMetaMask = exports.enableICPSnap = exports.MetamaskICPSnap = exports.getPrincipal = exports.signRawMessage = exports.sign = exports.configure = void 0; | ||
var methods_1 = require("./methods"); | ||
Object.defineProperty(exports, "getIdentity", { enumerable: true, get: function () { return methods_1.getIdentity; } }); | ||
Object.defineProperty(exports, "configure", { enumerable: true, get: function () { return methods_1.configure; } }); | ||
@@ -7,0 +6,0 @@ Object.defineProperty(exports, "sign", { enumerable: true, get: function () { return methods_1.sign; } }); |
import { SignRawMessageResponse, SignMessageResponse, SnapConfig } from '@astrox/icsnap-types'; | ||
import { MetamaskICPSnap } from './snap'; | ||
export declare function getIdentity(this: MetamaskICPSnap): Promise<string>; | ||
export declare function requestDelegationChain(this: MetamaskICPSnap, sessionPublicKey: string, milliseconds?: number, canisterIds?: string): Promise<string>; | ||
export declare function configure(this: MetamaskICPSnap, configuration: SnapConfig): Promise<void>; | ||
@@ -5,0 +5,0 @@ export declare function sign(this: MetamaskICPSnap, message: string): Promise<SignMessageResponse>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getRawPublicKey = exports.getPrincipal = exports.signRawMessage = exports.sign = exports.configure = exports.getIdentity = void 0; | ||
exports.getRawPublicKey = exports.getPrincipal = exports.signRawMessage = exports.sign = exports.configure = exports.requestDelegationChain = void 0; | ||
async function sendSnapMethod(request, snapId) { | ||
@@ -10,6 +10,9 @@ return await window.ethereum.request({ | ||
} | ||
async function getIdentity() { | ||
return await sendSnapMethod({ method: 'icp_getIdentity' }, this.snapId); | ||
// export async function getIdentity(this: MetamaskICPSnap): Promise<string> { | ||
// return await sendSnapMethod({ method: 'icp_getIdentity' }, this.snapId); | ||
// } | ||
async function requestDelegationChain(sessionPublicKey, milliseconds, canisterIds) { | ||
return await sendSnapMethod({ method: 'icp_requestDelegationChain', params: { sessionPublicKey: sessionPublicKey, milliseconds: milliseconds, canisterIds: canisterIds } }, this.snapId); | ||
} | ||
exports.getIdentity = getIdentity; | ||
exports.requestDelegationChain = requestDelegationChain; | ||
async function configure(configuration) { | ||
@@ -16,0 +19,0 @@ return await sendSnapMethod({ method: 'icp_configure', params: { configuration: configuration } }, this.snapId); |
import { ICPSnapApi, SnapConfig } from '@astrox/icsnap-types'; | ||
import { SnapIdentity } from './identity'; | ||
import { Secp256k1PublicKey } from '@dfinity/identity'; | ||
export declare class MetamaskICPSnap { | ||
protected readonly snapOrigin: string; | ||
protected readonly snapId: string; | ||
createSnapIdentity(): Promise<SnapIdentity>; | ||
constructor(snapOrigin: string); | ||
@@ -11,2 +10,6 @@ getICPSnapApi: () => Promise<ICPSnapApi>; | ||
export declare type SnapInstallationParamNames = 'version' | string; | ||
export interface EnableICPSnapResult { | ||
snap: MetamaskICPSnap; | ||
delegationChainString: string; | ||
} | ||
/** | ||
@@ -25,3 +28,8 @@ * Install and enable ICP snap | ||
*/ | ||
export declare function enableICPSnap(config: Partial<SnapConfig>, snapOrigin?: string, snapInstallationParams?: Record<SnapInstallationParamNames, unknown>): Promise<MetamaskICPSnap>; | ||
export declare function enableICPSnap({ config, snapOrigin, snapInstallationParams, sessionPublicKey, }: { | ||
config: Partial<SnapConfig>; | ||
snapOrigin?: string; | ||
snapInstallationParams: Record<SnapInstallationParamNames, unknown>; | ||
sessionPublicKey: Secp256k1PublicKey; | ||
}): Promise<EnableICPSnapResult>; | ||
//# sourceMappingURL=snap.d.ts.map |
@@ -6,3 +6,2 @@ "use strict"; | ||
const methods_1 = require("./methods"); | ||
const identity_1 = require("./identity"); | ||
class MetamaskICPSnap { | ||
@@ -13,3 +12,3 @@ constructor(snapOrigin) { | ||
configure: methods_1.configure.bind(this), | ||
getIdentity: methods_1.getIdentity.bind(this), | ||
requestDelegationChain: methods_1.requestDelegationChain.bind(this), | ||
sign: methods_1.sign.bind(this), | ||
@@ -24,8 +23,2 @@ signRawMessage: methods_1.signRawMessage.bind(this), | ||
} | ||
async createSnapIdentity() { | ||
const api = await this.getICPSnapApi(); | ||
const publicKey = await api.getRawPublicKey(); | ||
const principal = await api.getPrincipal(); | ||
return new identity_1.SnapIdentity(api, publicKey, principal); | ||
} | ||
} | ||
@@ -47,3 +40,3 @@ exports.MetamaskICPSnap = MetamaskICPSnap; | ||
*/ | ||
async function enableICPSnap(config, snapOrigin, snapInstallationParams = {}) { | ||
async function enableICPSnap({ config, snapOrigin, snapInstallationParams = {}, sessionPublicKey, }) { | ||
const snapId = snapOrigin !== null && snapOrigin !== void 0 ? snapOrigin : defaultSnapOrigin; | ||
@@ -76,7 +69,8 @@ // check all conditions | ||
// set initial configuration | ||
await (await snap.getICPSnapApi()).configure(config); | ||
// return snap object | ||
return snap; | ||
const api = await snap.getICPSnapApi(); | ||
await api.configure(config); | ||
const delegationChainString = await api.requestDelegationChain((0, util_1.toHexString)(sessionPublicKey.toDer())); | ||
return { snap, delegationChainString }; | ||
} | ||
exports.enableICPSnap = enableICPSnap; | ||
//# sourceMappingURL=snap.js.map |
@@ -33,3 +33,2 @@ "use strict"; | ||
async function isSnapInstalled(snapOrigin, version) { | ||
console.log(await getWalletSnaps()); | ||
try { | ||
@@ -36,0 +35,0 @@ return !!Object.values(await getWalletSnaps()).find(permission => permission.id === snapOrigin && (!version || permission.version === version)); |
{ | ||
"name": "@astrox/icsnap-adapter", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"main": "build/index.js", | ||
@@ -27,3 +27,3 @@ "module": "build/index.js", | ||
"dependencies": { | ||
"@astrox/icsnap-types": "^0.0.9", | ||
"@astrox/icsnap-types": "^0.0.10", | ||
"@dfinity/agent": "^0.11.3", | ||
@@ -33,3 +33,3 @@ "@dfinity/candid": "^0.11.3", | ||
}, | ||
"gitHead": "852161758c1834f4112e0f8bc65c245000566c4c" | ||
"gitHead": "cb46449abd6100698484dd6c054ef335fb00f5b8" | ||
} |
import { ICPSnapApi, SignRawMessageResponse, SignMessageResponse } from '@astrox/icsnap-types'; | ||
import { PublicKey, Signature, SignIdentity } from '@dfinity/agent'; | ||
import { fromHexString, toHexString } from './util'; | ||
import { Secp256k1KeyIdentity, Secp256k1PublicKey } from '@dfinity/identity'; | ||
import { DelegationChain, DelegationIdentity, Secp256k1KeyIdentity, Secp256k1PublicKey } from '@dfinity/identity'; | ||
import { Principal } from '@dfinity/principal'; | ||
export class SnapIdentity extends SignIdentity { | ||
#innerIdentity: Secp256k1KeyIdentity; | ||
constructor(private _api: ICPSnapApi, private _rawPublickeyString: string, private _principalString: string) { | ||
#innerIdentity: DelegationIdentity; | ||
constructor(private _api: ICPSnapApi, private _delegationChain: string, private _sessionKey: Secp256k1KeyIdentity) { | ||
super(); | ||
this.#innerIdentity = DelegationIdentity.fromDelegation(this._sessionKey, DelegationChain.fromJSON(JSON.parse(this._delegationChain))); | ||
} | ||
getPublicKey(): PublicKey { | ||
return Secp256k1PublicKey.fromRaw(fromHexString(this._rawPublickeyString)); | ||
return this.#innerIdentity.getPublicKey(); | ||
} | ||
getPrincipal(): Principal { | ||
return Principal.fromText(this._principalString); | ||
return this.#innerIdentity.getPrincipal(); | ||
} | ||
@@ -22,15 +23,2 @@ | ||
try { | ||
// console.log(toHexString(blob)); | ||
// const signedResponse = await this._api.sign(toHexString(blob)); | ||
// if (signedResponse.error) { | ||
// throw signedResponse.error; | ||
// } | ||
// if (signedResponse.confirmed === false) { | ||
// throw new Error(`signing message error: signing process terminated by user`); | ||
// } | ||
// return fromHexString(signedResponse.signature) as Signature; | ||
this.#innerIdentity = this.#innerIdentity ?? Secp256k1KeyIdentity.fromJSON(await this._api.getIdentity()); | ||
const sig = await this.#innerIdentity.sign(blob); | ||
@@ -37,0 +25,0 @@ return sig; |
@@ -1,2 +0,2 @@ | ||
export { getIdentity, configure, sign, signRawMessage, getPrincipal } from './methods'; | ||
export { configure, sign, signRawMessage, getPrincipal } from './methods'; | ||
export { MetamaskICPSnap, enableICPSnap } from './snap'; | ||
@@ -3,0 +3,0 @@ import './types'; |
@@ -12,4 +12,16 @@ import { MetamaskICPRpcRequest, SignRawMessageResponse, SignMessageResponse, SnapConfig } from '@astrox/icsnap-types'; | ||
export async function getIdentity(this: MetamaskICPSnap): Promise<string> { | ||
return await sendSnapMethod({ method: 'icp_getIdentity' }, this.snapId); | ||
// export async function getIdentity(this: MetamaskICPSnap): Promise<string> { | ||
// return await sendSnapMethod({ method: 'icp_getIdentity' }, this.snapId); | ||
// } | ||
export async function requestDelegationChain( | ||
this: MetamaskICPSnap, | ||
sessionPublicKey: string, | ||
milliseconds?: number, | ||
canisterIds?: string, | ||
): Promise<string> { | ||
return await sendSnapMethod( | ||
{ method: 'icp_requestDelegationChain', params: { sessionPublicKey: sessionPublicKey, milliseconds: milliseconds, canisterIds: canisterIds } }, | ||
this.snapId, | ||
); | ||
} | ||
@@ -16,0 +28,0 @@ |
import { ICPSnapApi, SnapConfig } from '@astrox/icsnap-types'; | ||
import { hasMetaMask, isMetamaskSnapsSupported, isSnapInstalled } from './util'; | ||
import { configure, getIdentity, getPrincipal, getRawPublicKey, sign, signRawMessage } from './methods'; | ||
import { hasMetaMask, isMetamaskSnapsSupported, isSnapInstalled, toHexString } from './util'; | ||
import { configure, getPrincipal, getRawPublicKey, requestDelegationChain, sign, signRawMessage } from './methods'; | ||
import { SnapIdentity } from './identity'; | ||
import { Secp256k1PublicKey } from '@dfinity/identity'; | ||
@@ -11,9 +12,2 @@ export class MetamaskICPSnap { | ||
public async createSnapIdentity(): Promise<SnapIdentity> { | ||
const api = await this.getICPSnapApi(); | ||
const publicKey = await api.getRawPublicKey(); | ||
const principal = await api.getPrincipal(); | ||
return new SnapIdentity(api, publicKey, principal); | ||
} | ||
public constructor(snapOrigin: string) { | ||
@@ -27,3 +21,3 @@ this.snapOrigin = snapOrigin; | ||
configure: configure.bind(this), | ||
getIdentity: getIdentity.bind(this), | ||
requestDelegationChain: requestDelegationChain.bind(this), | ||
sign: sign.bind(this), | ||
@@ -39,2 +33,7 @@ signRawMessage: signRawMessage.bind(this), | ||
export interface EnableICPSnapResult { | ||
snap: MetamaskICPSnap; | ||
delegationChainString: string; | ||
} | ||
const defaultSnapOrigin = 'https://bafybeigzphbumdkucnj2c6nr5xb3kwsq5gs2gp7w3qldgbvfeycfsbjylu.ipfs.infura-ipfs.io'; | ||
@@ -54,7 +53,13 @@ /** | ||
*/ | ||
export async function enableICPSnap( | ||
config: Partial<SnapConfig>, | ||
snapOrigin?: string, | ||
snapInstallationParams: Record<SnapInstallationParamNames, unknown> = {}, | ||
): Promise<MetamaskICPSnap> { | ||
export async function enableICPSnap({ | ||
config, | ||
snapOrigin, | ||
snapInstallationParams = {}, | ||
sessionPublicKey, | ||
}: { | ||
config: Partial<SnapConfig>; | ||
snapOrigin?: string; | ||
snapInstallationParams: Record<SnapInstallationParamNames, unknown>; | ||
sessionPublicKey: Secp256k1PublicKey; | ||
}): Promise<EnableICPSnapResult> { | ||
const snapId = snapOrigin ?? defaultSnapOrigin; | ||
@@ -94,5 +99,6 @@ | ||
// set initial configuration | ||
await (await snap.getICPSnapApi()).configure(config); | ||
// return snap object | ||
return snap; | ||
const api = await snap.getICPSnapApi(); | ||
await api.configure(config); | ||
const delegationChainString = await api.requestDelegationChain(toHexString(sessionPublicKey.toDer())); | ||
return { snap, delegationChainString }; | ||
} |
@@ -37,3 +37,2 @@ import './types'; | ||
export async function isSnapInstalled(snapOrigin: string, version?: string): Promise<boolean> { | ||
console.log(await getWalletSnaps()); | ||
try { | ||
@@ -40,0 +39,0 @@ return !!Object.values(await getWalletSnaps()).find(permission => permission.id === snapOrigin && (!version || permission.version === version)); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
68681
-0.04%581
-0.34%+ Added
- Removed
Updated