initWalletState method

  1. @override
Future<void> initWalletState(
  1. int networkId,
  2. int ledgerId,
  3. VerificationKey vk
)

Initialize the wallet state with the given verification key

networkId The network id to initialize the wallet state with ledgerId The ledger id to initialize the wallet state with vk The verification key to initialize the wallet state with

Implementation

@override
Future<void> initWalletState(
    int networkId, int ledgerId, m.VerificationKey vk) async {
  final defaultTemplate = PredicateTemplate(
    [SignatureTemplate("ExtendedEd25519", 0)],
    1,
  );

  final genesisTemplate = PredicateTemplate(
    [HeightTemplate('header', 1.toInt64, Int64.MAX_VALUE)],
    1,
  );

  // Create parties
  await fellowshipsStore.add(
      _instance, Fellowship(x: 0, name: 'nofellowship').toSembast);
  await fellowshipsStore.add(
      _instance, Fellowship(x: 1, name: 'self').toSembast);

  // Create templates
  await templatesStore.add(
      _instance,
      Template(
              y: 1,
              name: 'default',
              lock: jsonEncode(defaultTemplate.toJson()))
          .toSembast);
  await templatesStore.add(
      _instance,
      Template(
              y: 2,
              name: 'genesis',
              lock: jsonEncode(genesisTemplate.toJson()))
          .toSembast);

  // Create verification keys
  await verificationKeysStore.add(
      _instance,
      sk.VerificationKey(
        x: 1,
        y: 1,
        vks: [Encoding().encodeToBase58Check(vk.writeToBuffer())],
      ).toSembast); // TODO(ultimaterex): figure out if encoding to stringbase 58 is better
  await verificationKeysStore.add(
      _instance,
      sk.VerificationKey(
        x: 0,
        y: 2,
        vks: [],
      ).toSembast);

  final defaultSignatureLock = getLock("self", "default", 1)!; // unsafe
  final signatureLockAddress = m.LockAddress(
      network: networkId,
      ledger: ledgerId,
      id: m.LockId(
          value: defaultSignatureLock.predicate.sizedEvidence.digest.value));

  final childVk = api.deriveChildVerificationKey(vk, 1);
  final genesisHeightLock = getLock("nofellowship", "genesis", 1)!; // unsafe
  final heightLockAddress = m.LockAddress(
      network: networkId,
      ledger: ledgerId,
      id: m.LockId(
          value: genesisHeightLock.predicate.sizedEvidence.digest.value));

  // Create cartesian coordinates
  await cartesiansStore.add(
      _instance,
      Cartesian(
        x: 1,
        y: 1,
        z: 1,
        lockPredicate: Encoding().encodeToBase58Check(
            defaultSignatureLock.predicate.writeToBuffer()),
        address: AddressCodecs.encode(signatureLockAddress),
        routine: 'ExtendedEd25519',
        vk: Encoding().encodeToBase58Check(childVk.writeToBuffer()),
      ).toSembast);

  await cartesiansStore.add(
      _instance,
      Cartesian(
        x: 0,
        y: 2,
        z: 1,
        lockPredicate: Encoding()
            .encodeToBase58Check(genesisHeightLock.predicate.writeToBuffer()),
        address: AddressCodecs.encode(heightLockAddress),
      ).toSembast);
}