Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
eike-hass committed Aug 22, 2022
1 parent e8a47ff commit edcffbe
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 155 deletions.
76 changes: 46 additions & 30 deletions bindings/wasm/docs/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,8 @@ See <code>IVerifierOptions</code>.</p>
## Members

<dl>
<dt><a href="#MethodRelationship">MethodRelationship</a></dt>
<dt><a href="#StateMetadataEncoding">StateMetadataEncoding</a></dt>
<dd></dd>
<dt><a href="#DIDType">DIDType</a></dt>
<dd><p>Supported types representing a DID that can be generated by the storage interface.</p>
</dd>
<dt><a href="#StatusCheck">StatusCheck</a></dt>
<dd><p>Controls validation behaviour when checking whether or not a credential has been revoked by its
<a href="https://www.w3.org/TR/vc-data-model/#status"><code>credentialStatus</code></a>.</p>
Expand Down Expand Up @@ -226,11 +223,14 @@ This variant is the default used if no other variant is specified when construct
<dt><a href="#FirstError">FirstError</a></dt>
<dd><p>Return after the first error occurs.</p>
</dd>
<dt><a href="#DIDType">DIDType</a></dt>
<dd><p>Supported types representing a DID that can be generated by the storage interface.</p>
</dd>
<dt><a href="#KeyType">KeyType</a></dt>
<dd></dd>
<dt><a href="#DIDMessageEncoding">DIDMessageEncoding</a></dt>
<dt><a href="#MethodRelationship">MethodRelationship</a></dt>
<dd></dd>
<dt><a href="#StateMetadataEncoding">StateMetadataEncoding</a></dt>
<dt><a href="#DIDMessageEncoding">DIDMessageEncoding</a></dt>
<dd></dd>
</dl>

