Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 5e083bb
Author: Josh Long <josh@litprotocol.com>
Date:   Tue Aug 22 14:32:35 2023 -0400

    migrate hd pubkey method from lit-node-client to lit-core

commit 5502d4c
Author: Josh Long <josh@litprotocol.com>
Date:   Tue Aug 22 12:25:57 2023 -0400

    fix types

commit 310db0f
Author: Josh Long <josh@litprotocol.com>
Date:   Sun Aug 20 11:43:56 2023 -0400

    fix build issues from merge

commit cc5076a
Author: Josh Long <josh@litprotocol.com>
Date:   Sun Aug 20 11:43:12 2023 -0400

    add telem endpoint

commit bba8c01
Author: Josh Long <josh@litprotocol.com>
Date:   Thu Aug 17 17:51:32 2023 -0400

    implement relay fetch call

commit a8641c2
Author: Josh Long <josh@litprotocol.com>
Date:   Wed Aug 16 17:36:29 2023 -0400

    merge of pr #194 fixes

commit fc00d58
Author: sarah z <15064710+sarahzdev@users.noreply.github.com>
Date:   Tue Aug 15 10:05:54 2023 -0700

    Merge pull request #194 from LIT-Protocol/feat/update-mint-relay

    feat: support new mint and fetch endpoints

commit b4a9f09
Author: Josh Long <josh@litprotocol.com>
Date:   Wed Aug 16 17:22:25 2023 -0400

    Revert "chore: update pkgs in lit auth example"

    This reverts commit 83263dd.

commit 83263dd
Author: sarahzdev <sarah.hg.zhang@gmail.com>
Date:   Wed Aug 16 13:01:58 2023 -0700

    chore: update pkgs in lit auth example

commit 9b06618
Author: Josh Long <josh@litprotocol.com>
Date:   Wed Aug 16 16:34:25 2023 -0400

    remove key id from signing request

commit c896dda
Author: Josh Long <josh@litprotocol.com>
Date:   Tue Aug 15 09:59:42 2023 -0400

    update claim types  per node updates

commit be3d1c1
Author: Josh Long <josh@litprotocol.com>
Date:   Fri Aug 11 16:04:19 2023 -0400

    add mint claim callback

commit 38af96f
Author: Josh Long <josh@litprotocol.com>
Date:   Wed Aug 9 12:45:24 2023 -0400

    add rootkeys to handshake

commit e038f30
Author: Josh Long <josh@litprotocol.com>
Date:   Wed Aug 9 10:43:23 2023 -0400

    update claim impls

commit 67bb831
Author: Josh Long <josh@litprotocol.com>
Date:   Tue Aug 8 12:25:31 2023 -0400

    add hd key support

commit e3edec2
Author: Josh Long <josh@litprotocol.com>
Date:   Fri Aug 4 19:39:28 2023 -0400

    updates for new enums on nodes

commit ca1785f
Author: Josh Long <josh@litprotocol.com>
Date:   Mon Jul 31 08:47:12 2023 -0400

    add auth callback statement

commit 1ba9c92
Author: Josh Long <josh@litprotocol.com>
Date:   Sat Jul 29 10:31:21 2023 -0400

    update claim and signing tests

commit 4d11c65
Author: Josh Long <josh@litprotocol.com>
Date:   Fri Jul 28 18:00:44 2023 -0400

    add support for public key derivation

commit 29e4ad8
Author: Josh Long <josh@litprotocol.com>
Date:   Fri Jul 28 18:00:24 2023 -0400

    upgrade crypto and ecdsa lib for hd key signature and public key derivation

commit 1db195b
Author: Josh Long <josh@litprotocol.com>
Date:   Thu Jul 27 18:04:27 2023 -0400

    update test

commit ab340bb
Author: Josh Long <josh@litprotocol.com>
Date:   Thu Jul 27 17:55:16 2023 -0400

    upgrade signature combination for cait sith changes
  • Loading branch information
