Rust and WASM did-method-plc tools and structures
at main 3.3 kB view raw
1/** 2 * Browser tests for atproto-plc WASM module 3 */ 4 5import { expect } from '@esm-bundle/chai'; 6import init, { 7 WasmDid, 8 WasmDidBuilder, 9 WasmSigningKey, 10 WasmServiceEndpoint 11} from '../../pkg/atproto_plc.js'; 12 13describe('atproto-plc WASM', () => { 14 before(async () => { 15 await init(); 16 }); 17 18 describe('DID validation', () => { 19 it('should validate a valid DID', () => { 20 const did = new WasmDid('did:plc:ewvi7nxzyoun6zhxrhs64oiz'); 21 expect(did.isValid()).to.be.true; 22 expect(did.identifier).to.equal('ewvi7nxzyoun6zhxrhs64oiz'); 23 expect(did.did).to.equal('did:plc:ewvi7nxzyoun6zhxrhs64oiz'); 24 }); 25 26 it('should reject an invalid DID', () => { 27 expect(() => new WasmDid('did:plc:invalid')).to.throw(); 28 expect(() => new WasmDid('did:web:example.com')).to.throw(); 29 }); 30 31 it('should handle toString', () => { 32 const did = new WasmDid('did:plc:ewvi7nxzyoun6zhxrhs64oiz'); 33 expect(did.toString()).to.equal('did:plc:ewvi7nxzyoun6zhxrhs64oiz'); 34 }); 35 }); 36 37 describe('Key generation', () => { 38 it('should generate P-256 keys', () => { 39 const key = WasmSigningKey.generateP256(); 40 const didKey = key.toDidKey(); 41 expect(didKey).to.match(/^did:key:z/); 42 }); 43 44 it('should generate secp256k1 keys', () => { 45 const key = WasmSigningKey.generateK256(); 46 const didKey = key.toDidKey(); 47 expect(didKey).to.match(/^did:key:z/); 48 }); 49 }); 50 51 describe('DID creation', () => { 52 it('should create a new DID', async () => { 53 const rotationKey = WasmSigningKey.generateP256(); 54 const signingKey = WasmSigningKey.generateK256(); 55 56 const builder = new WasmDidBuilder() 57 .addRotationKey(rotationKey) 58 .addVerificationMethod('atproto', signingKey); 59 60 const result = builder.build(); 61 62 expect(result.did).to.match(/^did:plc:[a-z2-7]{24}$/); 63 expect(result.operation).to.be.a('string'); 64 65 // Parse the operation JSON 66 const operation = JSON.parse(result.operation); 67 expect(operation.type).to.equal('plc_operation'); 68 }); 69 70 it('should create a DID with services', async () => { 71 const rotationKey = WasmSigningKey.generateP256(); 72 const service = new WasmServiceEndpoint( 73 'AtprotoPersonalDataServer', 74 'https://pds.example.com' 75 ); 76 77 const builder = new WasmDidBuilder() 78 .addRotationKey(rotationKey) 79 .addService('atproto_pds', service); 80 81 const result = builder.build(); 82 expect(result.did).to.match(/^did:plc:/); 83 }); 84 85 it('should create a DID with also-known-as', async () => { 86 const rotationKey = WasmSigningKey.generateP256(); 87 88 const builder = new WasmDidBuilder() 89 .addRotationKey(rotationKey) 90 .addAlsoKnownAs('at://alice.bsky.social'); 91 92 const result = builder.build(); 93 expect(result.did).to.match(/^did:plc:/); 94 }); 95 }); 96 97 describe('ServiceEndpoint', () => { 98 it('should create a service endpoint', () => { 99 const service = new WasmServiceEndpoint( 100 'AtprotoPersonalDataServer', 101 'https://pds.example.com' 102 ); 103 104 expect(service.serviceType).to.equal('AtprotoPersonalDataServer'); 105 expect(service.endpoint).to.equal('https://pds.example.com'); 106 }); 107 }); 108});