decentralized-identity / veramo

A JavaScript Framework for Verifiable Data
https://veramo.io
Apache License 2.0
415 stars 129 forks source link

SQLError in react native basic setup with expo-sqlite driver #1240

Closed cre8 closed 7 months ago

cre8 commented 10 months ago

Bug severity 5

Describe the bug Following the tutorial I am getting an sql error:

SQLError {code: 5, message: 'could not prepare statement (23 not authorized)'}

To Reproduce Steps to reproduce the behaviour: Follow the tutorial here https://veramo.io/docs/react_native_tutorials/react_native_1_setup_identifiers and run the project in the web

Observed behaviour The error is thrown when the agent tries to connect with the database (either for the reading of identifiers and the writing)

Expected behaviour A successful connection.

Details It seems that the problem is the dbConnection is the problem. Running

// DB setup:
export let dbConnection = new DataSource({
  type: 'expo',
  driver: require('expo-sqlite'),
  database: 'veramo.sqlite',
  migrations: migrations,
  migrationsRun: true,
  logging: ['error', 'info', 'warn'],
  entities: Entities,
}).initialize().catch((err) => console.log(err));

Digging deeper it seems that the driver is not supported for the web, see https://docs.expo.dev/versions/latest/sdk/sqlite/

Therefore the tutorial should:

Versions (please complete the following information):

cre8 commented 10 months ago

I think the third option sounds the best, looking into the agent explorer the json storage seems to be the best option like:

let dataStoreConst: IAgentPlugin;
let keyStore: AbstractKeyStore;
let privateKeyStore: AbstractPrivateKeyStore;
let didStore: AbstractDIDStore;

if (Platform.OS === 'web') {
  const dataStore = BrowserLocalStorageStore.fromLocalStorage('veramo-state');
  const identifierDataStore =
    BrowserLocalStorageStore.fromLocalStorage('veramo-id-state');
  dataStoreConst = new DataStoreJson(dataStore);
  keyStore = new KeyStoreJson(dataStore);
  privateKeyStore = new PrivateKeyStoreJson(dataStore);
  didStore = new DIDStoreJson(identifierDataStore);
} else {
  // DB setup:
  const dbConnection = new DataSource({
    type: 'sqlite',
    driver: require('expo-sqlite'),
    database: 'veramo.sqlite',
    migrations: migrations,
    migrationsRun: true,
    logging: ['error', 'info', 'warn'],
    entities: Entities,
  }).initialize();
  dataStoreConst = new DataStore(dbConnection);
  keyStore = new KeyStore(dbConnection);
  privateKeyStore = new PrivateKeyStore(dbConnection);
  didStore = new DIDStore(dbConnection);
}
// Veramo agent setup
export const agent = createAgent<
  IDIDManager & IKeyManager & IDataStore & IDataStoreORM
>({
  plugins: [
    new KeyManager({
      store: keyStore,
      kms: {
        local: new KeyManagementSystem(privateKeyStore),
      },
    }),
    new DIDManager({
      store: didStore,
      defaultProvider: 'did:ethr:goerli',
      providers: {
        'did:ethr:goerli': new EthrDIDProvider({
          defaultKms: 'local',
          network: 'goerli',
          rpcUrl: 'https://goerli.infura.io/v3/' + INFURA_PROJECT_ID,
        }),
      },
    }),
    dataStoreConst,
    new DIDResolverPlugin({
        resolver: new Resolver({
          ...ethrDidResolver({ infuraProjectId: INFURA_PROJECT_ID }),
          ...webDidResolver(),
        }),
      }),
      new CredentialPlugin(),
  ],
});
mirceanis commented 10 months ago

Indeed we did not consider react-native-web when writing the react-native tutorial.

If you want to propose changes to the tutorial, you will find it in the veramo-website repo, or click the "Edit this page" button when browsing veramo.io

We appreciate all contributions

stale[bot] commented 8 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.