UNPKG

wowok

Version:

Wowok Blockchain TypeScript API

1 lines 2.37 kB
import{bcs,toBase64}from'../_deps/bcs/index.js';import{blake2b}from'@noble/hashes/blake2b';import{bech32}from'@scure/base';import{messageWithIntent}from'./intent.js';import{SIGNATURE_FLAG_TO_SCHEME,SIGNATURE_SCHEME_TO_FLAG}from'./signature-scheme.js';import{toSerializedSignature}from'./signature.js';export const FALCON512_PRIVATE_KEY_SIZE=0x501;export const PRIVATE_KEY_SIZE=FALCON512_PRIVATE_KEY_SIZE;export const W_PRIVATE_KEY_PREFIX='wowprivkey';export class Signer{async['signWithIntent'](a,b){const c=messageWithIntent(b,a),d=blake2b(c,{'dkLen':0x20}),e=toSerializedSignature({'signature':await this['sign'](d),'signatureScheme':this['getKeyScheme'](),'publicKey':this['getPublicKey']()});return{'signature':e,'bytes':toBase64(a)};}async['signTransaction'](a){return this['signWithIntent'](a,'TransactionData');}async['signPersonalMessage'](a){const {signature:b}=await this['signWithIntent'](bcs['byteVector']()['serialize'](a)['toBytes'](),'PersonalMessage');return{'bytes':toBase64(a),'signature':b};}async['signAndExecuteTransaction']({transaction:a,client:b}){const c=await a['build']({'client':b}),{signature:d}=await this['signTransaction'](c),e=await b['core']['executeTransaction']({'transaction':c,'signatures':[d]});return e['transaction'];}['toWowAddress'](){return this['getPublicKey']()['toWAddress']();}['toWAddress'](){return this['toWowAddress']();}}export class Keypair extends Signer{}export function decodeWowPrivateKey(a){const {prefix:b,words:c}=bech32['decode'](a,![]);if(b!==W_PRIVATE_KEY_PREFIX)throw new Error('invalid\x20private\x20key\x20prefix');const d=new Uint8Array(bech32['fromWords'](c)),e=d['slice'](0x1),f=SIGNATURE_FLAG_TO_SCHEME[d[0x0]];if(f!=='Falcon512')throw new Error('Unsupported\x20signature\x20scheme:\x20'+f+'.\x20Only\x20Falcon512\x20is\x20supported.');return{'scheme':f,'secretKey':e};}export function encodeWowPrivateKey(a,b){if(b!=='Falcon512')throw new Error('Unsupported\x20signature\x20scheme:\x20'+b+'.\x20Only\x20Falcon512\x20is\x20supported.');if(a['length']!==FALCON512_PRIVATE_KEY_SIZE)throw new Error('Invalid\x20bytes\x20length.\x20Expected\x20'+FALCON512_PRIVATE_KEY_SIZE+'\x20bytes\x20for\x20'+b+',\x20got\x20'+a['length']);const c=SIGNATURE_SCHEME_TO_FLAG[b],d=new Uint8Array(a['length']+0x1);return d['set']([c]),d['set'](a,0x1),bech32['encode'](W_PRIVATE_KEY_PREFIX,bech32['toWords'](d),![]);}