spruceid / wallet

A reference credential wallet built on Flutter and DIDKit.
https://spruceid.dev/docs/credible
Apache License 2.0
45 stars 24 forks source link

can't finish presentation with defaultDIDMethod = 'key' #91

Open hawkbee1 opened 2 years ago

hawkbee1 commented 2 years ago

When trying a DIDAuth presentation (https://talao.co/wallet/test/presentationRequest), with key as default method, I have an error. The error is happening in didkit.dart:

final vm = key_to_verification_method(
        methodPattern.toNativeUtf8(), key.toNativeUtf8());

vm equal Pointer: address=0x0 methodePattern = "key" key = {"kty":"OKP","crv":"Ed25519","d":"skMDj_TuYaq1ZB-4ZzntpN-7CQjOqKl69Zs5-8gPO4w=","x":"AMKCOePOz8RFfSLFOqi5Rtf4TvG6EOxPX3T6TWhSgNLi"}

bumblefudge commented 2 years ago

Hey sorry, I think we missed this! Could you maybe paste in the error here so we can understand better which of the three functions/methods is panicking?

NB @clehner

hawkbee1 commented 2 years ago

Sorry @bumblefudge , I should have updated the issue. In fact the key generation was the root cause and we changed it. Here's our new key generation code:

import 'dart:convert';
import 'package:bip39/bip39.dart' as bip39;
import 'package:bip32/bip32.dart' as bip32;
import 'package:hex/hex.dart';
import 'package:secp256k1/secp256k1.dart';

class KeyGeneration {
  static Future<String> privateKey(String mnemonic) async {
    final seed = bip39.mnemonicToSeed(mnemonic);
    var rootKey = bip32.BIP32.fromSeed(seed);
    // derive path for ethereum '60' see bip 44, first address
    final child = rootKey.derivePath("m/44'/60'/0'/0/0");
    Iterable<int> iterable = child.privateKey!;
    final epk = HEX.encode(List.from(iterable));
    print(epk);
    final pk = PrivateKey.fromHex(epk);
    final pub = pk.publicKey.toHex().substring(2);
    final ad = HEX.decode(epk);
    final d = base64Url.encode(ad).substring(0, 43); // remove "=" padding 43/44
    final mx = pub.substring(0, 64); // first 32 bytes
    final ax = HEX.decode(mx);
    final x = base64Url.encode(ax).substring(0, 43); // remove "=" padding 43/44
    final my = pub.substring(64); // last 32 bytes
    final ay = HEX.decode(my);
    final y = base64Url.encode(ay).substring(0, 43);
    // ATTENTION !!!!!
    // alg "ES256K-R" for did:ethr and did:tz2 "EcdsaSecp256k1RecoverySignature2020"
    // use alg "ES256K" for did:key
    final key = {
      'kty': 'EC',
      'crv': 'secp256k1',
      'd': d,
      'x': x,
      'y': y,
      'alg': 'ES256K-R' // or 'alg': "ES256K" for did:key
    };
    return jsonEncode(key);
  }
}