Skip to content

Commit

Permalink
add: saveSigningCondition();
Browse files Browse the repository at this point in the history
  • Loading branch information
Anson authored and Anson committed Oct 17, 2022
1 parent 72f122c commit c262f04
Show file tree
Hide file tree
Showing 3 changed files with 323 additions and 34 deletions.
85 changes: 67 additions & 18 deletions packages/constants/src/lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ export const ERight = (result: any) : IEither => {
}

/** ---------- Access Control Conditions Interfaces ---------- */
/**
* TODO: We should probably create a schema for these different types of params
*/

export type AccessControlConditions = Array<AccsRegularParams | AccsDefaultParams>;
export type EvmContractConditions = Array<AccsEVMParams>;
export type SolRpcConditions = Array<AccsSOLV2Params>;
export type UnifiedAccessControlConditions = Array<AccsRegularParams | AccsDefaultParams | AccsSOLV2Params | AccsEVMParams | AccsCOSMOSParams>;

export interface AccsOperatorParams {
operator: string
}
Expand Down Expand Up @@ -141,6 +144,7 @@ export interface JsonAuthSig{
algo?: [],
}


export interface CheckAndSignAuthParams {

// The chain you want to use. Find the supported list of chains here: https://developer.litprotocol.com/docs/supportedChains
Expand Down Expand Up @@ -178,16 +182,16 @@ export interface EncryptFileAndZipWithMetadataProps{
authSig: JsonAuthSig,

// The access control conditions that the user must meet to obtain this signed token. This could be posession of an NFT, for example. You must pass either accessControlConditions or evmContractConditions or solRpcConditions or unifiedAccessControlConditions.
accessControlConditions: Array<AccsRegularParams | AccsDefaultParams>,
accessControlConditions: AccessControlConditions,

// EVM Smart Contract access control conditions that the user must meet to obtain this signed token. This could be posession of an NFT, for example. This is different than accessControlConditions because accessControlConditions only supports a limited number of contract calls. evmContractConditions supports any contract call. You must pass either accessControlConditions or evmContractConditions or solRpcConditions or unifiedAccessControlConditions.
evmContractConditions: Array<AccsEVMParams>,

// Solana RPC call conditions that the user must meet to obtain this signed token. This could be posession of an NFT, for example.
solRpcConditions: Array<AccsSOLV2Params>,
solRpcConditions: SolRpcConditions,

// An array of unified access control conditions. You may use AccessControlCondition, EVMContractCondition, or SolRpcCondition objects in this array, but make sure you add a conditionType for each one. You must pass either accessControlConditions or evmContractConditions or solRpcConditions or unifiedAccessControlConditions.
unifiedAccessControlConditions: Array<AccsRegularParams | AccsDefaultParams | AccsSOLV2Params | AccsEVMParams | AccsCOSMOSParams>,
unifiedAccessControlConditions: UnifiedAccessControlConditions;

// The chain name of the chain that this contract is deployed on. See LIT_CHAINS for currently supported chains.
chain: string,
Expand Down Expand Up @@ -261,16 +265,16 @@ export interface IJWT{
export interface HumanizedAccsProps{

// The array of access control conditions that you want to humanize
accessControlConditions: Array<AccsRegularParams | AccsDefaultParams>;
accessControlConditions: AccessControlConditions;

// The array of evm contract conditions that you want to humanize
evmContractConditions: Array<AccsEVMParams>,
evmContractConditions: EvmContractConditions,

// The array of Solana RPC conditions that you want to humanize
solRpcConditions: Array<AccsSOLV2Params>,
solRpcConditions: SolRpcConditions,

// The array of unified access control conditions that you want to humanize
unifiedAccessControlConditions: Array< AccsRegularParams | AccsDefaultParams | AccsSOLV2Params | AccsEVMParams | AccsCOSMOSParams>;
unifiedAccessControlConditions: UnifiedAccessControlConditions;
tokenList: Array<any | string>,
myWalletAddress: string,
}
Expand Down Expand Up @@ -351,6 +355,30 @@ export interface JsonSigningResourceId{
extraData: string,
}

export interface JsonAccsRequest{

// The access control conditions that the user must meet to obtain this signed token. This could be posession of an NFT, for example. You must pass either accessControlConditions or evmContractConditions or solRpcConditions or unifiedAccessControlConditions.
accessControlConditions?: AccessControlConditions,

// EVM Smart Contract access control conditions that the user must meet to obtain this signed token. This could be posession of an NFT, for example. This is different than accessControlConditions because accessControlConditions only supports a limited number of contract calls. evmContractConditions supports any contract call. You must pass either accessControlConditions or evmContractConditions or solRpcConditions or unifiedAccessControlConditions.
evmContractConditions?: EvmContractConditions,

// Solana RPC call conditions that the user must meet to obtain this signed token. This could be posession of an NFT, for example.
solRpcConditions?: SolRpcConditions,

// An array of unified access control conditions. You may use AccessControlCondition, EVMContractCondition, or SolRpcCondition objects in this array, but make sure you add a conditionType for each one. You must pass either accessControlConditions or evmContractConditions or solRpcConditions or unifiedAccessControlConditions.
unifiedAccessControlConditions?: UnifiedAccessControlConditions,

// The chain name of the chain that you are querying. See ALL_LIT_CHAINS for currently supported chains.
chain?: string,

// The resourceId representing something on the web via a URL
resourceId: JsonSigningResourceId,

// The authentication signature that proves that the user owns the crypto wallet address that meets the access control conditions
authSig: JsonAuthSig,
}

/**
* Struct in rust
* -----
Expand All @@ -366,18 +394,39 @@ pub struct JsonSigningRetrieveRequest {
pub exp: u64,
}
*/
export interface JsonSigningRetrieveRequest{
accessControlConditions?: Array<AccsRegularParams | AccsDefaultParams>,
evmContractConditions?: Array<AccsEVMParams>,
solRpcConditions?: Array<AccsSOLV2Params>,
unifiedAccessControlConditions?: Array<AccsRegularParams | AccsDefaultParams | AccsSOLV2Params | AccsEVMParams | AccsCOSMOSParams>,
chain?: string,
resourceId: JsonSigningResourceId,
authSig: JsonAuthSig,
export interface JsonSigningRetrieveRequest extends JsonAccsRequest{
iat: number,
exp: number,
}


export interface JsonStoreSigningRequest extends JsonAccsRequest{

// Whether or not the access control condition should be saved permanently. If false, the access control conditions will be updateable by the creator. If you don't pass this param, it's set to true by default.
permanant : boolean,
permanent : boolean,
}

/**
* Struct in rust
* -----
pub struct JsonSigningStoreRequest {
pub key: String,
pub val: String,
pub chain: Option<String>,
pub permanant: Option<usize>,
pub auth_sig: AuthSigItem,
}
*/
export interface JsonSigningStoreRequest{
key: string,
val: string,
chain?: string,
permanant?: number,
permanent?: number,
authSig: JsonAuthSig,
}

export interface ExecuteJsProps extends JsonExecutionRequest{

// A boolean that defines if debug info will be returned or not.
Expand Down
105 changes: 100 additions & 5 deletions packages/utils/src/lib/browser/Crypto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ABIParams, AccsCOSMOSParams, AccsDefaultParams, AccsEVMParams, AccsOperatorParams, AccsRegularParams, AccsSOLV2Params, ILitError, JsonSigningResourceId, LIT_ERROR, SigShare, SigShares, SYMM_KEY_ALGO_PARAMS } from "@litprotocol-dev/constants";
import { ABIParams, AccessControlConditions, AccsCOSMOSParams, AccsDefaultParams, AccsEVMParams, AccsOperatorParams, AccsRegularParams, AccsSOLV2Params, EvmContractConditions, ILitError, JsonSigningResourceId, LIT_ERROR, SigShare, SigShares, SolRpcConditions, SYMM_KEY_ALGO_PARAMS, UnifiedAccessControlConditions } from "@litprotocol-dev/constants";
import { wasmBlsSdkHelpers } from "@litprotocol-dev/core";
import * as wasmECDSA from "@litprotocol-dev/core";
import { log, throwError } from "../utils";
Expand Down Expand Up @@ -49,6 +49,101 @@ export const hashUnifiedAccessControlConditions = (

}

/**
*
* Hash resource id
*
* @param { JsonSigningResourceId } resourceId
*
* @returns { Promise<ArrayBuffer> }
*
*/
export const hashResourceId = (
resourceId: JsonSigningResourceId
) : Promise<ArrayBuffer> => {

const resId = canonicalResourceIdFormatter(resourceId);
const toHash = JSON.stringify(resId);
const encoder = new TextEncoder();
const data = encoder.encode(toHash);

return crypto.subtle.digest("SHA-256", data);
}

/**
*
* Hash access control conditions
*
* @param { AccessControlConditions } accessControlConditions
*
* @returns { Promise<ArrayBuffer> }
*
*/
export const hashAccessControlConditions = (
accessControlConditions: AccessControlConditions
) : Promise<ArrayBuffer> => {

const conds = accessControlConditions.map((c) =>
canonicalAccessControlConditionFormatter(c)
);

const toHash = JSON.stringify(conds);
log("Hashing access control conditions: ", toHash);
const encoder = new TextEncoder();
const data = encoder.encode(toHash);

return crypto.subtle.digest("SHA-256", data);
}

/**
*
* Hash EVM access control conditions
*
* @param { EvmContractConditions } evmContractConditions
*
* @returns { Promise<ArrayBuffer> }
*
*/
export const hashEVMContractConditions = (
evmContractConditions: EvmContractConditions
) : Promise<ArrayBuffer> => {

const conds = evmContractConditions.map((c) =>
canonicalEVMContractConditionFormatter(c)
);

const toHash = JSON.stringify(conds);
log("Hashing evm contract conditions: ", toHash);
const encoder = new TextEncoder();
const data = encoder.encode(toHash);
return crypto.subtle.digest("SHA-256", data);
}

/**
*
* Hash SOL access control conditions
*
* @param { SolRpcConditions } solRpcConditions
*
* @returns { Promise<ArrayBuffer> }
*
*/
export const hashSolRpcConditions = (
solRpcConditions: SolRpcConditions,
) : Promise<ArrayBuffer> => {

const conds = solRpcConditions.map((c) =>
canonicalSolRpcConditionFormatter(c)
);

const toHash = JSON.stringify(conds);
log("Hashing sol rpc conditions: ", toHash);
const encoder = new TextEncoder();
const data = encoder.encode(toHash);

return crypto.subtle.digest("SHA-256", data);
}

/**
*
* Get operator param
Expand Down Expand Up @@ -582,13 +677,13 @@ export const generateSymmetricKey = async () : Promise<CryptoKey> => {
* @param { SigShares | Array<SigShare> } sigSharesWithEverything
* @param { string } networkPubKeySet
*
* @returns { BLSSharesCombined }
* @returns { any }
*
*/
export const combineBlsShares = (
sigSharesWithEverything: SigShares,
networkPubKeySet: string
) : BLSSharesCombined => {
) : any => {

const pkSetAsBytes = uint8arrayFromString(networkPubKeySet, "base16");

Expand Down Expand Up @@ -617,12 +712,12 @@ export const combineBlsShares = (
*
* @param { SigShares | Array<SigShare> } sigShares
*
* @returns { ECDSASharesCombined }
* @returns { any }
*
*/
export const combineEcdsaShares = (
sigShares: SigShares
) : ECDSASharesCombined => {
) : any => {

// R_x & R_y values can come from any node (they will be different per node), and will generate a valid signature
const R_x = sigShares[0].localX;
Expand Down

0 comments on commit c262f04

Please sign in to comment.