This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/put-did-into-class-context' into develop
- Loading branch information
Showing
7 changed files
with
150 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { InvalidDIDError, InvalidDIDTypeError } from './error'; | ||
|
||
export class DecentralizedID { | ||
public static fromAddress(address: string): DecentralizedID { | ||
return new DecentralizedID('btc-addr', address); | ||
} | ||
|
||
public static fromPublicKey(publicKey: string): DecentralizedID { | ||
return new DecentralizedID('ecdsa-pub', publicKey); | ||
} | ||
|
||
public static fromString(str: string): DecentralizedID { | ||
const didParts = str.split(':'); | ||
|
||
if (didParts.length !== 3) { | ||
throw new InvalidDIDError(str, 'Decentralized IDs must have 3 parts'); | ||
} | ||
|
||
if (didParts[0].toLowerCase() !== 'did') { | ||
throw new InvalidDIDError(str, 'Decentralized IDs must start with "did"'); | ||
} | ||
|
||
const type = didParts[1]; | ||
const identifier = didParts[2]; | ||
return new DecentralizedID(type, identifier); | ||
} | ||
|
||
private readonly type: string; | ||
private readonly identifier: string; | ||
|
||
public constructor(type: string, identifier: string) { | ||
this.type = type; | ||
this.identifier = identifier; | ||
} | ||
|
||
public getAddress() { | ||
if(this.type == 'btc-addr') { | ||
return this.identifier; | ||
} else { | ||
throw new InvalidDIDTypeError(this.type, 'btc-addr'); | ||
} | ||
} | ||
|
||
public getType(): string { | ||
return this.type; | ||
} | ||
|
||
public toString(): string { | ||
return `did:${this.type}:${this.identifier}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export class InvalidDIDTypeError extends Error { | ||
public readonly name: string = 'InvalidDIDTypeError'; | ||
public readonly message: string; | ||
public readonly actualType: string; | ||
public readonly expectedType: string; | ||
|
||
constructor(actualType: string, expectedType: string) { | ||
super(); | ||
|
||
this.message = `The given DID type "${actualType}" did not match the expected "${expectedType}"`; | ||
this.actualType = actualType; | ||
this.expectedType = expectedType; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import * as chai from 'chai'; | ||
|
||
import { correct, incorrect } from '../fun'; | ||
|
||
import { DecentralizedID } from '../../src/DecentralizedID'; | ||
import { InvalidDIDError, InvalidDIDTypeError } from '../../src/error'; | ||
|
||
describe('DecentralizedID', () => { | ||
const didWithBtcAddr = new DecentralizedID('btc-addr', '1111111111111111111114oLvT2'); | ||
const didWithPubKey = new DecentralizedID('ecdsa-pub', '000000000000000000000000000000000000000000000000000000000000000000') | ||
|
||
const btcAddr = '1111111111111111111114oLvT2'; | ||
const pubKey = '000000000000000000000000000000000000000000000000000000000000000000'; | ||
|
||
const didStringWithBtcAddr = 'did:btc-addr:1111111111111111111114oLvT2'; | ||
const didStringWithPubKey = 'did:ecdsa-pub:000000000000000000000000000000000000000000000000000000000000000000'; | ||
|
||
describe('fromAddress', () => { | ||
it(`creates ${correct()} DID objects from an address`, () => { | ||
const did = DecentralizedID.fromAddress('1111111111111111111114oLvT2'); | ||
|
||
chai.expect(did).to.deep.equal(didWithBtcAddr); | ||
}); | ||
}); | ||
|
||
describe('fromPublicKey', () => { | ||
it(`creates ${correct()} DID Objects from a public key`, () => { | ||
const did = DecentralizedID.fromPublicKey('000000000000000000000000000000000000000000000000000000000000000000'); | ||
|
||
chai.expect(did).to.deep.equal(didWithPubKey); | ||
}); | ||
}); | ||
|
||
describe('fromString', () => { | ||
it(`creates ${correct()} DID objects from a string`, () => { | ||
chai.expect(DecentralizedID.fromString(didStringWithBtcAddr)).to.deep.equal(didWithBtcAddr); | ||
chai.expect(DecentralizedID.fromString(didStringWithPubKey)).to.deep.equal(didWithPubKey); | ||
}); | ||
|
||
it(`fails on ${incorrect()} inputs`, () => { | ||
chai.expect(() => DecentralizedID.fromString('did:1:2:3')).to.throw(InvalidDIDError); | ||
chai.expect(() => DecentralizedID.fromString('did:1')).to.throw(InvalidDIDError); | ||
chai.expect(() => DecentralizedID.fromString('')).to.throw(InvalidDIDError); | ||
chai.expect(() => DecentralizedID.fromString('no-did:1:2')).to.throw(InvalidDIDError); | ||
}); | ||
}); | ||
|
||
describe('constructor', () => { | ||
it(`creates ${correct()} DID objects`, () => { | ||
chai.expect(new DecentralizedID('btc-addr', btcAddr)).to.deep.equal(didWithBtcAddr); | ||
chai.expect(new DecentralizedID('ecdsa-pub', pubKey)).to.deep.equal(didWithPubKey); | ||
}); | ||
}); | ||
|
||
describe('getAddress', () => { | ||
it(`retrieves the ${correct()} address of a DID object`, () => { | ||
chai.expect(didWithBtcAddr.getAddress()).to.equal(btcAddr); | ||
}); | ||
|
||
it(`fails on ${incorrect()} inputs`, () => { | ||
chai.expect(() => didWithPubKey.getAddress()).to.throw(InvalidDIDTypeError); | ||
}); | ||
}); | ||
|
||
describe('getType', () => { | ||
it(`retrieves the ${correct()} type of a DID object`, () => { | ||
chai.expect(didWithBtcAddr.getType()).to.equal('btc-addr'); | ||
chai.expect(didWithPubKey.getType()).to.equal('ecdsa-pub'); | ||
}); | ||
}); | ||
|
||
describe('toString', () => { | ||
it(`creates ${correct()} DID strings from DID objects`, () => { | ||
chai.expect(didWithBtcAddr.toString()).to.equal(didStringWithBtcAddr); | ||
chai.expect(didWithPubKey.toString()).to.equal(didStringWithPubKey); | ||
}); | ||
}); | ||
}); |