# Resolver

[**@twine-protocol/twine-core v0.1.0**](https://docs.twine.world/twine-js/twine-protocol/twine-core/index) • **Docs**

***

[twine-js](https://docs.twine.world/twine-js/index) / [@twine-protocol/twine-core](https://docs.twine.world/twine-js/twine-protocol/twine-core/index) / Resolver

## Interface: Resolver

Resolves a query into a chain or pulse

Resolvers are the primary way to fetch twines from somewhere. This specifies a general interface for all resolvers.

Resolvers are expected to verify the signature of the resolved twine.

### See

[Store](https://docs.twine.world/twine-js/twine-protocol/twine-core/interfaces/store)

### Example

```js
import { collect } from '@twine-protocol/twine-core'
const resolver = new MyResolver()
const chains = await collect(resolver.chains())
// resolve the latest pulse of the first chain
const resolution = await resolver.resolveLatest({ chain: chains[0] })
if (resolution.pulse) {
  // a verified pulse that is the latest this resolver knows of
  console.log('pulse', resolution.pulse)
}
```

### Extended by

* [`CombinedResolver`](https://docs.twine.world/twine-js/twine-protocol/twine-core/interfaces/combinedresolver)
* [`Store`](https://docs.twine.world/twine-js/twine-protocol/twine-core/interfaces/store)

### Methods

#### resolve()

**resolve(query, options)**

> **resolve**(`query`, `options`?): `Promise`<[`ChainResolution`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/chainresolution)>

Resolve a chain from a query

This is the main way to get a pulse or chain from somewhere and have it automatically verified.

If the input is already a successful resolution, it will be returned as is.

If the input is a chain or pulse, it will be resolved as a chain or pulse resolution.

**Parameters**

| Parameter  | Type                                                                                                                      | Description                |
| ---------- | ------------------------------------------------------------------------------------------------------------------------- | -------------------------- |
| `query`    | [`IntoResolveChainQuery`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/intoresolvechainquery) | The query to resolve       |
| `options`? | [`ResolveOptions`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/resolveoptions)               | Options for the resolution |

**Returns**

`Promise`<[`ChainResolution`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/chainresolution)>

A chain or pulse resolution

**Examples**

```js
const { chain } = await resolver.resolve({ chain: 'bafybeib3...' })
if (chain) {
  console.log('chain', chain)
} else {
  console.log('no chain')
}
```

```js
const { chain, pulse } = await resolver.resolve({
  chain: 'bafybeib3...',
  pulse: 'bafybeib3...'
})

if (pulse) {
  console.log('pulse', pulse)
} else {
  console.log('no pulse')
}
```

**Defined in**

[packages/twine-core/src/resolver/types.ts:233](https://github.com/twine-protocol/twine-js/blob/3800995f9c83f4f5711bcf3062ea754a1e4448ce/packages/twine-core/src/resolver/types.ts#L233)

**resolve(query, options)**

> **resolve**(`query`, `options`?): `Promise`<[`PulseResolution`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/pulseresolution)>

Resolve a pulse (with its chain) from a query

**Parameters**

| Parameter  | Type                                                                                                                      |
| ---------- | ------------------------------------------------------------------------------------------------------------------------- |
| `query`    | [`IntoResolvePulseQuery`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/intoresolvepulsequery) |
| `options`? | [`ResolveOptions`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/resolveoptions)               |

**Returns**

`Promise`<[`PulseResolution`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/pulseresolution)>

**Defined in**

[packages/twine-core/src/resolver/types.ts:237](https://github.com/twine-protocol/twine-js/blob/3800995f9c83f4f5711bcf3062ea754a1e4448ce/packages/twine-core/src/resolver/types.ts#L237)

***

#### resolveLatest()

> **resolveLatest**(`chain`, `options`?): `Promise`<[`PulseResolution`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/pulseresolution)>

Resolve the latest pulse of a chain

**Parameters**

| Parameter  | Type                                                                                                        | Description                                                    |
| ---------- | ----------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------- |
| `chain`    | [`IntoCid`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/intocid)               | The chain CID or chain itself to resolve the latest pulse from |
| `options`? | [`ResolveOptions`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/resolveoptions) | Options for the resolution                                     |

**Returns**

`Promise`<[`PulseResolution`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/pulseresolution)>

A pulse resolution

**Examples**

```js
const resolution = await resolver.resolveLatest('bafybeib3...')
if (resolution.pulse) {
  console.log('pulse', resolution.pulse)
}
```

```js
const { chain } = await resolver.resolve({ chain: 'bafybeib3...' })
const resolution = await resolver.resolveLatest(chain)
```

**Defined in**

[packages/twine-core/src/resolver/types.ts:260](https://github.com/twine-protocol/twine-js/blob/3800995f9c83f4f5711bcf3062ea754a1e4448ce/packages/twine-core/src/resolver/types.ts#L260)

***

#### resolveIndex()

> **resolveIndex**(`chain`, `index`, `options`?): `Promise`<[`PulseResolution`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/pulseresolution)>

Resolve a pulse by index

**Parameters**

| Parameter  | Type                                                                                                        | Description                                             |
| ---------- | ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------- |
| `chain`    | [`IntoCid`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/intocid)               | The chain CID or chain itself to resolve the pulse from |
| `index`    | `number`                                                                                                    | The index of the pulse to resolve                       |
| `options`? | [`ResolveOptions`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/resolveoptions) | Options for the resolution                              |

**Returns**

`Promise`<[`PulseResolution`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/pulseresolution)>

A pulse resolution

**Example**

```js
const resolution = await resolver.resolveIndex('bafybeib3...', 42)
if (resolution.pulse) {
  console.log('pulse', resolution.pulse)
}
```

**Defined in**

[packages/twine-core/src/resolver/types.ts:278](https://github.com/twine-protocol/twine-js/blob/3800995f9c83f4f5711bcf3062ea754a1e4448ce/packages/twine-core/src/resolver/types.ts#L278)

***

#### has()

> **has**(`cid`): [`Awaitable`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/awaitable)<`boolean`>

Check if a cid can be resolved

**Parameters**

| Parameter | Type                                                                                          | Description      |
| --------- | --------------------------------------------------------------------------------------------- | ---------------- |
| `cid`     | [`IntoCid`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/intocid) | The CID to check |

**Returns**

[`Awaitable`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/awaitable)<`boolean`>

True if the CID can be resolved, false otherwise

**Example**

```js
const exists = await resolver.has('bafybeib3...')
if (exists) {
  console.log('chain exists')
} else {
  console.log('chain does not exist')
}
```

**Defined in**

[packages/twine-core/src/resolver/types.ts:295](https://github.com/twine-protocol/twine-js/blob/3800995f9c83f4f5711bcf3062ea754a1e4448ce/packages/twine-core/src/resolver/types.ts#L295)

***

#### pulses()

> **pulses**(`chain`, `start`?, `options`?): `AsyncGenerator`<[`Pulse`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/pulse), `any`, `any`> | `Generator`<[`Pulse`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/pulse), `any`, `any`> | [`AnyIterable`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/anyiterable)<[`Pulse`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/pulse)>

Get the pulses of a chain

**Parameters**

| Parameter  | Type                                                                                                        | Description                                          |
| ---------- | ----------------------------------------------------------------------------------------------------------- | ---------------------------------------------------- |
| `chain`    | [`IntoCid`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/intocid)               | The chain CID or chain itself to get the pulses from |
| `start`?   | `number` \| [`IntoCid`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/intocid)   | The index or CID of the pulse to start from          |
| `options`? | [`ResolveOptions`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/resolveoptions) | Options for the resolution                           |

**Returns**

`AsyncGenerator`<[`Pulse`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/pulse), `any`, `any`> | `Generator`<[`Pulse`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/pulse), `any`, `any`> | [`AnyIterable`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/anyiterable)<[`Pulse`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/pulse)>

An sync/async iterable of pulses

**Example**

```js
// loop through pulses and print indices
for await (const pulse of resolver.pulses('bafybeib3...')) {
  console.log('pulse', pulse.value.content.index)
}
```

**Defined in**

[packages/twine-core/src/resolver/types.ts:313](https://github.com/twine-protocol/twine-js/blob/3800995f9c83f4f5711bcf3062ea754a1e4448ce/packages/twine-core/src/resolver/types.ts#L313)

***

#### chains()

> **chains**(): `AsyncGenerator`<[`Chain`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/chain), `any`, `any`> | `Generator`<[`Chain`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/chain), `any`, `any`> | [`AnyIterable`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/anyiterable)<[`Chain`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/chain)>

Get the chains that are known to the resolver

**Returns**

`AsyncGenerator`<[`Chain`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/chain), `any`, `any`> | `Generator`<[`Chain`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/chain), `any`, `any`> | [`AnyIterable`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/anyiterable)<[`Chain`](https://docs.twine.world/twine-js/twine-protocol/twine-core/type-aliases/chain)>

An sync/async iterable of chains

**Example**

```js
const chains = await collect(resolver.chains())
```

**Defined in**

[packages/twine-core/src/resolver/types.ts:324](https://github.com/twine-protocol/twine-js/blob/3800995f9c83f4f5711bcf3062ea754a1e4448ce/packages/twine-core/src/resolver/types.ts#L324)
