Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
fix docs, add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
nichoth committed Mar 16, 2024
1 parent 6d6edc6 commit 109fcbc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
37 changes: 29 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,47 @@ test('toString', t => {
t.equal(typeof myString, 'string', 'should conver a Uint8Array to string')
})
```

### writeKeyToDid
Convert a given public key to a DID string.

```ts
async function writeKeyToDid (crypto:Crypto.Implementation):Promise<DID>
```

#### example
```js
import { writeKeyToDid } from '@ssc-half-light/util'
test('writeKeyToDid', async t => {
const crypto = program.components.crypto
const did = await writeKeyToDid(crypto)
console.log('**did**', did)
t.equal(typeof did, 'string', 'should create string')
t.ok(did.includes('did:key:'), 'should return the right format string')
})
```

### didToPublicKey
Convert a DID string to a `Uint8Array` and `type` string.

```ts
(did:string):({
function didToPublicKey (did:string):({
publicKey:Uint8Array,
type: 'rsa' | 'ed25519' | 'bls12-381'
})
```

#### example &mdash; writeKeyToDid
#### example &mdash; didToPublicKey
Get the DID version of the key used to sign messages.

```js
test('writeKeyToDid', async t => {
const crypto = program.components.crypto
const did = await writeKeyToDid(crypto)
t.equal(typeof did, 'string', 'should create a string')
t.ok(did.includes('did:key:'), 'should return the right format string')
test('didToPublicKey', t => {
const did = 'did:key:z13...'
const pubKey = didToPublicKey(did)
t.equal(pubKey.type, 'rsa', 'should return the right key type')
t.ok(pubKey.publicKey instanceof Uint8Array,
'should return the public key as a Uint8Array')
})
```

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function publicKeyToDid (
return (BASE58_DID_PREFIX + arrToString(prefixedBuf, 'base58btc')) as DID
}

export async function writeKeyToDid (crypto: Crypto.Implementation)
export async function writeKeyToDid (crypto:Crypto.Implementation)
:Promise<DID> {
const [pubKey, ksAlg] = await Promise.all([
crypto.keystore.publicWriteKey(),
Expand Down
18 changes: 17 additions & 1 deletion test/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { test } from '@nichoth/tapzero'
import * as odd from '@oddjs/odd'
import { components, createCryptoComponent } from '@ssc-hermes/node-components'
import { verify, writeKeyToDid, toString, sign } from '../dist/index.js'
import {
verify,
writeKeyToDid,
toString,
sign,
didToPublicKey
} from '../dist/index.js'

// root DID
const did = 'did:key:z13V3Sog2YaUKhdGCmgx9UZuW1o1ShFJYc6DvGYe7NTt689NoL2htQdMxpcGJ3C7aZxdwvAzVjiib8MGB5R4vVFYtQJe1k5YfgxHnhAy2AxtG9CCfDMioGExvWNQREeBt6kwZweRCm4D2c6UmAvosCpf48EcdVATJKdQiwW1Swp9Vo5rkbPCTYWHvSpwgw8N9WntcfrPNRF7xDvGFmQ1ZiZkvZw1E4sVUMvhoaLbnHoRSB8NLrdW1mXjkVCyeA3a72x76sXhXtvbQ63noGth8Rke8tGCfXs9Skha81F9UFZz3gmJZTrgFTfCJrcMF2b6AsHZtWgLGnsXcB3hj7pxRy8APSCeq4AYfzCexkrkVdctmfQkrMSDd5WmGEeF8KKzkoNaHZhcgHd8VcYFXnuoKe8'
Expand Down Expand Up @@ -54,6 +60,16 @@ test('toString', t => {
test('writeKeyToDid', async t => {
const crypto = program.components.crypto
const did = await writeKeyToDid(crypto)
console.log('**did**', did)
t.equal(typeof did, 'string', 'should create string')
t.ok(did.includes('did:key:'), 'should return the right format string')
})

test('didToPublicKey', t => {
const did = 'did:key:z13V3Sog2YaUKhdGCmgx9UZuW1o1ShFJYc6DvGYe7NTt689NoL3d4K4nWAnkvpsYjc6cvvHJ2ae5UKju7b2AjpkquiL6ZTFbVAcBmxcfFUKFnhyqYobQ6S6CJpa5WotSWdANS26wcMiDjBU3PVq3EfBmVd1mUHFctDtBoU4tnenVgVZn7NLAxXELix4S9V9sfrmqhNtNe3fhqXYjJg635imLTnhH3swxStauB3AngmGYiNzSUv3aZCWxJkTyz53zfhgAfQTV7D59iv1jEdwYySFm7gVNYrXZXSCgcCPFzpCKbupHCNuQBBh2fbZCBViDigsCmR1r6r3obAvCHsauwonEtoTJPTBVRJ7Ghzw4nto7Rr3Jo7VGE5BLTyfF8G9pE2TV9sfDsz9cWA1gudc1hmz'
const pubKey = didToPublicKey(did)
t.ok(pubKey, 'should return')
t.equal(pubKey.type, 'rsa', 'should return the right key type')
t.ok(pubKey.publicKey instanceof Uint8Array,
'should return the public key as a Uint8Array')
})

0 comments on commit 109fcbc

Please sign in to comment.