svanas / delphereum

web3 implementation for the Delphi programming language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Attempt to add the missing TSignature.Create(hex: string)

CryptoTangerine opened this issue · comments

I was happy to see ecrecover added, but found out the only way to use it is to have a TSignature already.

Though I tried to make a reverse method for the TSignature.ToHex, so that a signature can be deconstructed into R,S,V from the hex string, all I'm getting back is either 0x0 or "Numbers not relatively prime".

Please consider adding a new Constructor for this, that accepts the signature hex as input param. The currently available methods are only one side of the coin. At first, I was ambitious and wanted to make and commit the code myself, but the solution seems to be eluding me.

constructor TSignature.Create(hex: string);
begin
  delete(hex,1,2);
  if length(hex)<>130 then
    raise Exception.Create('Invalid signature');

  R := TBigInteger.Create( fromHex( Copy(hex,1, 64)  ));
  S := TBigInteger.Create( fromHex( Copy(hex, 65, 64) ));
  V := TBigInteger.Create( fromHex( Copy(hex, 129, 2) ));
end;
commented

I guess you are asking what the TBigInteger reverse is for ToByteArrayUnsigned. @Xor-el can you step in and shed some light on @CryptoTangerine's question maybe?

I think what @CryptoTangerine is asking for is the ability to decode an existing signature in the wild (in it's hex representation) to its individual components like R, S and V before proceeding to perform an ecrecover on it.
The current implementation only handles signatures generated by the library as it already generates the V component while generating the sig.
The current blocker to achieving what you want @CryptoTangerine is correctly decoding the hex sigs to its components, especially V.
There are a couple of nuances attached to it, not impossible but would require some R & D to account for the various variations in the wild.

the ability to decode an existing signature in the wild

That's right @Xor-el . While the python bridge we have set up in the meantime does this job well, it's not an elegant solution.

I did try to reverse engineer this from other languages, but it feels like I'm no closer to solving it. Any help would be much appreciated.

@svanas just saw the commit and ran successful test. Thank you so much! Really appreciate it!

commented

just saw the commit and ran successful test. Thank you so much! Really appreciate it!

@CryptoTangerine Happy to hear that, but @Xor-el did most of the work. Our of curiosity, what are you using Delphereum for?

In that case, many thanks to @Xor-el as well! This latest addition made the server verifications much faster than the previous Delphi-python bridge ever could.

@svanas We're using Delphereum for all sorts of stuff. I actually learned how transactions work (and how they are created) by studying the Delphi code last year. Our projects are mostly reading from the chain and doing some analysis, but there were also attempts at trading bots and the new ecrecover function is used together with a login website to verify wallet ownership on the server, for our blockchain-friendly online game.