Ansonhkg committed Aug 24, 2023
1 parent 75fff82 commit 77ce208
Show file tree
Hide file tree
Showing 18 changed files with 1,977 additions and 1,477 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@
"blockstore-core": "^3.0.0",
"bs58": "^5.0.0",
"bytes32": "^0.0.3",
"cbor-web": "^9.0.0",
"chalk": "^5.3.0",
"cbor-web": "^9.0.1",
"commander": "^9.4.0",
"concurrently": "^7.4.0",
"core-js": "^3.6.5",
Expand Down
1 change: 1 addition & 0 deletions packages/constants/src/lib/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,3 +675,4 @@ export const AUTH_METHOD_TYPE_IDS = {

// ========== PKP Client ==========
export const PKP_CLIENT_SUPPORTED_CHAINS = ['eth', 'cosmos'];
export const TELEM_API_URL = 'https://lit-general-worker.getlit.dev';
3 changes: 2 additions & 1 deletion packages/constants/src/lib/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export enum VMTYPE {

export enum SIGTYPE {
BLS = 'BLS',
EcdsaCAITSITHK256 = 'EcdsaCaitSithK256',
EcdsaCaitSith = 'EcdsaCaitSith',
EcdsaCAITSITHP256 = 'EcdsaCaitSithP256'
}

Expand Down Expand Up @@ -45,4 +45,5 @@ export enum ProviderType {
Otp = 'otp',
StytchOtp = 'stytchOtp',
Apple = 'apple',
StytchOtp = 'stytchOtp'
}
44 changes: 42 additions & 2 deletions packages/core/src/lib/lit-core.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { computeHDPubKey } from '@lit-protocol/crypto';
import {
canonicalAccessControlConditionFormatter,
canonicalEVMContractConditionFormatter,
Expand All @@ -15,6 +16,8 @@ import {
LIT_NETWORKS,
defaultLitnodeClientConfig,
version,
TELEM_API_URL,
SIGTYPE,
} from '@lit-protocol/constants';

import {
Expand Down Expand Up @@ -52,6 +55,7 @@ export class LitCore {
subnetPubKey: string | null;
networkPubKey: string | null;
networkPubKeySet: string | null;
hdRootPubkeys: string[] | null;

// ========== Constructor ==========
constructor(args: any[LitNodeClientConfig | CustomNetwork | any]) {
Expand All @@ -76,7 +80,7 @@ export class LitCore {
this.subnetPubKey = null;
this.networkPubKey = null;
this.networkPubKeySet = null;

this.hdRootPubkeys = null;
// -- set bootstrapUrls to match the network litNetwork unless it's set to custom
this.setCustomBootstrapUrls();

Expand Down Expand Up @@ -134,8 +138,8 @@ export class LitCore {
subnetPubKey: resp.subnetPublicKey,
networkPubKey: resp.networkPublicKey,
networkPubKeySet: resp.networkPublicKeySet,
hdRootPubkeys: resp.hdRootPubkeys,
};

this.serverKeys[url] = keys;
})
.catch((e: any) => {
Expand Down Expand Up @@ -166,6 +170,11 @@ export class LitCore {
(keysFromSingleNode: any) => keysFromSingleNode.networkPubKeySet
)
);
this.hdRootPubkeys = mostCommonString(
Object.values(this.serverKeys).map(
(keysFromSingleNode: any) => keysFromSingleNode.hdRootPubkeys
)
);
this.ready = true;

log(
Expand Down Expand Up @@ -558,4 +567,35 @@ export class LitCore {
formattedUnifiedAccessControlConditions,
};
};

/**
* Calculates an HD public key from a given {@link keyId} the curve type or signature type will assumed to be k256 unless given
* @param keyId
* @param sigType
* @returns {string} public key
*/
computePubKey = (keyId: string, sigType: SIGTYPE = SIGTYPE.EcdsaCaitSith) => {
if(!this.hdRootPubkeys) {
throwError({
message: `root public keys not found, have you connected to the nodes?`,
errorKind: LIT_ERROR.LIT_NODE_CLIENT_NOT_READY_ERROR.kind,
errorCode: LIT_ERROR.LIT_NODE_CLIENT_NOT_READY_ERROR.code,
});
}
return computeHDPubKey(this.hdRootPubkeys as string[], keyId, sigType);
}

collectData = (
date: string,
functionName: string,
executionTime: number
) => {
fetch(TELEM_API_URL + '/collect', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ date, functionName, executionTime }),
});
}
}
58 changes: 23 additions & 35 deletions packages/crypto/src/lib/crypto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,47 +166,34 @@ describe('combine ECDSA Shares', () => {
});

it('Should recombine ECDSA signature shares', async () => {
/*
JSON.parse(
'{ "sig_type": "EcdsaCaitSith", "data_signed": "A591A6D40BF420404A011733CFB7B190D62C65BF0BCDA32B57B277D9AD9F146E", "signature_share": "01C4E0EDD498B14DFE8D87163C39F738B8AC17172B55A6A9518E3704362B4FC1", "share_index": 0, "local_x": "0203899AD2B6B56D65130DDAE01A099D867FDC5DF9219CD1B31A48B03FA0AF05EA", "local_y": "0203899AD2B6B56D65130DDAE01A099D867FDC5DF9219CD1B31A48B03FA0AF05EA", "public_key": "03D589E6B6FF8B611D073F6161E8A8D9A9A737C6B102D284984ACE0F0326073402", "sig_name": "sig1" }'
),
JSON.parse(
'{ "sig_type": "EcdsaCaitSith", "data_signed": "A591A6D40BF420404A011733CFB7B190D62C65BF0BCDA32B57B277D9AD9F146E", "signature_share": "FE3B1F122B674EB2017278E9C3C608C60202C5CF83F2F9926E4427889A0AF180", "share_index": 0, "local_x": "0203899AD2B6B56D65130DDAE01A099D867FDC5DF9219CD1B31A48B03FA0AF05EA", "local_y": "0203899AD2B6B56D65130DDAE01A099D867FDC5DF9219CD1B31A48B03FA0AF05EA", "public_key": "03D589E6B6FF8B611D073F6161E8A8D9A9A737C6B102D284984ACE0F0326073402", "sig_name": "sig1" }'
),
JSON.parse(
`{ \"sig_type\": \"EcdsaCaitSith\", \"data_signed\": \"A591A6D40BF420404A011733CFB7B190D62C65BF0BCDA32B57B277D9AD9F146E\", \"signature_share\": \"55EC4AF9F1883B19FF84825CBEBDFD127BC8FBFF48DF6CF705CADC85ACCB3056\", \"share_index\": 0, \"local_x\": \"0203899AD2B6B56D65130DDAE01A099D867FDC5DF9219CD1B31A48B03FA0AF05EA\", \"local_y\": \"0203899AD2B6B56D65130DDAE01A099D867FDC5DF9219CD1B31A48B03FA0AF05EA\", \"public_key\": \"03D589E6B6FF8B611D073F6161E8A8D9A9A737C6B102D284984ACE0F0326073402\", \"sig_name\": \"sig1\" }`
),
*/

const sigShares = [
{
sigType:"EcdsaCaitSithK256",
dataSigned: "A591A6D40BF420404A011733CFB7B190D62C65BF0BCDA32B57B277D9AD9F146E",
signatureShare: "01C4E0EDD498B14DFE8D87163C39F738B8AC17172B55A6A9518E3704362B4FC1",
shareIndex: 0,
localX: "0203899AD2B6B56D65130DDAE01A099D867FDC5DF9219CD1B31A48B03FA0AF05EA",
localY: "0203899AD2B6B56D65130DDAE01A099D867FDC5DF9219CD1B31A48B03FA0AF05EA",
publicKey: "03D589E6B6FF8B611D073F6161E8A8D9A9A737C6B102D284984ACE0F0326073402",
sigName: "sig1"
"sigType": "EcdsaCaitSith",
"signatureShare": "BC8108AD9CAE8358942BB4B27632B87FFA705CCB675F85A59847CC1B84845A38",
"shareIndex": 0,
"bigR": "03E6D15C805443F57F57E180C730C2FCA5297F7671E8148A669410808AB4D70122",
"publicKey": "03AECABDF2EDC1194BED6FE9650F08D109C77D2526236EA3F6C20F88E0675643BC",
"dataSigned": "90AB86E6389AA65B56D701E36EEECD786242405C792ED863C395FA7C55E517A4",
"sigName": "sig"
},
{
sigType:"EcdsaCaitSithK256",
dataSigned: "A591A6D40BF420404A011733CFB7B190D62C65BF0BCDA32B57B277D9AD9F146E",
signatureShare: "FE3B1F122B674EB2017278E9C3C608C60202C5CF83F2F9926E4427889A0AF180",
shareIndex: 0,
localX: "0203899AD2B6B56D65130DDAE01A099D867FDC5DF9219CD1B31A48B03FA0AF05EA",
localY: "0203899AD2B6B56D65130DDAE01A099D867FDC5DF9219CD1B31A48B03FA0AF05EA",
publicKey: "03D589E6B6FF8B611D073F6161E8A8D9A9A737C6B102D284984ACE0F0326073402",
sigName: "sig1",
"sigType": "EcdsaCaitSith",
"signatureShare": "BA77EB500884A60583DEA49578D4BB64BB55EF497F37C88DF935D739CE8E0A9F",
"shareIndex": 0,
"bigR": "03E6D15C805443F57F57E180C730C2FCA5297F7671E8148A669410808AB4D70122",
"publicKey": "03AECABDF2EDC1194BED6FE9650F08D109C77D2526236EA3F6C20F88E0675643BC",
"dataSigned": "90AB86E6389AA65B56D701E36EEECD786242405C792ED863C395FA7C55E517A4",
"sigName": "sig"
},
{
sigType:"EcdsaCaitSithK256",
dataSigned: "A591A6D40BF420404A011733CFB7B190D62C65BF0BCDA32B57B277D9AD9F146E",
signatureShare: "55EC4AF9F1883B19FF84825CBEBDFD127BC8FBFF48DF6CF705CADC85ACCB3056",
shareIndex: 0,
localX: "0203899AD2B6B56D65130DDAE01A099D867FDC5DF9219CD1B31A48B03FA0AF05EA",
localY: "0203899AD2B6B56D65130DDAE01A099D867FDC5DF9219CD1B31A48B03FA0AF05EA",
publicKey: "03D589E6B6FF8B611D073F6161E8A8D9A9A737C6B102D284984ACE0F0326073402",
sigName: "sig1",
"sigType": "EcdsaCaitSith",
"signatureShare": "EF850AE61B6D658976B2560B880BF03ABC1A070BACDEAE2311781F65A524F245",
"shareIndex": 0,
"bigR": "03E6D15C805443F57F57E180C730C2FCA5297F7671E8148A669410808AB4D70122",
"publicKey": "03AECABDF2EDC1194BED6FE9650F08D109C77D2526236EA3F6C20F88E0675643BC",
"dataSigned": "90AB86E6389AA65B56D701E36EEECD786242405C792ED863C395FA7C55E517A4",
"sigName": "sig"
}
];

Expand All @@ -220,6 +207,7 @@ describe('combine ECDSA Shares', () => {
s: '0x' + sig.s,
v: sig.recid
});

let msg: any = ethers.utils.arrayify('0x' + sigShares[0].dataSigned)
const recoveredPk = ethers.utils.recoverPublicKey(msg, sigRes);

Expand Down
68 changes: 48 additions & 20 deletions packages/crypto/src/lib/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ if (!globalThis.wasmECDSA) {
let init = wasmECDSA.initWasmEcdsaSdk;
let env;

if (isBrowser()) {
if (isBrowser()) {
env = 'Browser';
} else {
env = 'NodeJS';
Expand Down Expand Up @@ -176,7 +176,9 @@ export const verifySignature = (
* @returns { any }
*
*/
export const combineEcdsaShares = (sigShares: Array<SigShare>): CombinedECDSASignature => {
export const combineEcdsaShares = (
sigShares: Array<SigShare>
): CombinedECDSASignature => {
log('sigShares:', sigShares);
let type = sigShares[0].sigType;
// the public key can come from any node - it obviously will be identical from each node
Expand Down Expand Up @@ -206,28 +208,34 @@ export const combineEcdsaShares = (sigShares: Array<SigShare>): CombinedECDSASig

try {
let res: string = '';
switch(type) {
case SIGTYPE.EcdsaCAITSITHK256:
res = wasmECDSA.combine_signature(validShares, 3);
switch (type) {
case SIGTYPE.EcdsaCaitSith:
res = wasmECDSA.combine_signature(validShares, 2);
sig = JSON.parse(res) as CombinedECDSASignature;
/*
r and s values of the signature should be maximum of 64 bytes
r and s values can have polarity as the first two bits, here we remove
*/
if (sig.r && sig.r.length > 64) {
while(sig.r.length > 64) { sig.r = sig.r.slice(1); }
while (sig.r.length > 64) {
sig.r = sig.r.slice(1);
}
}
if (sig.s && sig.s.length > 64) {
while(sig.s.length > 64) { sig.s = sig.s.slice(1); }
while (sig.s.length > 64) {
sig.s = sig.s.slice(1);
}
}
break;
break;
case SIGTYPE.ECDSCAITSITHP256:
res = wasmECDSA.combine_signature(validShares, 4);
res = wasmECDSA.combine_signature(validShares, 3);
sig = JSON.parse(res);
break;
break;
// if its another sig type, it shouldnt be resolving to this method
default:
throw new Error("Unsupported signature type present in signature shares. Please report this issue");
throw new Error(
'Unsupported signature type present in signature shares. Please report this issue'
);
}
} catch (e) {
log('Failed to combine signatures:', e);
Expand All @@ -238,6 +246,20 @@ export const combineEcdsaShares = (sigShares: Array<SigShare>): CombinedECDSASig
return sig;
};

export const computeHDPubKey = (pubkeys: string[], keyId: string, sigType: SIGTYPE): string => {
// TODO: hardcoded for now, need to be replaced on each DKG as the last dkg id will be the active root key set.

try {
switch (sigType) {
case SIGTYPE.EcdsaCaitSith:
return wasmECDSA.compute_public_key(keyId, pubkeys, 2);
defualt: throw new Error('Non supported signature type');
}
} catch (e) {
log('Failed to derive public key', e);
}
};

/**
*
* Generate a session key pair
Expand All @@ -255,14 +277,20 @@ export const generateSessionKeyPair = (): SessionKeyPair => {
return sessionKeyPair;
};


const _remapKeyShareForEcdsa = (share: SigShare): any[] => {
const keys = Object.keys(share);
let newShare = {};
for (const key of keys) {
const new_key = key.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
newShare = Object.defineProperty(newShare, new_key, Object.getOwnPropertyDescriptor(share, key));
}
const keys = Object.keys(share);
let newShare = {};
for (const key of keys) {
const new_key = key.replace(
/[A-Z]/g,
(letter) => `_${letter.toLowerCase()}`
);
newShare = Object.defineProperty(
newShare,
new_key,
Object.getOwnPropertyDescriptor(share, key)
);
}

return newShare;
}
return newShare;
};
Loading

0 comments on commit 77ce208

Please sign in to comment.