Expand Down Expand Up @@ -274,10 +274,10 @@ publishing to the Tangle.
* [.unrevokeCredentials(fragment, indices)](#Account+unrevokeCredentials) ⇒ <code>Promise.&lt;void&gt;</code>
* [.encryptData(plaintext, associated_data, encryption_algorithm, cek_algorithm, public_key)](#Account+encryptData)[<code>Promise.&lt;EncryptedData&gt;</code>](#EncryptedData)
* [.decryptData(data, encryption_algorithm, cek_algorithm, fragment)](#Account+decryptData) ⇒ <code>Promise.&lt;Uint8Array&gt;</code>
* [.deleteMethod(options)](#Account+deleteMethod) ⇒ <code>Promise.&lt;void&gt;</code>
* [.deleteService(options)](#Account+deleteService) ⇒ <code>Promise.&lt;void&gt;</code>
* [.setAlsoKnownAs(options)](#Account+setAlsoKnownAs) ⇒ <code>Promise.&lt;void&gt;</code>
* [.setController(options)](#Account+setController) ⇒ <code>Promise.&lt;void&gt;</code>
* [.deleteMethod(options)](#Account+deleteMethod) ⇒ <code>Promise.&lt;void&gt;</code>

<a name="Account+createService"></a>

Expand Down Expand Up @@ -517,17 +517,6 @@ Returns the decrypted text.
| cek_algorithm | [<code>CekAlgorithm</code>](#CekAlgorithm) |
| fragment | <code>string</code> |

<a name="Account+deleteMethod"></a>

### account.deleteMethod(options) ⇒ <code>Promise.&lt;void&gt;</code>
Deletes a verification method if the method exists.

**Kind**: instance method of [<code>Account</code>](#Account)

| Param | Type |
| --- | --- |
| options | <code>DeleteMethodOptions</code> |

<a name="Account+deleteService"></a>

### account.deleteService(options) ⇒ <code>Promise.&lt;void&gt;</code>
Expand Down Expand Up @@ -561,6 +550,17 @@ Sets the controllers of the DID document.
| --- | --- |
| options | <code>SetControllerOptions</code> |

<a name="Account+deleteMethod"></a>

### account.deleteMethod(options) ⇒ <code>Promise.&lt;void&gt;</code>
Deletes a verification method if the method exists.

**Kind**: instance method of [<code>Account</code>](#Account)

| Param | Type |
| --- | --- |
| options | <code>DeleteMethodOptions</code> |

<a name="AccountBuilder"></a>

## AccountBuilder
Expand Down Expand Up @@ -5127,6 +5127,7 @@ Deserializes an instance from a JSON object.
* _static_
* [.newWithId(id)](#StardustDocument.newWithId)[<code>StardustDocument</code>](#StardustDocument)
* [.unpack(did, stateMetadata, allowEmpty)](#StardustDocument.unpack)[<code>StardustDocument</code>](#StardustDocument)
* [.unpackFromBlock(network, block)](#StardustDocument.unpackFromBlock)[<code>Array.&lt;StardustDocument&gt;</code>](#StardustDocument)
* [.fromJSON(json)](#StardustDocument.fromJSON)[<code>StardustDocument</code>](#StardustDocument)

<a name="new_StardustDocument_new"></a>
Expand Down Expand Up @@ -5527,6 +5528,21 @@ encoded in the `AliasId` alone.
| stateMetadata | <code>Uint8Array</code> |
| allowEmpty | <code>boolean</code> |

<a name="StardustDocument.unpackFromBlock"></a>

### StardustDocument.unpackFromBlock(network, block) ⇒ [<code>Array.&lt;StardustDocument&gt;</code>](#StardustDocument)
Returns all DID documents of the Alias Outputs contained in the block's transaction payload
outputs, if any.

Errors if any Alias Output does not contain a valid or empty DID Document.

**Kind**: static method of [<code>StardustDocument</code>](#StardustDocument)

| Param | Type |
| --- | --- |
| network | <code>string</code> |
| block | <code>IBlock</code> |

<a name="StardustDocument.fromJSON"></a>

### StardustDocument.fromJSON(json) ⇒ [<code>StardustDocument</code>](#StardustDocument)
Expand Down Expand Up @@ -6263,15 +6279,9 @@ This is possible because Ed25519 is birationally equivalent to Curve25519 used b
| --- | --- |
| publicKey | <code>Uint8Array</code> |

<a name="MethodRelationship"></a>

## MethodRelationship
**Kind**: global variable
<a name="DIDType"></a>

## DIDType
Supported types representing a DID that can be generated by the storage interface.
<a name="StateMetadataEncoding"></a>

## StateMetadataEncoding
**Kind**: global variable
<a name="StatusCheck"></a>

Expand Down Expand Up @@ -6350,18 +6360,24 @@ Return all errors that occur during validation.
## FirstError
Return after the first error occurs.

**Kind**: global variable
<a name="DIDType"></a>

## DIDType
Supported types representing a DID that can be generated by the storage interface.

**Kind**: global variable
<a name="KeyType"></a>

## KeyType
**Kind**: global variable
<a name="DIDMessageEncoding"></a>
<a name="MethodRelationship"></a>

## DIDMessageEncoding
## MethodRelationship
**Kind**: global variable
<a name="StateMetadataEncoding"></a>
<a name="DIDMessageEncoding"></a>

## StateMetadataEncoding
## DIDMessageEncoding
**Kind**: global variable
<a name="start"></a>

Expand Down
48 changes: 43 additions & 5 deletions bindings/wasm/examples-stardust/src/ex0_create_did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import {
StardustIdentityClient,
StardustVerificationMethod
} from '../../node';

import {Bech32Helper, IAliasOutput,} from '@iota/iota.js';
import {Bech32Helper, IAliasOutput} from '@iota/iota.js';
import {Bip39} from "@iota/crypto.js";
import fetch from "node-fetch";
import {Client, MnemonicSecretManager, SecretManager} from "@cycraig/iota-client-wasm/node";
Expand Down Expand Up @@ -49,8 +48,8 @@ export async function createIdentity(): Promise<{
}))[0];
console.log("Wallet address Bech32:", walletAddressBech32);

// Request funds for the newly-created wallet - only works on development networks.
await requestFundsFromFaucet(walletAddressBech32);
// Request funds for the wallet, if needed - only works on development networks.
await ensureAddressHasFunds(client, walletAddressBech32);

// Create a new DID document with a placeholder DID.
// The DID will be derived from the Alias Id of the Alias Output after publishing.
Expand Down Expand Up @@ -78,7 +77,46 @@ export async function createIdentity(): Promise<{
};
}

/** Request tokens from the faucet API. */
/** Request funds from the testnet faucet API, if needed, and wait for them to show in the wallet. */
async function ensureAddressHasFunds(client: Client, addressBech32: string) {
let balance = await getAddressBalance(client, addressBech32);
if (balance > 0) {
return;
}

await requestFundsFromFaucet(addressBech32);

for (let i = 0; i < 9; i++) {
// Wait for the funds to reflect.
await new Promise(f => setTimeout(f, 5000));

let balance = await getAddressBalance(client, addressBech32);
if (balance > 0) {
break;
}
}
}

/** Returns the balance of the given Bech32-encoded address. */
async function getAddressBalance(client: Client, addressBech32: string): Promise<number> {
// TODO: use the `addresses/ed25519/<addressHex>` API to get the balance?
const outputIds = await client.basicOutputIds([
{address: addressBech32},
{hasExpiration: false},
{hasTimelock: false},
{hasStorageDepositReturn: false}
]);
const outputs = await client.getOutputs(outputIds);

let totalAmount = 0;
for (const output of outputs) {
totalAmount += Number(output.output.amount);
}

return totalAmount;
}

/** Request tokens from the testnet faucet API. */
async function requestFundsFromFaucet(addressBech32: string) {
const requestObj = JSON.stringify({address: addressBech32});
let errorMessage, data;
Expand Down
2 changes: 1 addition & 1 deletion bindings/wasm/examples-stardust/src/ex1_update_did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import {MethodRelationship, StardustDocument, StardustService, Timestamp} from '../../node';
import {IAliasOutput, IRent, TransactionHelper,} from '@iota/iota.js';
import {IAliasOutput, IRent, TransactionHelper} from '@iota/iota.js';

import {createIdentity} from "./ex0_create_did";

Expand Down
3 changes: 0 additions & 3 deletions bindings/wasm/examples-stardust/src/ex4_delete_did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ export async function deleteIdentity() {
const destinationAddress = Bech32Helper.addressFromBech32(walletAddressBech32, await didClient.getNetworkHrp());
await didClient.deleteDidOutput(secretManager, destinationAddress, did);

// Wait for the node to index the new state.
await new Promise(f => setTimeout(f, 5000));

// Attempting to resolve a deleted DID results in a `NotFound` error.
let deleted = false;
try {
Expand Down
65 changes: 1 addition & 64 deletions bindings/wasm/lib/stardust_identity_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,11 @@ import {
AddressTypes,
ALIAS_OUTPUT_TYPE,
IAliasOutput,
IBlock,
IOutputResponse,
IRent,
ITransactionPayload,
IUTXOInput,
OutputTypes,
TRANSACTION_PAYLOAD_TYPE,
TransactionHelper
} from '@iota/iota.js';
import {Converter} from '@iota/util.js';
import {Blake2b} from '@iota/crypto.js';

/** Provides operations for IOTA UTXO DID Documents with Alias Outputs. */
export class StardustIdentityClient implements IStardustIdentityClient {
Expand Down Expand Up @@ -120,7 +114,7 @@ export class StardustIdentityClient implements IStardustIdentityClient {
await this.client.retryUntilIncluded(blockId);

// Extract document with computed AliasId.
const documents = extractDocumentsFromBlock(networkHrp, block);
const documents = StardustDocument.unpackFromBlock(networkHrp, block);
if (documents.length < 1) {
throw new Error("publishDidOutput: no DID document in transaction payload");
}
Expand Down Expand Up @@ -166,60 +160,3 @@ export class StardustIdentityClient implements IStardustIdentityClient {
await this.client.retryUntilIncluded(blockId);
}
}

/** Compute the AliasId as a prefix-hex encoded string. */
function computeAliasId(transactionIdHex: string, outputIndex: number): string {
const outputIdHex: string = TransactionHelper.outputIdFromTransactionData(transactionIdHex, outputIndex);
return computeAliasIdFromOutputId(outputIdHex);
}

/** Compute the AliasId as a prefix-hex encoded string. */
function computeAliasIdFromOutputId(outputIdHex: string): string {
// Blake2b-256 digest of output id.
const outputIdBytes: Uint8Array = Converter.hexToBytes(outputIdHex);
const digest: Uint8Array = Blake2b.sum256(outputIdBytes);
return Converter.bytesToHex(digest, true);
}

/** Extract all DID documents of the Alias Outputs contained in a transaction payload, if any. */
function extractDocumentsFromBlock(networkHrp: string, block: IBlock): StardustDocument[] {
const documents: StardustDocument[] = [];

if (block.payload === undefined || block.payload?.type !== TRANSACTION_PAYLOAD_TYPE) {
throw new Error("failed to extract documents from block, transaction payload missing or wrong type");
}
const payload: ITransactionPayload = block.payload;

// Compute TransactionId.
const transactionPayloadHash: Uint8Array = TransactionHelper.getTransactionPayloadHash(payload);
const transactionId: string = Converter.bytesToHex(transactionPayloadHash, true);

// Loop over Alias Outputs.
const outputs: OutputTypes[] = payload.essence.outputs;
for (let index = 0; index < outputs.length; index += 1) {
const output = outputs[index];
if (output.type !== ALIAS_OUTPUT_TYPE) {
continue;
}

// Compute Alias Id.
let aliasIdHex: string;
if (output.stateIndex === 0) {
aliasIdHex = computeAliasId(transactionId, index);
} else {
aliasIdHex = output.aliasId;
}
const aliasId: Uint8Array = Converter.hexToBytes(aliasIdHex);

// Unpack document.
const did: StardustDID = new StardustDID(aliasId, networkHrp);
let stateMetadata: Uint8Array;
if (output.stateMetadata === undefined) {
stateMetadata = new Uint8Array(0);
} else {
stateMetadata = Converter.hexToBytes(output.stateMetadata);
}
documents.push(StardustDocument.unpack(did, stateMetadata, true));
}
return documents;
}
13 changes: 3 additions & 10 deletions bindings/wasm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit edcffbe

Please sign in to comment.