bsv-bap 0.1.20 → 0.1.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,9 @@
1
- import { HD, type PrivateKey, PublicKey } from "@bsv/sdk";
1
+ import { PublicKey, type PrivateKey, HD } from "@bsv/sdk";
2
2
  import { type APIFetcher } from "./api";
3
3
  import type { GetAttestationResponse, GetSigningKeysResponse } from "./apiTypes";
4
- import { BaseClass } from "./BaseClass";
5
- import type { Identity, IdentityAttributes, MemberIdentity, OldIdentity } from "./interface";
4
+ import type { Identity, IdentityAttributes, OldIdentity, MemberIdentity } from "./interface";
6
5
  import { MemberID } from "./MemberID";
6
+ import { BaseClass } from "./BaseClass";
7
7
  interface Type42KeySource {
8
8
  rootPk: PrivateKey;
9
9
  }
@@ -103,11 +103,41 @@ declare class MasterID extends BaseClass {
103
103
  */
104
104
  getIdTransaction(previousPath?: string): number[][];
105
105
  /**
106
- * Get the private key for a given path
106
+ * Get the private key for a given path (before identity signing key derivation)
107
+ * This is the "member key" for the path
107
108
  */
108
109
  private getPathDerivedKey;
110
+ /**
111
+ * Get the identity signing key for a given path
112
+ * This is derived from the path key using the BAP protocol pattern
113
+ */
114
+ private getIdentitySigningKeyForPath;
115
+ /**
116
+ * Get the member key's public key for the given path
117
+ * This is the root key before signing key derivation
118
+ */
119
+ getMemberKey(path?: string): string;
120
+ /**
121
+ * Get the legacy (pre-signing-key-derivation) address for a path
122
+ * This is the address without the extra "1-bap-identity" derivation
123
+ */
124
+ getLegacyAddress(path?: string): string;
125
+ /**
126
+ * Check if the on-chain signing address uses legacy derivation
127
+ * @param registeredAddress The address registered on-chain (optional, defaults to rootAddress)
128
+ * @returns true if the registered address matches legacy derivation
129
+ */
130
+ needsRotation(registeredAddress?: string): boolean;
131
+ /**
132
+ * Get OP_RETURN for migrating from legacy to new signing address derivation
133
+ * Signs with the LEGACY key to prove ownership of the old address
134
+ * Caller handles funding and broadcast
135
+ * @returns OP_RETURN data as number[][]
136
+ */
137
+ getLegacyRotationTransaction(): number[][];
109
138
  /**
110
139
  * Get address for given path
140
+ * Returns the identity signing address (derived from member key)
111
141
  *
112
142
  * @param path
113
143
  * @returns {*}
@@ -186,6 +216,7 @@ declare class MasterID extends BaseClass {
186
216
  getAttestationHash(attribute: string): string | null;
187
217
  /**
188
218
  * Sign a message with the current signing address of this identity
219
+ * Uses the derived identity signing key
189
220
  *
190
221
  * @param message
191
222
  * @param signingPath
@@ -1,4 +1,4 @@
1
- import { PrivateKey, PublicKey } from "@bsv/sdk";
1
+ import { PublicKey, PrivateKey } from "@bsv/sdk";
2
2
  import { BaseClass } from "./BaseClass";
3
3
  import type { IdentityAttributes, MemberIdentity } from "./interface";
4
4
  export declare class MemberID extends BaseClass {
@@ -8,6 +8,23 @@ export declare class MemberID extends BaseClass {
8
8
  address: string;
9
9
  identityKey: string;
10
10
  constructor(key: PrivateKey, identityAttributes?: IdentityAttributes);
11
+ /**
12
+ * Get the derived identity signing key
13
+ * This is derived from the member key using the BAP protocol pattern
14
+ * invoiceNumber = "1-bap-identity" (securityLevel-protocolName-keyID)
15
+ */
16
+ private getIdentitySigningKey;
17
+ /**
18
+ * Get the member key's public key
19
+ * This is the root key for this member before signing key derivation
20
+ * @returns The member's public key hex string
21
+ */
22
+ getMemberKey(): string;
23
+ /**
24
+ * Get the legacy (pre-signing-key-derivation) address
25
+ * This is the address without the extra "1-bap-identity" derivation
26
+ */
27
+ getLegacyAddress(): string;
11
28
  signMessage(message: number[], _signingPath?: string): {
12
29
  address: string;
13
30
  signature: string;
@@ -6,5 +6,8 @@ export declare const BAP_SERVER = "https://api.sigmaidentity.com/v1";
6
6
  export declare const MAX_INT: number;
7
7
  export declare const SIGNING_PATH_PREFIX = "m/424150'/0'/0'";
8
8
  export declare const ENCRYPTION_PATH = "m/424150'/2147483647'/2147483647'";
9
+ export declare const BAP_PROTOCOL_ID: [1, string];
10
+ export declare const BAP_KEY_ID = "identity";
11
+ export declare const BAP_INVOICE_NUMBER = "1-bap-identity";
9
12
  export declare const FRIEND_SECURITY_LEVEL = 2;
10
13
  export declare const FRIEND_PROTOCOL = "friend";
package/dist/index.d.ts CHANGED
@@ -300,5 +300,6 @@ export declare class BAP {
300
300
  };
301
301
  }
302
302
  export { MasterID, MemberID };
303
+ export { bapIdFromAddress, bapIdFromPubkey } from "./utils";
303
304
  export type { Attestation, Identity, MemberIdentity, IdentityAttributes, PathPrefix, Type42Params, };
304
305
  export * from "./apiTypes";
@@ -1,8 +1,8 @@
1
1
  // @bun
2
- import{BSM as g,BigNumber as Mj,ECIES as kj,HD as Vj,OP as v,Signature as Oj,PrivateKey as _j}from"@bsv/sdk";import{Utils as xj}from"@bsv/sdk";var s=async(j,J,$,q)=>{let z=`${$}${j}`;return(await fetch(z,{method:"post",headers:{"Content-type":"application/json; charset=utf-8",token:q,format:"json"},body:JSON.stringify(J)})).json()},B=(j,J)=>async($,q)=>{return s($,q,j,J)};import{Utils as i}from"@bsv/sdk";var{toHex:K,toArray:m}=i,C="1BAPSuaPnfGnSBM3GLV9yhxUdYe4vGbdMT",c=K(m(C)),x="15PciHG22SNLQJXMoSUaWVi7WSqc7hCfva",Nj=K(m(x)),E="https://api.sigmaidentity.com/v1",F=2147483647,V="m/424150'/0'/0'",L=`m/424150'/${F}'/${F}'`,b=2,h="friend";import{Utils as Fj,ECIES as Cj,Hash as U,HD as Uj,PublicKey as N}from"@bsv/sdk";import{ECIES as r,Utils as a,OP as p,PublicKey as P,BSM as H,BigNumber as e}from"@bsv/sdk";var G={getRandomBytes(j=32){if(typeof globalThis<"u"&&globalThis.crypto&&globalThis.crypto.getRandomValues){let J=new Uint8Array(j);return globalThis.crypto.getRandomValues(J),J}throw Error("Secure random number generation not available. crypto.getRandomValues() is required for cryptographic operations. This environment may not be suitable for secure key generation.")},getRandomString(j=32){let J=this.getRandomBytes(j);return Array.from(J,($)=>$.toString(16).padStart(2,"0")).join("")},getSigningPathFromHex(j,J=!0){let $="m",q=j.match(/.{1,8}/g);if(!q)throw Error("Invalid hex string");let z=2147483647;for(let Q of q){let W=Number(`0x${Q}`);if(W>z)W-=z;$+=`/${W}${J?"'":""}`}return $},getNextIdentityPath(j){let J=j.split("/"),$=J[J.length-2],q=!1;if($.match("'"))q=!0;let z=(Number($.replace(/[^0-9]/g,""))+1).toString();return J[J.length-2]=z+(q?"'":""),J[J.length-1]=`0${q?"'":""}`,J.join("/")},getNextPath(j){let J=j.split("/"),$=J[J.length-1],q=!1;if($.match("'"))q=!0;let z=(Number($.replace(/[^0-9]/g,""))+1).toString();return J[J.length-1]=z+(q?"'":""),J.join("/")}};var{toArray:M,toUTF8:t,toBase64:jj}=a,{magicHash:Jj}=H,{electrumDecrypt:$j,electrumEncrypt:qj}=r;class R{identityAttributes={};signWithBSM(j,J){let $=J.toPublicKey().toAddress(),q=H.sign(j,J,"raw"),z=new e(Jj(j)),Q=q.CalculateRecoveryFactor(J.toPublicKey(),z),W=H.sign(j,J,"raw").toCompact(Q,!0,"base64");return{address:$,signature:W}}encrypt(j,J){let{privKey:$,pubKey:q}=this.getEncryptionKey(),z=J?P.fromString(J):q;return jj(qj(M(j),z,$))}decrypt(j,J){let{privKey:$}=this.getEncryptionKey(),q;if(J)q=P.fromString(J);return t($j(M(j,"base64"),$,q))}signOpReturnWithAIP(j,J){let $=this.getAIPMessageBuffer(j),{address:q,signature:z}=this.signMessage($.flat(),J);return this.formatAIPOutput($,q,z)}getAttributes(){return this.identityAttributes}getAttribute(j){if(this.identityAttributes[j])return this.identityAttributes[j];return null}setAttribute(j,J){if(!J)return;if(this.identityAttributes[j])this.updateExistingAttribute(j,J);else this.createNewAttribute(j,J)}unsetAttribute(j){delete this.identityAttributes[j]}addAttribute(j,J,$=""){let q=$;if(!$)q=G.getRandomString();this.identityAttributes[j]={value:J,nonce:q}}getAttributeUrns(){let j="";for(let J in this.identityAttributes){let $=this.getAttributeUrn(J);if($)j+=`${$}
2
+ import{BSM as A,BigNumber as Bj,ECIES as Ej,HD as Hj,OP as l,Signature as Tj,PrivateKey as Nj}from"@bsv/sdk";import{Utils as Sj}from"@bsv/sdk";var a=async(j,J,$,q)=>{let z=`${$}${j}`;return(await fetch(z,{method:"post",headers:{"Content-type":"application/json; charset=utf-8",token:q,format:"json"},body:JSON.stringify(J)})).json()},E=(j,J)=>async($,q)=>{return a($,q,j,J)};import{Utils as e}from"@bsv/sdk";var{toHex:c,toArray:b}=e,f="1BAPSuaPnfGnSBM3GLV9yhxUdYe4vGbdMT",h=c(b(f)),_="15PciHG22SNLQJXMoSUaWVi7WSqc7hCfva",gj=c(b(_)),H="https://api.sigmaidentity.com/v1",F=2147483647,M="m/424150'/0'/0'",Y=`m/424150'/${F}'/${F}'`;var R="1-bap-identity",p=2,P="friend";import{Utils as xj,ECIES as _j,Hash as V,PublicKey as N,HD as Rj}from"@bsv/sdk";import{Hash as o,PublicKey as t,Utils as jj}from"@bsv/sdk";var{toHex:Jj,toBase58:$j}=jj;function T(j){let J=Jj(o.sha256(j,"utf8"));return $j(o.ripemd160(J,"hex"))}function qj(j){let J=t.fromString(j);return T(J.toAddress())}var X={getRandomBytes(j=32){if(typeof globalThis<"u"&&globalThis.crypto&&globalThis.crypto.getRandomValues){let J=new Uint8Array(j);return globalThis.crypto.getRandomValues(J),J}throw Error("Secure random number generation not available. crypto.getRandomValues() is required for cryptographic operations. This environment may not be suitable for secure key generation.")},getRandomString(j=32){let J=this.getRandomBytes(j);return Array.from(J,($)=>$.toString(16).padStart(2,"0")).join("")},getSigningPathFromHex(j,J=!0){let $="m",q=j.match(/.{1,8}/g);if(!q)throw Error("Invalid hex string");let z=2147483647;for(let Q of q){let W=Number(`0x${Q}`);if(W>z)W-=z;$+=`/${W}${J?"'":""}`}return $},getNextIdentityPath(j){let J=j.split("/"),$=J[J.length-2],q=!1;if($.match("'"))q=!0;let z=(Number($.replace(/[^0-9]/g,""))+1).toString();return J[J.length-2]=z+(q?"'":""),J[J.length-1]=`0${q?"'":""}`,J.join("/")},getNextPath(j){let J=j.split("/"),$=J[J.length-1],q=!1;if($.match("'"))q=!0;let z=(Number($.replace(/[^0-9]/g,""))+1).toString();return J[J.length-1]=z+(q?"'":""),J.join("/")}};import{PublicKey as Xj,PrivateKey as v,Hash as Fj,Utils as fj,ECIES as Cj}from"@bsv/sdk";import{ECIES as zj,Utils as Qj,OP as u,PublicKey as n,BSM as D,BigNumber as Zj}from"@bsv/sdk";var{toArray:U,toUTF8:Wj,toBase64:wj}=Qj,{magicHash:Lj}=D,{electrumDecrypt:Yj,electrumEncrypt:Gj}=zj;class B{identityAttributes={};signWithBSM(j,J){let $=J.toPublicKey().toAddress(),q=D.sign(j,J,"raw"),z=new Zj(Lj(j)),Q=q.CalculateRecoveryFactor(J.toPublicKey(),z),W=D.sign(j,J,"raw").toCompact(Q,!0,"base64");return{address:$,signature:W}}encrypt(j,J){let{privKey:$,pubKey:q}=this.getEncryptionKey(),z=J?n.fromString(J):q;return wj(Gj(U(j),z,$))}decrypt(j,J){let{privKey:$}=this.getEncryptionKey(),q;if(J)q=n.fromString(J);return Wj(Yj(U(j,"base64"),$,q))}signOpReturnWithAIP(j,J){let $=this.getAIPMessageBuffer(j),{address:q,signature:z}=this.signMessage($.flat(),J);return this.formatAIPOutput($,q,z)}getAttributes(){return this.identityAttributes}getAttribute(j){if(this.identityAttributes[j])return this.identityAttributes[j];return null}setAttribute(j,J){if(!J)return;if(this.identityAttributes[j])this.updateExistingAttribute(j,J);else this.createNewAttribute(j,J)}unsetAttribute(j){delete this.identityAttributes[j]}addAttribute(j,J,$=""){let q=$;if(!$)q=X.getRandomString();this.identityAttributes[j]={value:J,nonce:q}}getAttributeUrns(){let j="";for(let J in this.identityAttributes){let $=this.getAttributeUrn(J);if($)j+=`${$}
3
3
  `}return j}getAttributeUrn(j){let J=this.identityAttributes[j];if(J)return`urn:bap:id:${j}:${J.value}:${J.nonce}`;return null}parseStringUrns(j){let J={},$=j.replace(/^\s+/g,"").replace(/\r/gm,"").split(`
4
- `);for(let q of $){let Q=q.replace(/^\s+/g,"").replace(/\s+$/g,"").split(":");if(Q[0]==="urn"&&Q[1]==="bap"&&Q[2]==="id"&&Q[3]&&Q[4]&&Q[5])J[Q[3]]={value:Q[4],nonce:Q[5]}}return J}parseAttributes(j){if(typeof j==="string")return this.parseStringUrns(j);for(let J in j)if(!j[J].value||!j[J].nonce)throw Error("Invalid identity attribute");return j||{}}updateExistingAttribute(j,J){if(typeof J==="string"){this.identityAttributes[j].value=J;return}if(this.identityAttributes[j].value=J.value||"",J.nonce)this.identityAttributes[j].nonce=J.nonce}createNewAttribute(j,J){if(typeof J==="string"){this.addAttribute(j,J);return}this.addAttribute(j,J.value||"",J.nonce)}getAIPMessageBuffer(j,J){let $=j.findIndex((z)=>z[0]===p.OP_RETURN),q=[];if($===-1)q.push([p.OP_RETURN]),$=0;if(J)for(let z of J)q.push(j[$+z]);else for(let z of j)q.push(z);return q}formatAIPOutput(j,J,$){let q=[M("|"),M(x),M("BITCOIN_ECDSA"),M(J),M($,"base64")];return[...j,...q]}}import{Utils as zj,ECIES as Qj,Hash as Zj,PrivateKey as S,PublicKey as Wj}from"@bsv/sdk";var{toArray:o,toUTF8:wj,toBase64:Lj,toHex:Yj}=zj,{electrumDecrypt:Gj,electrumEncrypt:Xj}=Qj;class O extends R{key;idName;description;address;identityKey;constructor(j,J={}){super();this.key=j,this.address=this.key.toPublicKey().toAddress(),this.idName="Member ID 1",this.description="",this.identityKey="",this.identityAttributes=this.parseAttributes(J)}signMessage(j,J){return this.signWithBSM(j,this.key)}signOpReturnWithAIP(j){let J=this.getAIPMessageBuffer(j),{address:$,signature:q}=this.signMessage(J.flat());return this.formatAIPOutput(J,$,q)}getPublicKey(){return this.key.toPublicKey().toString()}import(j){this.idName=j.name,this.description=j.description,this.key=S.fromWif(j.derivedPrivateKey),this.address=this.key.toPublicKey().toAddress(),this.identityAttributes=j.identityAttributes||{},this.identityKey=j.identityKey}static fromMemberIdentity(j){let J=new O(S.fromWif(j.derivedPrivateKey));return J.import(j),J}static fromBackup(j){let J=new O(S.fromWif(j.wif)),$=JSON.parse(J.decrypt(j.id));return J.import($),J}export(){return{name:this.idName,description:this.description,derivedPrivateKey:this.key.toWif(),address:this.address,identityAttributes:this.getAttributes(),identityKey:this.identityKey}}getEncryptionKey(){return{privKey:this.key.deriveChild(this.key.toPublicKey(),L),pubKey:this.key.deriveChild(this.key.toPublicKey(),L).toPublicKey()}}getEncryptionPublicKey(){let{pubKey:j}=this.getEncryptionKey();return j.toString()}getEncryptionPrivateKeyWithSeed(j){let J=Yj(Zj.sha256(j,"utf8")),$=`${b}-${h}-${J}`;return this.key.deriveChild(this.key.toPublicKey(),$)}getEncryptionKeyWithSeed(j){let J=this.getEncryptionPrivateKeyWithSeed(j);return{privKey:J,pubKey:J.toPublicKey()}}getEncryptionPublicKeyWithSeed(j){return this.getEncryptionPrivateKeyWithSeed(j).toPublicKey().toString()}encryptWithSeed(j,J,$){let q=this.getEncryptionPrivateKeyWithSeed(J),z=q.toPublicKey(),Q=this.key.toPublicKey().constructor,W=$?Q.fromString($):z;return Lj(Xj(o(j),W,q))}decryptWithSeed(j,J,$){let q=this.getEncryptionPrivateKeyWithSeed(J),z;if($)z=Wj.fromString($);return wj(Gj(o(j,"base64"),q,z))}exportForBackup(j){let J=this.export(),$=this.encrypt(JSON.stringify(J));return{wif:this.key.toWif(),id:$,...j&&{label:j},createdAt:new Date().toISOString()}}}var{toArray:X,toHex:k,toBase58:fj,toUTF8:n,toBase64:D}=Fj,{electrumDecrypt:d,electrumEncrypt:I}=Cj;class f extends R{#J;#j;#$;#z=E;#W="";#q;#Z;#Q;#w;idName;description;rootAddress;identityKey;identityAttributes;getApiData;constructor(j,J={},$=""){super();if(j instanceof Uj)if(this.#$=!1,$){let z=k(U.sha256($,"utf8")),Q=G.getSigningPathFromHex(z);this.#J=j.derive(Q)}else this.#J=j;else if(this.#$=!0,this.#j=j.rootPk,$){let z=k(U.sha256($,"utf8"));this.#j=this.#j.deriveChild(this.#j.toPublicKey(),z)}if(this.#w=$,this.idName="ID 1",this.description="",this.#q=`${V}/0/0/0`,this.#Z=`${V}/0/0/0`,this.#Q=`${V}/0/0/1`,this.#$){if(!this.#j)throw Error("Master private key not initialized");let z=this.#j.deriveChild(this.#j.toPublicKey(),this.#q);this.rootAddress=z.toPublicKey().toAddress()}else{if(!this.#J)throw Error("HD private key not initialized");let z=this.#J.derive(this.#q);this.rootAddress=z.privKey.toPublicKey().toAddress()}this.identityKey=this.deriveIdentityKey(this.rootAddress);let q={...J};this.identityAttributes=this.parseAttributes(q),this.getApiData=B(this.#z,this.#W)}set BAP_SERVER(j){this.#z=j}get BAP_SERVER(){return this.#z}set BAP_TOKEN(j){this.#W=j}get BAP_TOKEN(){return this.#W}deriveIdentityKey(j){let J=k(U.sha256(j,"utf8"));return fj(U.ripemd160(J,"hex"))}parseAttributes(j){if(typeof j==="string")return this.parseStringUrns(j);for(let J in j)if(!j[J].value||!j[J].nonce)throw Error("Invalid identity attribute");return j||{}}parseStringUrns(j){let J={},$=j.replace(/^\s+/g,"").replace(/\r/gm,"").split(`
5
- `);for(let q of $){let Q=q.replace(/^\s+/g,"").replace(/\s+$/g,"").split(":");if(Q[0]==="urn"&&Q[1]==="bap"&&Q[2]==="id"&&Q[3]&&Q[4]&&Q[5])J[Q[3]]={value:Q[4],nonce:Q[5]}}return J}getIdentityKey(){return this.identityKey}set rootPath(j){if(this.#$){if(this.#q=j,!this.#j)throw Error("Master private key not initialized");let J=this.#j.deriveChild(this.#j.toPublicKey(),j);this.rootAddress=J.toPublicKey().toAddress(),this.#Z=j,this.#Q=j}else{let J=j;if(j.split("/").length<5)J=`${V}${j}`;if(!this.validatePath(J))throw Error(`invalid signing path given ${J}`);if(this.#q=J,!this.#J)throw Error("HD private key not initialized");let $=this.#J.derive(J);this.rootAddress=$.pubKey.toAddress(),this.#Z=J,this.#Q=J}this.identityKey=this.deriveIdentityKey(this.rootAddress)}get rootPath(){return this.#q}getRootPath(){return this.#q}set currentPath(j){if(this.#$)this.#Z=this.#Q,this.#Q=j;else{let J=j;if(j.split("/").length<5)J=`${V}${j}`;if(!this.validatePath(J))throw Error("invalid signing path given");this.#Z=this.#Q,this.#Q=J}}get currentPath(){return this.#Q}get previousPath(){return this.#Z}get idSeed(){return this.#w}incrementPath(){this.currentPath=G.getNextPath(this.currentPath)}validatePath(j){if(j.match(/\/[0-9]{1,10}'?\/[0-9]{1,10}'?\/[0-9]{1,10}'?\/[0-9]{1,10}'?\/[0-9]{1,10}'?\/[0-9]{1,10}'?/)){let J=j.split("/");if(J.length===7&&Number(J[1].replace("'",""))<=F&&Number(J[2].replace("'",""))<=F&&Number(J[3].replace("'",""))<=F&&Number(J[4].replace("'",""))<=F&&Number(J[5].replace("'",""))<=F&&Number(J[6].replace("'",""))<=F)return!0}return!1}getInitialIdTransaction(){return this.getIdTransaction(this.#q)}getIdTransaction(j=""){if(this.#Q===this.#q)throw Error("Current path equals rootPath. ID was probably not initialized properly");let J=[X(C),X("ID"),X(this.identityKey),X(this.getCurrentAddress())];return this.signOpReturnWithAIP(J,j||this.#Z)}getPathDerivedKey(j){if(this.#$){if(!this.#j)throw Error("Master private key not initialized");return this.#j.deriveChild(this.#j.toPublicKey(),j)}if(!this.#J)throw Error("HD private key not initialized");return this.#J.derive(j).privKey}getAddress(j){return this.getPathDerivedKey(j).toPublicKey().toAddress()}getCurrentAddress(){return this.getAddress(this.#Q)}getEncryptionKey(){if(this.#$){if(!this.#j)throw Error("Master private key not initialized");let $=this.#j.deriveChild(this.#j.toPublicKey(),this.#q),q=$.deriveChild($.toPublicKey(),L);return{privKey:q,pubKey:q.toPublicKey()}}if(!this.#J)throw Error("HD private key not initialized");let J=this.#J.derive(this.#q).derive(L).privKey;return{privKey:J,pubKey:J.toPublicKey()}}getEncryptionKeyType42(){if(this.#$)return this.getEncryptionKey();if(!this.#J)throw Error("HD private key not initialized");let j=this.#J.derive(this.#q),J=j.privKey.deriveChild(j.toPublic().pubKey,L);return{privKey:J,pubKey:J.toPublicKey()}}getEncryptionPublicKey(){let{pubKey:j}=this.getEncryptionKey();return j.toString()}getEncryptionPublicKeyWithSeed(j){return this.getEncryptionPrivateKeyWithSeed(j).toPublicKey().toString()}encrypt(j,J){let{privKey:$,pubKey:q}=this.getEncryptionKey(),z=J?N.fromString(J):q;return D(I(X(j),z,$))}decrypt(j,J){let{privKey:$}=this.getEncryptionKey(),q;if(J)q=N.fromString(J);return n(d(X(j,"base64"),$,q))}encryptWithSeed(j,J,$){let q=this.getEncryptionPrivateKeyWithSeed(J),z=q.toPublicKey(),Q=$?N.fromString($):z;return D(I(X(j),Q,q))}decryptWithSeed(j,J,$){let q=this.getEncryptionPrivateKeyWithSeed(J),z;if($)z=N.fromString($);return n(d(X(j,"base64"),q,z))}getEncryptionPrivateKeyWithSeed(j){let J=k(U.sha256(j,"utf8"));if(this.#$){if(!this.#j)throw Error("Master private key not initialized");let z=this.#j.deriveChild(this.#j.toPublicKey(),this.#q);return z.deriveChild(z.toPublicKey(),J)}if(!this.#J)throw Error("HD private key not initialized");let $=G.getSigningPathFromHex(J);return this.#J.derive(this.#q).derive($).privKey}getAttestation(j){let J=U.sha256(j,"utf8");return`bap:attest:${k(J)}:${this.getIdentityKey()}`}getAttestationHash(j){let J=this.getAttributeUrn(j);if(!J)return null;let $=this.getAttestation(J),q=U.sha256($,"utf8");return k(q)}signMessage(j,J){let $=J||this.#Q;return this.signWithBSM(j,this.getPathDerivedKey($))}signMessageWithSeed(j,J){let $=k(U.sha256(J,"utf8")),q;if(this.#$){if(!this.#j)throw Error("Master private key not initialized");let z=this.#j.deriveChild(this.#j.toPublicKey(),this.#q);q=z.deriveChild(z.toPublicKey(),$)}else{if(!this.#J)throw Error("HD private key not initialized");let z=G.getSigningPathFromHex($);q=this.#J.derive(this.#q).derive(z).privKey}return this.signWithBSM(X(j,"utf8"),q)}signOpReturnWithAIP(j,J=""){let $=this.getAIPMessageBuffer(j),{address:q,signature:z}=this.signMessage($.flat(),J);return this.formatAIPOutput(j,q,z)}async getIdSigningKeys(){let j=await this.getApiData("/signing-keys",{idKey:this.identityKey});return console.log("getIdSigningKeys",j),j}async getAttributeAttestations(j){let J=this.getAttestationHash(j),$=await this.getApiData("/attestation/get",{hash:J});return console.log("getAttestations",j,J,$),$}import(j){this.idName=j.name,this.description=j.description||"",this.identityKey=j.identityKey,this.#q=j.rootPath,this.rootAddress=j.rootAddress,this.#Z=j.previousPath,this.#Q=j.currentPath,this.#w=("idSeed"in j?j.idSeed:"")||"",this.identityAttributes=this.parseAttributes(j.identityAttributes)}export(){return{name:this.idName,description:this.description,identityKey:this.identityKey,rootPath:this.#q,rootAddress:this.rootAddress,previousPath:this.#Z,currentPath:this.#Q,idSeed:this.#w,identityAttributes:this.getAttributes(),lastIdPath:""}}exportMemberBackup(){let j=this.getPathDerivedKey(this.#Q);return{name:this.idName,description:this.description,derivedPrivateKey:j.toWif(),address:j.toPublicKey().toAddress(),identityAttributes:this.getAttributes(),identityKey:this.identityKey}}newId(){this.incrementPath();let j=this.getPathDerivedKey(this.#Q);return new O(j)}exportMember(){let j=this.exportMemberBackup(),J=this.getPathDerivedKey(this.#Q),$=D(I(X(JSON.stringify(j)),J.toPublicKey()));return{wif:j.derivedPrivateKey,encryptedData:$}}}var{toArray:Z,toUTF8:_,toBase64:A,toHex:T}=xj,{electrumEncrypt:u,electrumDecrypt:y}=kj;class Rj{#J;#j;#$;#z={};#W=E;#q="";#Z="";#Q=0;getApiData;constructor(j,J="",$=""){if(!j)throw Error("No key source given");if(typeof j==="string")this.#J=Vj.fromString(j),this.#$=!1;else this.#j=_j.fromWif(j.rootPk),this.#$=!0;if(J)this.#q=J;if($)this.#W=$;this.getApiData=B(this.#W,this.#q)}get lastIdPath(){return this.#Z}getPublicKey(j=""){if(this.#$){if(!this.#j)throw Error("Master private key not initialized");if(j)return this.#j.deriveChild(this.#j.toPublicKey(),j).toPublicKey().toString();return this.#j.toPublicKey().toString()}if(!this.#J)throw Error("HD private key not initialized");if(j)return this.#J.derive(j).pubKey.toString();return this.#J.pubKey.toString()}getHdPublicKey(j=""){if(this.#$)throw Error("HD public keys are not available in Type 42 mode");if(!this.#J)throw Error("HD private key not initialized");if(j)return this.#J.derive(j).toPublic().toString();return this.#J.toPublic().toString()}set BAP_SERVER(j){this.#W=j;for(let J in this.#z)this.#z[J].BAP_SERVER=j}get BAP_SERVER(){return this.#W}set BAP_TOKEN(j){this.#q=j;for(let J in this.#z)this.#z[J].BAP_TOKEN=j}get BAP_TOKEN(){return this.#q}checkIdBelongs(j){let J;if(this.#$){if(!this.#j)throw Error("Master private key not initialized");J=this.#j.deriveChild(this.#j.toPublicKey(),j.rootPath).toPublicKey().toAddress()}else{if(!this.#J)throw Error("HD private key not initialized");J=this.#J.derive(j.rootPath).pubKey.toAddress()}if(J!==j.rootAddress)throw Error("ID does not belong to this private key");return!0}listIds(){return Object.keys(this.#z)}newId(j,J,$={},q=""){let z,Q,W;if(typeof j==="object"||j===void 0||typeof j==="string"&&j.startsWith("/"))Q=typeof j==="string"?j:void 0,W=typeof j==="object"?j:typeof J==="object"?J:{},z="Default Identity";else z=j,Q=typeof J==="string"?J:void 0,W=typeof J==="object"?J:$;let w;if(Q)w=Q;else if(this.#$)w=`bap:${this.#Q}`,this.#Q++;else w=this.getNextValidPath();let Y;if(this.#$){if(!this.#j)throw Error("Type 42 parameters not initialized");Y=new f({rootPk:this.#j},W,q)}else{if(!this.#J)throw Error("HD private key not initialized");Y=new f(this.#J,W,q)}if(Y.BAP_SERVER=this.#W,Y.BAP_TOKEN=this.#q,Y.idName=z,Y.rootPath=w,this.#$)Y.currentPath=w;else Y.currentPath=G.getNextPath(w);let l=Y.getIdentityKey();return this.#z[l]=Y,this.#Z=w,this.#z[l]}removeId(j){delete this.#z[j]}getNextValidPath(){if(this.#Z)return G.getNextIdentityPath(this.#Z);return`/0'/${Object.keys(this.#z).length}'/0'`}newIdWithCounter(j,J=`Identity ${j}`){if(!this.#$)throw Error("newIdWithCounter only works in Type 42 mode");let $=`bap:${j}`;return this.newId(J,$)}getId(j){return this.#z[j]||null}setId(j){this.checkIdBelongs(j),this.#z[j.getIdentityKey()]=j}importIds(j,J=!0){if(J&&typeof j==="string"){this.importEncryptedIds(j);return}let $=j;if(!$.lastIdPath)throw Error("ID cannot be imported as it is not complete");if(!$.ids)throw Error(`ID data is not in the correct format: ${j}`);let q=j.lastIdPath;for(let z of $.ids){if(!z.identityKey||!z.identityAttributes||!z.rootAddress)throw Error("ID cannot be imported as it is not complete");let Q;if(this.#$){if(!this.#j)throw Error("Type 42 parameters not initialized");Q=new f({rootPk:this.#j},{},z.idSeed)}else{if(!this.#J)throw Error("HD private key not initialized");Q=new f(this.#J,{},z.idSeed)}if(Q.BAP_SERVER=this.#W,Q.BAP_TOKEN=this.#q,Q.import(z),q==="")q=Q.currentPath;if(this.checkIdBelongs(Q),this.#z[Q.getIdentityKey()]=Q,this.#$&&Q.rootPath.startsWith("bap:")){let W=Q.rootPath.split(":");if(W.length>=2){let w=Number.parseInt(W[1],10);if(!Number.isNaN(w))this.#Q=Math.max(this.#Q,w+1)}}}this.#Z=q}importEncryptedIds(j){let J=this.decrypt(j),$=JSON.parse(J);if(Array.isArray($)){console.log(`Importing old format:
6
- `,$),this.importOldIds($);return}if(typeof $!=="object")throw Error("decrypted, but found unrecognized identities format");this.importIds($,!1)}importOldIds(j){for(let J of j){let $;if(this.#$){if(!this.#j)throw Error("Type 42 parameters not initialized");$=new f({rootPk:this.#j},{},J.idSeed??"")}else{if(!this.#J)throw Error("HD private key not initialized");$=new f(this.#J,{},J.idSeed??"")}$.BAP_SERVER=this.#W,$.BAP_TOKEN=this.#q,$.import(J),this.checkIdBelongs($),this.#z[$.getIdentityKey()]=$,this.#Z=$.currentPath}}exportIds(j,J=!0){let $={lastIdPath:this.#Z,ids:[]},q=j||Object.keys(this.#z);for(let z of q){if(!this.#z[z])throw Error(`Identity ${z} not found`);$.ids.push(this.#z[z].export())}if(J)return this.encrypt(JSON.stringify($));return $}exportId(j,J=!0){let $={lastIdPath:this.#Z,ids:[]};if($.ids.push(this.#z[j].export()),J)return this.encrypt(JSON.stringify($));return $}encrypt(j){if(this.#$){if(!this.#j)throw Error("Master private key not initialized");let $=this.#j.deriveChild(this.#j.toPublicKey(),L);return A(u(Z(j),$.toPublicKey(),null))}if(!this.#J)throw Error("HD private key not initialized");let J=this.#J.derive(L);return A(u(Z(j),J.pubKey,null))}decrypt(j){if(this.#$){if(!this.#j)throw Error("Master private key not initialized");let $=this.#j.deriveChild(this.#j.toPublicKey(),L);return _(y(Z(j,"base64"),$))}if(!this.#J)throw Error("HD private key not initialized");let J=this.#J.derive(L);return _(y(Z(j,"base64"),J.privKey))}signAttestationWithAIP(j,J,$=0,q=""){let z=this.getId(J);if(!z)throw Error("Could not find identity to attest with");let Q=this.getAttestationBuffer(j,$,q),{address:W,signature:w}=z.signMessage(Q);return this.createAttestationTransaction(j,$,W,w,q)}verifyAttestationWithAIP(j){if(!j.every((q)=>Array.isArray(q))||j[0][0]!==v.OP_RETURN||T(j[1])!==c)throw Error("Not a valid BAP transaction");let J=T(j[7])==="44415441"?5:0,$={type:_(j[2]),hash:T(j[3]),sequence:_(j[4]),signingProtocol:_(j[7+J]),signingAddress:_(j[8+J]),signature:A(j[9+J])};if(J&&j[3]===j[8])$.data=T(j[9]);console.log({attestation:$});try{let q=[];for(let z=0;z<6+J;z++)q.push(j[z]);$.verified=this.verifySignature(q.flat(),$.signingAddress,$.signature)}catch{$.verified=!1}return $}createAttestationTransaction(j,J,$,q,z=""){let Q=[[v.OP_RETURN],Z(C),Z("ATTEST"),Z(j),Z(`${J}`),Z("|")];if(z)Q.push(Z(C),Z("DATA"),Z(j),Z(z),Z("|"));return Q.push(Z(x),Z("BITCOIN_ECDSA"),Z($),Z(q,"base64")),console.log({elements:Q}),Q}getAttestationBuffer(j,J=0,$=""){let q=[[v.OP_RETURN],Z(C),Z("ATTEST"),Z(j),Z(`${J}`),Z("|")];if($)q.push(Z(C),Z("DATA"),Z(j),Z($),Z("|"));return q.flat()}verifySignature(j,J,$){let q;if(Array.isArray(j))q=j;else if(Buffer.isBuffer(j))q=[...j];else q=Z(j,"utf8");let z=Oj.fromCompact($,"base64"),Q;for(let W=0;W<4;W++)try{if(Q=z.RecoverPublicKey(W,new Mj(g.magicHash(q))),g.verify(q,z,Q)&&Q.toAddress()===J)return!0}catch{}return!1}async verifyChallengeSignature(j,J,$,q){if(!this.verifySignature($,J,q))return!1;try{let Q=await this.getApiData("/attestation/valid",{idKey:j,address:J,challenge:$,signature:q});if(Q?.status==="success"&&Q?.result?.valid===!0)return!0;return!1}catch(Q){return console.error("API call failed:",Q),!1}}async isValidAttestationTransaction(j){if(this.verifyAttestationWithAIP(j))return this.getApiData("/attestation/valid",{tx:j});return!1}async getIdentityFromAddress(j){return this.getApiData("/identity/validByAddress",{address:j})}async getIdentity(j){return this.getApiData("/identity/get",{idKey:j})}async getAttestationsForHash(j){return this.getApiData("/attestations",{hash:j})}exportForBackup(j,J,$){let z={ids:this.exportIds(),...j&&{label:j},createdAt:new Date().toISOString()};if(this.#$){if(!this.#j)throw Error("Type 42 parameters not initialized");return{...z,rootPk:this.#j.toWif()}}if(!this.#J)throw Error("HD private key not initialized");return{...z,xprv:J||this.#J.toString(),mnemonic:$||""}}exportMemberForBackup(j,J){let $=this.#z[j];if(!$)throw Error(`Identity ${j} not found`);let q=$.exportMember();return{wif:q.wif,id:q.encryptedData,...J&&{label:J},createdAt:new Date().toISOString()}}}export{O as MemberID,f as MasterID,Rj as BAP};
4
+ `);for(let q of $){let Q=q.replace(/^\s+/g,"").replace(/\s+$/g,"").split(":");if(Q[0]==="urn"&&Q[1]==="bap"&&Q[2]==="id"&&Q[3]&&Q[4]&&Q[5])J[Q[3]]={value:Q[4],nonce:Q[5]}}return J}parseAttributes(j){if(typeof j==="string")return this.parseStringUrns(j);for(let J in j)if(!j[J].value||!j[J].nonce)throw Error("Invalid identity attribute");return j||{}}updateExistingAttribute(j,J){if(typeof J==="string"){this.identityAttributes[j].value=J;return}if(this.identityAttributes[j].value=J.value||"",J.nonce)this.identityAttributes[j].nonce=J.nonce}createNewAttribute(j,J){if(typeof J==="string"){this.addAttribute(j,J);return}this.addAttribute(j,J.value||"",J.nonce)}getAIPMessageBuffer(j,J){let $=j.findIndex((z)=>z[0]===u.OP_RETURN),q=[];if($===-1)q.push([u.OP_RETURN]),$=0;if(J)for(let z of J)q.push(j[$+z]);else for(let z of j)q.push(z);return q}formatAIPOutput(j,J,$){let q=[U("|"),U(_),U("BITCOIN_ECDSA"),U(J),U($,"base64")];return[...j,...q]}}var{toArray:d,toUTF8:Uj,toBase64:Mj,toHex:kj}=fj,{electrumDecrypt:Vj,electrumEncrypt:Oj}=Cj;class k extends B{key;idName;description;address;identityKey;constructor(j,J={}){super();this.key=j,this.address=this.getIdentitySigningKey().toPublicKey().toAddress(),this.idName="Member ID 1",this.description="",this.identityKey="",this.identityAttributes=this.parseAttributes(J)}getIdentitySigningKey(){return this.key.deriveChild(this.key.toPublicKey(),R)}getMemberKey(){return this.key.toPublicKey().toString()}getLegacyAddress(){return this.key.toPublicKey().toAddress()}signMessage(j,J){let $=this.getIdentitySigningKey();return this.signWithBSM(j,$)}signOpReturnWithAIP(j){let J=this.getAIPMessageBuffer(j),{address:$,signature:q}=this.signMessage(J.flat());return this.formatAIPOutput(J,$,q)}getPublicKey(){return this.getIdentitySigningKey().toPublicKey().toString()}import(j){this.idName=j.name,this.description=j.description,this.key=v.fromWif(j.derivedPrivateKey),this.address=this.getIdentitySigningKey().toPublicKey().toAddress(),this.identityAttributes=j.identityAttributes||{},this.identityKey=j.identityKey}static fromMemberIdentity(j){let J=new k(v.fromWif(j.derivedPrivateKey));return J.import(j),J}static fromBackup(j){let J=new k(v.fromWif(j.wif)),$=JSON.parse(J.decrypt(j.id));return J.import($),J}export(){return{name:this.idName,description:this.description,derivedPrivateKey:this.key.toWif(),address:this.address,identityAttributes:this.getAttributes(),identityKey:this.identityKey}}getEncryptionKey(){return{privKey:this.key.deriveChild(this.key.toPublicKey(),Y),pubKey:this.key.deriveChild(this.key.toPublicKey(),Y).toPublicKey()}}getEncryptionPublicKey(){let{pubKey:j}=this.getEncryptionKey();return j.toString()}getEncryptionPrivateKeyWithSeed(j){let J=kj(Fj.sha256(j,"utf8")),$=`${p}-${P}-${J}`;return this.key.deriveChild(this.key.toPublicKey(),$)}getEncryptionKeyWithSeed(j){let J=this.getEncryptionPrivateKeyWithSeed(j);return{privKey:J,pubKey:J.toPublicKey()}}getEncryptionPublicKeyWithSeed(j){return this.getEncryptionPrivateKeyWithSeed(j).toPublicKey().toString()}encryptWithSeed(j,J,$){let q=this.getEncryptionPrivateKeyWithSeed(J),z=q.toPublicKey(),Q=this.key.toPublicKey().constructor,W=$?Q.fromString($):z;return Mj(Oj(d(j),W,q))}decryptWithSeed(j,J,$){let q=this.getEncryptionPrivateKeyWithSeed(J),z;if($)z=Xj.fromString($);return Uj(Vj(d(j,"base64"),q,z))}exportForBackup(j){let J=this.export(),$=this.encrypt(JSON.stringify(J));return{wif:this.key.toWif(),id:$,...j&&{label:j},createdAt:new Date().toISOString()}}}var{toArray:L,toHex:O,toBase58:aj,toUTF8:y,toBase64:I}=xj,{electrumDecrypt:s,electrumEncrypt:g}=_j;class C extends B{#J;#j;#q;#Q=H;#W="";#$;#Z;#z;#w;idName;description;rootAddress;identityKey;identityAttributes;getApiData;constructor(j,J={},$=""){super();if(j instanceof Rj)if(this.#q=!1,$){let z=O(V.sha256($,"utf8")),Q=X.getSigningPathFromHex(z);this.#J=j.derive(Q)}else this.#J=j;else if(this.#q=!0,this.#j=j.rootPk,$){let z=O(V.sha256($,"utf8"));this.#j=this.#j.deriveChild(this.#j.toPublicKey(),z)}if(this.#w=$,this.idName="ID 1",this.description="",this.#$=`${M}/0/0/0`,this.#Z=`${M}/0/0/0`,this.#z=`${M}/0/0/1`,this.#q){if(!this.#j)throw Error("Master private key not initialized");let z=this.#j.deriveChild(this.#j.toPublicKey(),this.#$);this.rootAddress=z.toPublicKey().toAddress()}else{if(!this.#J)throw Error("HD private key not initialized");let z=this.#J.derive(this.#$);this.rootAddress=z.privKey.toPublicKey().toAddress()}this.identityKey=this.deriveIdentityKey(this.rootAddress);let q={...J};this.identityAttributes=this.parseAttributes(q),this.getApiData=E(this.#Q,this.#W)}set BAP_SERVER(j){this.#Q=j}get BAP_SERVER(){return this.#Q}set BAP_TOKEN(j){this.#W=j}get BAP_TOKEN(){return this.#W}deriveIdentityKey(j){return T(j)}parseAttributes(j){if(typeof j==="string")return this.parseStringUrns(j);for(let J in j)if(!j[J].value||!j[J].nonce)throw Error("Invalid identity attribute");return j||{}}parseStringUrns(j){let J={},$=j.replace(/^\s+/g,"").replace(/\r/gm,"").split(`
5
+ `);for(let q of $){let Q=q.replace(/^\s+/g,"").replace(/\s+$/g,"").split(":");if(Q[0]==="urn"&&Q[1]==="bap"&&Q[2]==="id"&&Q[3]&&Q[4]&&Q[5])J[Q[3]]={value:Q[4],nonce:Q[5]}}return J}getIdentityKey(){return this.identityKey}set rootPath(j){if(this.#q){if(this.#$=j,!this.#j)throw Error("Master private key not initialized");let J=this.#j.deriveChild(this.#j.toPublicKey(),j);this.rootAddress=J.toPublicKey().toAddress(),this.#Z=j,this.#z=j}else{let J=j;if(j.split("/").length<5)J=`${M}${j}`;if(!this.validatePath(J))throw Error(`invalid signing path given ${J}`);if(this.#$=J,!this.#J)throw Error("HD private key not initialized");let $=this.#J.derive(J);this.rootAddress=$.pubKey.toAddress(),this.#Z=J,this.#z=J}this.identityKey=this.deriveIdentityKey(this.rootAddress)}get rootPath(){return this.#$}getRootPath(){return this.#$}set currentPath(j){if(this.#q)this.#Z=this.#z,this.#z=j;else{let J=j;if(j.split("/").length<5)J=`${M}${j}`;if(!this.validatePath(J))throw Error("invalid signing path given");this.#Z=this.#z,this.#z=J}}get currentPath(){return this.#z}get previousPath(){return this.#Z}get idSeed(){return this.#w}incrementPath(){this.currentPath=X.getNextPath(this.currentPath)}validatePath(j){if(j.match(/\/[0-9]{1,10}'?\/[0-9]{1,10}'?\/[0-9]{1,10}'?\/[0-9]{1,10}'?\/[0-9]{1,10}'?\/[0-9]{1,10}'?/)){let J=j.split("/");if(J.length===7&&Number(J[1].replace("'",""))<=F&&Number(J[2].replace("'",""))<=F&&Number(J[3].replace("'",""))<=F&&Number(J[4].replace("'",""))<=F&&Number(J[5].replace("'",""))<=F&&Number(J[6].replace("'",""))<=F)return!0}return!1}getInitialIdTransaction(){return this.getIdTransaction(this.#$)}getIdTransaction(j=""){if(this.#z===this.#$)throw Error("Current path equals rootPath. ID was probably not initialized properly");let J=[L(f),L("ID"),L(this.identityKey),L(this.getCurrentAddress())];return this.signOpReturnWithAIP(J,j||this.#Z)}getPathDerivedKey(j){if(this.#q){if(!this.#j)throw Error("Master private key not initialized");return this.#j.deriveChild(this.#j.toPublicKey(),j)}if(!this.#J)throw Error("HD private key not initialized");return this.#J.derive(j).privKey}getIdentitySigningKeyForPath(j){let J=this.getPathDerivedKey(j);return J.deriveChild(J.toPublicKey(),R)}getMemberKey(j){let J=j||this.#z;return this.getPathDerivedKey(J).toPublicKey().toString()}getLegacyAddress(j){let J=j||this.#z;return this.getPathDerivedKey(J).toPublicKey().toAddress()}needsRotation(j){let J=j||this.rootAddress,$=this.getLegacyAddress(this.#$);return J===$}getLegacyRotationTransaction(){let j=this.getAddress(this.#$),J=[L(f),L("ID"),L(this.identityKey),L(j)],$=this.getAIPMessageBuffer(J),q=this.getPathDerivedKey(this.#$),{address:z,signature:Q}=this.signWithBSM($.flat(),q);return this.formatAIPOutput(J,z,Q)}getAddress(j){return this.getIdentitySigningKeyForPath(j).toPublicKey().toAddress()}getCurrentAddress(){return this.getAddress(this.#z)}getEncryptionKey(){if(this.#q){if(!this.#j)throw Error("Master private key not initialized");let $=this.#j.deriveChild(this.#j.toPublicKey(),this.#$),q=$.deriveChild($.toPublicKey(),Y);return{privKey:q,pubKey:q.toPublicKey()}}if(!this.#J)throw Error("HD private key not initialized");let J=this.#J.derive(this.#$).derive(Y).privKey;return{privKey:J,pubKey:J.toPublicKey()}}getEncryptionKeyType42(){if(this.#q)return this.getEncryptionKey();if(!this.#J)throw Error("HD private key not initialized");let j=this.#J.derive(this.#$),J=j.privKey.deriveChild(j.toPublic().pubKey,Y);return{privKey:J,pubKey:J.toPublicKey()}}getEncryptionPublicKey(){let{pubKey:j}=this.getEncryptionKey();return j.toString()}getEncryptionPublicKeyWithSeed(j){return this.getEncryptionPrivateKeyWithSeed(j).toPublicKey().toString()}encrypt(j,J){let{privKey:$,pubKey:q}=this.getEncryptionKey(),z=J?N.fromString(J):q;return I(g(L(j),z,$))}decrypt(j,J){let{privKey:$}=this.getEncryptionKey(),q;if(J)q=N.fromString(J);return y(s(L(j,"base64"),$,q))}encryptWithSeed(j,J,$){let q=this.getEncryptionPrivateKeyWithSeed(J),z=q.toPublicKey(),Q=$?N.fromString($):z;return I(g(L(j),Q,q))}decryptWithSeed(j,J,$){let q=this.getEncryptionPrivateKeyWithSeed(J),z;if($)z=N.fromString($);return y(s(L(j,"base64"),q,z))}getEncryptionPrivateKeyWithSeed(j){let J=O(V.sha256(j,"utf8"));if(this.#q){if(!this.#j)throw Error("Master private key not initialized");let z=this.#j.deriveChild(this.#j.toPublicKey(),this.#$);return z.deriveChild(z.toPublicKey(),J)}if(!this.#J)throw Error("HD private key not initialized");let $=X.getSigningPathFromHex(J);return this.#J.derive(this.#$).derive($).privKey}getAttestation(j){let J=V.sha256(j,"utf8");return`bap:attest:${O(J)}:${this.getIdentityKey()}`}getAttestationHash(j){let J=this.getAttributeUrn(j);if(!J)return null;let $=this.getAttestation(J),q=V.sha256($,"utf8");return O(q)}signMessage(j,J){let $=J||this.#z,q=this.getIdentitySigningKeyForPath($);return this.signWithBSM(j,q)}signMessageWithSeed(j,J){let $=O(V.sha256(J,"utf8")),q;if(this.#q){if(!this.#j)throw Error("Master private key not initialized");let Q=this.#j.deriveChild(this.#j.toPublicKey(),this.#$);q=Q.deriveChild(Q.toPublicKey(),$)}else{if(!this.#J)throw Error("HD private key not initialized");let Q=X.getSigningPathFromHex($);q=this.#J.derive(this.#$).derive(Q).privKey}let z=q.deriveChild(q.toPublicKey(),R);return this.signWithBSM(L(j,"utf8"),z)}signOpReturnWithAIP(j,J=""){let $=this.getAIPMessageBuffer(j),{address:q,signature:z}=this.signMessage($.flat(),J);return this.formatAIPOutput(j,q,z)}async getIdSigningKeys(){let j=await this.getApiData("/signing-keys",{idKey:this.identityKey});return console.log("getIdSigningKeys",j),j}async getAttributeAttestations(j){let J=this.getAttestationHash(j),$=await this.getApiData("/attestation/get",{hash:J});return console.log("getAttestations",j,J,$),$}import(j){this.idName=j.name,this.description=j.description||"",this.identityKey=j.identityKey,this.#$=j.rootPath,this.rootAddress=j.rootAddress,this.#Z=j.previousPath,this.#z=j.currentPath,this.#w=("idSeed"in j?j.idSeed:"")||"",this.identityAttributes=this.parseAttributes(j.identityAttributes)}export(){return{name:this.idName,description:this.description,identityKey:this.identityKey,rootPath:this.#$,rootAddress:this.rootAddress,previousPath:this.#Z,currentPath:this.#z,idSeed:this.#w,identityAttributes:this.getAttributes(),lastIdPath:""}}exportMemberBackup(){let j=this.getPathDerivedKey(this.#z),J=this.getIdentitySigningKeyForPath(this.#z);return{name:this.idName,description:this.description,derivedPrivateKey:j.toWif(),address:J.toPublicKey().toAddress(),identityAttributes:this.getAttributes(),identityKey:this.identityKey}}newId(){this.incrementPath();let j=this.getPathDerivedKey(this.#z);return new k(j)}exportMember(){let j=this.exportMemberBackup(),J=this.getPathDerivedKey(this.#z),$=I(g(L(JSON.stringify(j)),J.toPublicKey()));return{wif:j.derivedPrivateKey,encryptedData:$}}}var{toArray:Z,toUTF8:x,toBase64:K,toHex:S}=Sj,{electrumEncrypt:i,electrumDecrypt:r}=Ej;class Dj{#J;#j;#q;#Q={};#W=H;#$="";#Z="";#z=0;getApiData;constructor(j,J="",$=""){if(!j)throw Error("No key source given");if(typeof j==="string")this.#J=Hj.fromString(j),this.#q=!1;else this.#j=Nj.fromWif(j.rootPk),this.#q=!0;if(J)this.#$=J;if($)this.#W=$;this.getApiData=E(this.#W,this.#$)}get lastIdPath(){return this.#Z}getPublicKey(j=""){if(this.#q){if(!this.#j)throw Error("Master private key not initialized");if(j)return this.#j.deriveChild(this.#j.toPublicKey(),j).toPublicKey().toString();return this.#j.toPublicKey().toString()}if(!this.#J)throw Error("HD private key not initialized");if(j)return this.#J.derive(j).pubKey.toString();return this.#J.pubKey.toString()}getHdPublicKey(j=""){if(this.#q)throw Error("HD public keys are not available in Type 42 mode");if(!this.#J)throw Error("HD private key not initialized");if(j)return this.#J.derive(j).toPublic().toString();return this.#J.toPublic().toString()}set BAP_SERVER(j){this.#W=j;for(let J in this.#Q)this.#Q[J].BAP_SERVER=j}get BAP_SERVER(){return this.#W}set BAP_TOKEN(j){this.#$=j;for(let J in this.#Q)this.#Q[J].BAP_TOKEN=j}get BAP_TOKEN(){return this.#$}checkIdBelongs(j){let J;if(this.#q){if(!this.#j)throw Error("Master private key not initialized");J=this.#j.deriveChild(this.#j.toPublicKey(),j.rootPath).toPublicKey().toAddress()}else{if(!this.#J)throw Error("HD private key not initialized");J=this.#J.derive(j.rootPath).pubKey.toAddress()}if(J!==j.rootAddress)throw Error("ID does not belong to this private key");return!0}listIds(){return Object.keys(this.#Q)}newId(j,J,$={},q=""){let z,Q,W;if(typeof j==="object"||j===void 0||typeof j==="string"&&j.startsWith("/"))Q=typeof j==="string"?j:void 0,W=typeof j==="object"?j:typeof J==="object"?J:{},z="Default Identity";else z=j,Q=typeof J==="string"?J:void 0,W=typeof J==="object"?J:$;let w;if(Q)w=Q;else if(this.#q)w=`bap:${this.#z}`,this.#z++;else w=this.getNextValidPath();let G;if(this.#q){if(!this.#j)throw Error("Type 42 parameters not initialized");G=new C({rootPk:this.#j},W,q)}else{if(!this.#J)throw Error("HD private key not initialized");G=new C(this.#J,W,q)}if(G.BAP_SERVER=this.#W,G.BAP_TOKEN=this.#$,G.idName=z,G.rootPath=w,this.#q)G.currentPath=w;else G.currentPath=X.getNextPath(w);let m=G.getIdentityKey();return this.#Q[m]=G,this.#Z=w,this.#Q[m]}removeId(j){delete this.#Q[j]}getNextValidPath(){if(this.#Z)return X.getNextIdentityPath(this.#Z);return`/0'/${Object.keys(this.#Q).length}'/0'`}newIdWithCounter(j,J=`Identity ${j}`){if(!this.#q)throw Error("newIdWithCounter only works in Type 42 mode");let $=`bap:${j}`;return this.newId(J,$)}getId(j){return this.#Q[j]||null}setId(j){this.checkIdBelongs(j),this.#Q[j.getIdentityKey()]=j}importIds(j,J=!0){if(J&&typeof j==="string"){this.importEncryptedIds(j);return}let $=j;if(!$.lastIdPath)throw Error("ID cannot be imported as it is not complete");if(!$.ids)throw Error(`ID data is not in the correct format: ${j}`);let q=j.lastIdPath;for(let z of $.ids){if(!z.identityKey||!z.identityAttributes||!z.rootAddress)throw Error("ID cannot be imported as it is not complete");let Q;if(this.#q){if(!this.#j)throw Error("Type 42 parameters not initialized");Q=new C({rootPk:this.#j},{},z.idSeed)}else{if(!this.#J)throw Error("HD private key not initialized");Q=new C(this.#J,{},z.idSeed)}if(Q.BAP_SERVER=this.#W,Q.BAP_TOKEN=this.#$,Q.import(z),q==="")q=Q.currentPath;if(this.checkIdBelongs(Q),this.#Q[Q.getIdentityKey()]=Q,this.#q&&Q.rootPath.startsWith("bap:")){let W=Q.rootPath.split(":");if(W.length>=2){let w=Number.parseInt(W[1],10);if(!Number.isNaN(w))this.#z=Math.max(this.#z,w+1)}}}this.#Z=q}importEncryptedIds(j){let J=this.decrypt(j),$=JSON.parse(J);if(Array.isArray($)){console.log(`Importing old format:
6
+ `,$),this.importOldIds($);return}if(typeof $!=="object")throw Error("decrypted, but found unrecognized identities format");this.importIds($,!1)}importOldIds(j){for(let J of j){let $;if(this.#q){if(!this.#j)throw Error("Type 42 parameters not initialized");$=new C({rootPk:this.#j},{},J.idSeed??"")}else{if(!this.#J)throw Error("HD private key not initialized");$=new C(this.#J,{},J.idSeed??"")}$.BAP_SERVER=this.#W,$.BAP_TOKEN=this.#$,$.import(J),this.checkIdBelongs($),this.#Q[$.getIdentityKey()]=$,this.#Z=$.currentPath}}exportIds(j,J=!0){let $={lastIdPath:this.#Z,ids:[]},q=j||Object.keys(this.#Q);for(let z of q){if(!this.#Q[z])throw Error(`Identity ${z} not found`);$.ids.push(this.#Q[z].export())}if(J)return this.encrypt(JSON.stringify($));return $}exportId(j,J=!0){let $={lastIdPath:this.#Z,ids:[]};if($.ids.push(this.#Q[j].export()),J)return this.encrypt(JSON.stringify($));return $}encrypt(j){if(this.#q){if(!this.#j)throw Error("Master private key not initialized");let $=this.#j.deriveChild(this.#j.toPublicKey(),Y);return K(i(Z(j),$.toPublicKey(),null))}if(!this.#J)throw Error("HD private key not initialized");let J=this.#J.derive(Y);return K(i(Z(j),J.pubKey,null))}decrypt(j){if(this.#q){if(!this.#j)throw Error("Master private key not initialized");let $=this.#j.deriveChild(this.#j.toPublicKey(),Y);return x(r(Z(j,"base64"),$))}if(!this.#J)throw Error("HD private key not initialized");let J=this.#J.derive(Y);return x(r(Z(j,"base64"),J.privKey))}signAttestationWithAIP(j,J,$=0,q=""){let z=this.getId(J);if(!z)throw Error("Could not find identity to attest with");let Q=this.getAttestationBuffer(j,$,q),{address:W,signature:w}=z.signMessage(Q);return this.createAttestationTransaction(j,$,W,w,q)}verifyAttestationWithAIP(j){if(!j.every((q)=>Array.isArray(q))||j[0][0]!==l.OP_RETURN||S(j[1])!==h)throw Error("Not a valid BAP transaction");let J=S(j[7])==="44415441"?5:0,$={type:x(j[2]),hash:S(j[3]),sequence:x(j[4]),signingProtocol:x(j[7+J]),signingAddress:x(j[8+J]),signature:K(j[9+J])};if(J&&j[3]===j[8])$.data=S(j[9]);console.log({attestation:$});try{let q=[];for(let z=0;z<6+J;z++)q.push(j[z]);$.verified=this.verifySignature(q.flat(),$.signingAddress,$.signature)}catch{$.verified=!1}return $}createAttestationTransaction(j,J,$,q,z=""){let Q=[[l.OP_RETURN],Z(f),Z("ATTEST"),Z(j),Z(`${J}`),Z("|")];if(z)Q.push(Z(f),Z("DATA"),Z(j),Z(z),Z("|"));return Q.push(Z(_),Z("BITCOIN_ECDSA"),Z($),Z(q,"base64")),console.log({elements:Q}),Q}getAttestationBuffer(j,J=0,$=""){let q=[[l.OP_RETURN],Z(f),Z("ATTEST"),Z(j),Z(`${J}`),Z("|")];if($)q.push(Z(f),Z("DATA"),Z(j),Z($),Z("|"));return q.flat()}verifySignature(j,J,$){let q;if(Array.isArray(j))q=j;else if(Buffer.isBuffer(j))q=[...j];else q=Z(j,"utf8");let z=Tj.fromCompact($,"base64"),Q;for(let W=0;W<4;W++)try{if(Q=z.RecoverPublicKey(W,new Bj(A.magicHash(q))),A.verify(q,z,Q)&&Q.toAddress()===J)return!0}catch{}return!1}async verifyChallengeSignature(j,J,$,q){if(!this.verifySignature($,J,q))return!1;try{let Q=await this.getApiData("/attestation/valid",{idKey:j,address:J,challenge:$,signature:q});if(Q?.status==="success"&&Q?.result?.valid===!0)return!0;return!1}catch(Q){return console.error("API call failed:",Q),!1}}async isValidAttestationTransaction(j){if(this.verifyAttestationWithAIP(j))return this.getApiData("/attestation/valid",{tx:j});return!1}async getIdentityFromAddress(j){return this.getApiData("/identity/validByAddress",{address:j})}async getIdentity(j){return this.getApiData("/identity/get",{idKey:j})}async getAttestationsForHash(j){return this.getApiData("/attestations",{hash:j})}exportForBackup(j,J,$){let z={ids:this.exportIds(),...j&&{label:j},createdAt:new Date().toISOString()};if(this.#q){if(!this.#j)throw Error("Type 42 parameters not initialized");return{...z,rootPk:this.#j.toWif()}}if(!this.#J)throw Error("HD private key not initialized");return{...z,xprv:J||this.#J.toString(),mnemonic:$||""}}exportMemberForBackup(j,J){let $=this.#Q[j];if(!$)throw Error(`Identity ${j} not found`);let q=$.exportMember();return{wif:q.wif,id:q.encryptedData,...J&&{label:J},createdAt:new Date().toISOString()}}}export{qj as bapIdFromPubkey,T as bapIdFromAddress,k as MemberID,C as MasterID,Dj as BAP};
7
7
 
8
- //# debugId=AAAE4A75607CA7B164756E2164756E21
8
+ //# debugId=29AD2D5A28DD750864756E2164756E21