@johnhenry/browsermesh-core 0.4.0 → 0.6.0
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.
- package/package.json +1 -1
- package/src/identity-wallet.mjs +6 -3
- package/src/identity.mjs +7 -2
- package/src/peer-tools.mjs +100 -15
package/package.json
CHANGED
package/src/identity-wallet.mjs
CHANGED
|
@@ -405,12 +405,15 @@ export class IdentityWallet {
|
|
|
405
405
|
* Verify a signature against a public key (raw bytes).
|
|
406
406
|
*
|
|
407
407
|
* @param {Uint8Array} pubKeyBytes
|
|
408
|
-
*
|
|
408
|
+
* Argument order is `(publicKey, signature, data)`, like WebCrypto.
|
|
409
|
+
* BREAKING in core 0.5.0: previously `(publicKey, data, signature)`.
|
|
410
|
+
*
|
|
409
411
|
* @param {BufferSource} sig
|
|
412
|
+
* @param {BufferSource} data
|
|
410
413
|
* @returns {Promise<boolean>}
|
|
411
414
|
*/
|
|
412
|
-
async verify(pubKeyBytes,
|
|
413
|
-
return this.#identityManager.verify(pubKeyBytes,
|
|
415
|
+
async verify(pubKeyBytes, sig, data) {
|
|
416
|
+
return this.#identityManager.verify(pubKeyBytes, sig, data);
|
|
414
417
|
}
|
|
415
418
|
|
|
416
419
|
/**
|
package/src/identity.mjs
CHANGED
|
@@ -614,12 +614,17 @@ export class MeshIdentityManager {
|
|
|
614
614
|
/**
|
|
615
615
|
* Verify a signature against a public key (raw bytes).
|
|
616
616
|
*
|
|
617
|
+
* Argument order is `(publicKey, signature, data)`, the same as
|
|
618
|
+
* `crypto.subtle.verify`. BREAKING in core 0.5.0: previously
|
|
619
|
+
* `(publicKey, data, signature)`. Throws a TypeError when the old order is
|
|
620
|
+
* detected (64-byte data with a non-64-byte signature).
|
|
621
|
+
*
|
|
617
622
|
* @param {Uint8Array} publicKeyBytes - Raw Ed25519 public key (32 bytes)
|
|
623
|
+
* @param {BufferSource} signature - Ed25519 signature (64 bytes)
|
|
618
624
|
* @param {BufferSource} data
|
|
619
|
-
* @param {BufferSource} signature
|
|
620
625
|
* @returns {Promise<boolean>}
|
|
621
626
|
*/
|
|
622
|
-
async verify(publicKeyBytes,
|
|
627
|
+
async verify(publicKeyBytes, signature, data) {
|
|
623
628
|
const publicKey = await crypto.subtle.importKey(
|
|
624
629
|
'raw',
|
|
625
630
|
publicKeyBytes,
|
package/src/peer-tools.mjs
CHANGED
|
@@ -727,57 +727,121 @@ export class MeshGatewayStatusTool extends BrowserTool {
|
|
|
727
727
|
}
|
|
728
728
|
}
|
|
729
729
|
|
|
730
|
+
// ── Byte/text helpers for the torrent and IPFS tools ──────────────────
|
|
731
|
+
|
|
732
|
+
const BASE64_RE = /^[A-Za-z0-9+/]*={0,2}$/
|
|
733
|
+
|
|
734
|
+
/** @param {Uint8Array} bytes @returns {string} */
|
|
735
|
+
function bytesToBase64(bytes) {
|
|
736
|
+
let bin = ''
|
|
737
|
+
const STEP = 0x8000
|
|
738
|
+
for (let i = 0; i < bytes.length; i += STEP) {
|
|
739
|
+
bin += String.fromCharCode.apply(null, bytes.subarray(i, i + STEP))
|
|
740
|
+
}
|
|
741
|
+
return btoa(bin)
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
/**
|
|
745
|
+
* Strict base64 decode: whitespace is ignored, anything else outside the
|
|
746
|
+
* alphabet (or a bad length) throws instead of decoding to garbage.
|
|
747
|
+
* @param {string} str @returns {Uint8Array}
|
|
748
|
+
*/
|
|
749
|
+
function base64ToBytes(str) {
|
|
750
|
+
const clean = String(str).replace(/\s+/g, '')
|
|
751
|
+
if (!BASE64_RE.test(clean) || clean.length % 4 === 1) {
|
|
752
|
+
throw new Error('data is not valid base64 (use encoding "text" for plain text)')
|
|
753
|
+
}
|
|
754
|
+
const bin = atob(clean)
|
|
755
|
+
const bytes = new Uint8Array(bin.length)
|
|
756
|
+
for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i)
|
|
757
|
+
return bytes
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/**
|
|
761
|
+
* The tools' `data` string -> bytes, per its `encoding` ('text' = UTF-8, the
|
|
762
|
+
* default; 'base64').
|
|
763
|
+
* @param {string} data @param {string} [encoding]
|
|
764
|
+
* @returns {Uint8Array}
|
|
765
|
+
*/
|
|
766
|
+
function decodeToolData(data, encoding = 'text') {
|
|
767
|
+
if (typeof data !== 'string') throw new Error('data must be a string')
|
|
768
|
+
if (encoding === 'text') return new TextEncoder().encode(data)
|
|
769
|
+
if (encoding === 'base64') return base64ToBytes(data)
|
|
770
|
+
throw new Error(`unknown encoding "${encoding}" (use "text" or "base64")`)
|
|
771
|
+
}
|
|
772
|
+
|
|
730
773
|
// ── Torrent tools ─────────────────────────────────────────────────────
|
|
731
774
|
|
|
732
775
|
export class TorrentSeedTool extends BrowserTool {
|
|
733
776
|
get name() { return 'torrent_seed' }
|
|
734
|
-
get description() { return 'Seed a file or data via BitTorrent-style P2P distribution.' }
|
|
777
|
+
get description() { return 'Seed a file or data via BitTorrent-style P2P distribution. Returns the magnet URI to hand to peers.' }
|
|
735
778
|
get parameters() {
|
|
736
779
|
return {
|
|
737
780
|
type: 'object',
|
|
738
781
|
properties: {
|
|
739
782
|
name: { type: 'string', description: 'Content name/identifier' },
|
|
740
|
-
data: { type: 'string', description: '
|
|
783
|
+
data: { type: 'string', description: 'Content to seed: plain text, or base64 when encoding is "base64"' },
|
|
784
|
+
encoding: {
|
|
785
|
+
type: 'string',
|
|
786
|
+
enum: ['text', 'base64'],
|
|
787
|
+
description: 'How data is encoded: "text" (UTF-8, default) or "base64" (for binary content)',
|
|
788
|
+
},
|
|
741
789
|
},
|
|
742
790
|
required: ['name', 'data'],
|
|
743
791
|
}
|
|
744
792
|
}
|
|
745
793
|
get permission() { return 'approve' }
|
|
746
794
|
|
|
747
|
-
async execute({ name, data }) {
|
|
795
|
+
async execute({ name, data, encoding = 'text' }) {
|
|
748
796
|
const tm = peerToolsContext.getTorrentManager()
|
|
749
797
|
if (!tm) return { success: false, output: '', error: 'Torrent manager not initialized.' }
|
|
750
798
|
try {
|
|
751
|
-
const
|
|
752
|
-
|
|
799
|
+
const bytes = decodeToolData(data, encoding)
|
|
800
|
+
const result = await tm.seed(bytes, { name })
|
|
801
|
+
const size = result?.size ?? bytes.byteLength
|
|
802
|
+
const id = result?.magnetURI || result?.infoHash || name
|
|
803
|
+
return { success: true, output: `Seeding "${name}" (${size} bytes): ${id}` }
|
|
753
804
|
} catch (err) {
|
|
754
805
|
return { success: false, output: '', error: err.message }
|
|
755
806
|
}
|
|
756
807
|
}
|
|
757
808
|
}
|
|
758
809
|
|
|
759
|
-
// ──
|
|
810
|
+
// ── Content-addressed store tools ─────────────────────────────────────
|
|
811
|
+
//
|
|
812
|
+
// Despite the `ipfs_` names (kept for compatibility), the store behind these
|
|
813
|
+
// is mesh-local with SHA-256 hex CIDs, not the public IPFS network.
|
|
760
814
|
|
|
761
815
|
export class IpfsStoreTool extends BrowserTool {
|
|
762
816
|
get name() { return 'ipfs_store' }
|
|
763
|
-
get description() { return 'Store data in the
|
|
817
|
+
get description() { return 'Store data in the mesh-local content-addressed store; returns its CID (a SHA-256 hex digest, not an IPFS CID). Content is local to this node.' }
|
|
764
818
|
get parameters() {
|
|
765
819
|
return {
|
|
766
820
|
type: 'object',
|
|
767
821
|
properties: {
|
|
768
|
-
data: { type: 'string', description: 'Data to store' },
|
|
822
|
+
data: { type: 'string', description: 'Data to store: plain text, or base64 when encoding is "base64"' },
|
|
823
|
+
encoding: {
|
|
824
|
+
type: 'string',
|
|
825
|
+
enum: ['text', 'base64'],
|
|
826
|
+
description: 'How data is encoded: "text" (UTF-8, default) or "base64" (for binary content)',
|
|
827
|
+
},
|
|
769
828
|
},
|
|
770
829
|
required: ['data'],
|
|
771
830
|
}
|
|
772
831
|
}
|
|
773
832
|
get permission() { return 'write' }
|
|
774
833
|
|
|
775
|
-
async execute({ data }) {
|
|
834
|
+
async execute({ data, encoding = 'text' }) {
|
|
776
835
|
const store = peerToolsContext.getIpfsStore()
|
|
777
836
|
if (!store) return { success: false, output: '', error: 'IPFS store not initialized.' }
|
|
778
837
|
try {
|
|
779
|
-
const
|
|
780
|
-
|
|
838
|
+
const bytes = decodeToolData(data, encoding)
|
|
839
|
+
const added = await store.add(bytes)
|
|
840
|
+
// IPFSStore.add() resolves { cid, size }; tolerate a store that returns just the cid string.
|
|
841
|
+
const cid = typeof added === 'string' ? added : added?.cid
|
|
842
|
+
if (!cid) throw new Error('store did not return a CID')
|
|
843
|
+
const size = (typeof added === 'object' ? added?.size : undefined) ?? bytes.byteLength
|
|
844
|
+
return { success: true, output: `Stored with CID: ${cid} (${size} bytes)` }
|
|
781
845
|
} catch (err) {
|
|
782
846
|
return { success: false, output: '', error: err.message }
|
|
783
847
|
}
|
|
@@ -786,25 +850,46 @@ export class IpfsStoreTool extends BrowserTool {
|
|
|
786
850
|
|
|
787
851
|
export class IpfsRetrieveTool extends BrowserTool {
|
|
788
852
|
get name() { return 'ipfs_retrieve' }
|
|
789
|
-
get description() { return 'Retrieve data from the
|
|
853
|
+
get description() { return 'Retrieve data from the mesh-local content-addressed store by CID. Returns UTF-8 text when the content is text, otherwise base64.' }
|
|
790
854
|
get parameters() {
|
|
791
855
|
return {
|
|
792
856
|
type: 'object',
|
|
793
857
|
properties: {
|
|
794
858
|
cid: { type: 'string', description: 'Content identifier (CID)' },
|
|
859
|
+
encoding: {
|
|
860
|
+
type: 'string',
|
|
861
|
+
enum: ['text', 'base64'],
|
|
862
|
+
description: 'Output encoding. Omit to get text when the content is valid UTF-8 and base64 otherwise; "text" fails on binary content; "base64" always returns base64.',
|
|
863
|
+
},
|
|
795
864
|
},
|
|
796
865
|
required: ['cid'],
|
|
797
866
|
}
|
|
798
867
|
}
|
|
799
868
|
get permission() { return 'read' }
|
|
800
869
|
|
|
801
|
-
async execute({ cid }) {
|
|
870
|
+
async execute({ cid, encoding }) {
|
|
802
871
|
const store = peerToolsContext.getIpfsStore()
|
|
803
872
|
if (!store) return { success: false, output: '', error: 'IPFS store not initialized.' }
|
|
804
873
|
try {
|
|
874
|
+
if (encoding !== undefined && encoding !== 'text' && encoding !== 'base64') {
|
|
875
|
+
throw new Error(`unknown encoding "${encoding}" (use "text" or "base64")`)
|
|
876
|
+
}
|
|
805
877
|
const data = await store.get(cid)
|
|
806
|
-
if (
|
|
807
|
-
return { success: true, output:
|
|
878
|
+
if (data === null || data === undefined) return { success: true, output: `CID ${cid} not found.` }
|
|
879
|
+
if (typeof data === 'string') return { success: true, output: data, encoding: 'text' }
|
|
880
|
+
|
|
881
|
+
const bytes = data instanceof Uint8Array ? data : new Uint8Array(data)
|
|
882
|
+
if (encoding === 'base64') return { success: true, output: bytesToBase64(bytes), encoding: 'base64' }
|
|
883
|
+
try {
|
|
884
|
+
return { success: true, output: new TextDecoder('utf-8', { fatal: true }).decode(bytes), encoding: 'text' }
|
|
885
|
+
} catch {
|
|
886
|
+
if (encoding === 'text') throw new Error('content is not valid UTF-8 text; retry with encoding "base64"')
|
|
887
|
+
return {
|
|
888
|
+
success: true,
|
|
889
|
+
output: `Binary content (${bytes.byteLength} bytes), base64: ${bytesToBase64(bytes)}`,
|
|
890
|
+
encoding: 'base64',
|
|
891
|
+
}
|
|
892
|
+
}
|
|
808
893
|
} catch (err) {
|
|
809
894
|
return { success: false, output: '', error: err.message }
|
|
810
895
|
}
|