Full Example

To demonstrate the usage of the library, you need to install sigpair-sdk2 and buffer. In this context, the react-native-tss library represents Party 1, while sigpair-sdk2 represents Party 2.

Install sigpair-sdk2

The .npmrc the file needs to be configured as described in Configure npm to use the GitLab registry

Install package sigpair-sdk2

With yarn

export GITLAB_AUTH_TOKEN=<You company access token> yarn add @com.silencelaboratories/sigpair-sdk2

Or using npm

export GITLAB_AUTH_TOKEN=<You company access token> npm install @com.silencelaboratories/sigpair-sdk2

Install package buffer

yarn add buffer

Or using npm

npm install buffer
import {
  KeyshareP1,
  P1PartyKeys,
  P1Tss,
} from '@com.silencelaboratories/react-native-tss';
import { Client } from '@com.silencelaboratories/sigpair-sdk2';
import * as React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Buffer } from 'buffer';

global.Buffer = Buffer;

export default function App() {
  React.useEffect(() => {
    // Example usage
    const adminToken = 'ADMIN_TOKEN';
    const apiUrl = 'API_URL';
    const userId = 'USER_ID_IN_NUMBER';
    const msgHash = '4qFZ0Xt7txSu12ddfH05TeyNLkM3hChIEEaUv4nHHAM=';

    const base64toUint8Array = (base64: string) => {
      return Buffer.from(base64, 'base64');
    };

    const fullTest = async () => {
      try {
        const s = new Date();
        const p1Keys = await P1PartyKeys.create();

        const tssClient = new Client(apiUrl);

        const token = await tssClient.userToken(adminToken, userId);
        const pk = base64toUint8Array(await p1Keys.extractPk());
        console.log('pk:', pk);

        const byteP1 = await p1Keys.toBytes();
        const p1KeysFromBytes = await P1PartyKeys.fromBytes(byteP1);

        const bytePk = base64toUint8Array(await p1KeysFromBytes.extractPk());
        console.log(
          'bytespk === pk: ',
          Buffer.from(bytePk).toString('base64') ===
            Buffer.from(pk).toString('base64')
        );

        const keySessionId = await tssClient.createKeygenSession(pk, token);

        const keyshare = await P1Tss.keygen(keySessionId, p1Keys, apiUrl);
        const keyId = await keyshare.getKeyId();

        const keyshareB64 = await keyshare.toBase64String();
        const keyshareByte = await keyshare.toBytes();
        const loadedKeyshare = await KeyshareP1.fromBase64String(keyshareB64);
        const loadedKeyshareKeyId = await loadedKeyshare.getKeyId();
        console.log(
          'loadedKeyshareKeyId === keyId: ',
          loadedKeyshareKeyId === keyId
        );

        const loadedKeyshareByte = await KeyshareP1.fromBytes(keyshareByte);
        const loadedKeyshareByteId = await loadedKeyshareByte.getKeyId();
        console.log(
          'loadedKeyshareByteId === keyId: ',
          loadedKeyshareByteId === keyId
        );

        const keysharePK = await keyshare.getPublicKey();
        console.log('keysharePK: ', keysharePK);

        const signSessionId = await tssClient.createSignSession(
          pk,
          token,
          keyId,
          Buffer.from(msgHash, 'base64').toString('hex'),
          'm'
        );
        console.log('signSessionId:', signSessionId);

        const sign = await P1Tss.signParty(
          signSessionId,
          keyshare,
          p1Keys,
          msgHash,
          'm',
          apiUrl
        );
        console.log('sign:', sign);

        const refreshSession = await tssClient.createRefreshSession(
          pk,
          token,
          keyId
        );
        console.log('refreshSession:', refreshSession);

        const newKeyshare = await P1Tss.refreshKeygen(
          refreshSession,
          p1Keys,
          keyshare,
          apiUrl
        );
        const newKeyshareB64 = await await keyshare.toBase64String();

        const newKeyId = await newKeyshare.getKeyId();
        console.log('newKeyId: ', newKeyId);

        const newSignSessionId = await tssClient.createSignSession(
          pk,
          token,
          newKeyId,
          Buffer.from(msgHash, 'base64').toString('hex'),
          'm'
        );
        console.log('newSignSessionId:', newSignSessionId);

        const newSign = await P1Tss.signParty(
          newSignSessionId,
          newKeyshare,
          p1Keys,
          msgHash,
          'm',
          apiUrl
        );
        console.log('newSign:', newSign);

        console.log(
          'newKeyshareB64 === keyshareB64: ',
          newKeyshareB64 === keyshareB64
        );
        console.log('newKeyId === keyId: ', newKeyId === keyId);

        console.log('Test success!');
        const e = new Date();
        console.log('time:', e.getTime() - s.getTime());
      } catch (e) {
        console.log(e);
      }
    };

    fullTest();
  }, []);

  return (
    <View style={styles.container}>
      <Text>RN-TSS example in useEffect function</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  box: {
    width: 60,
    height: 60,
    marginVertical: 20,
  },
});

Last updated